Created
March 25, 2013 17:09
-
-
Save takehiko/5238751 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# -*- coding: utf-8 -*- | |
# threeposition.rb by takehikom | |
# ruby threeposition.rb 541 => "541-145=396, 396+693=1089" | |
# ruby threeposition.rb | |
# Inspired by | |
# 藤井斉亮「数学的問題解決過程における文字式の役割と機能」, | |
# 『続・新しい算数数学教育の実践をめざして―杉山吉茂先生喜寿記念論文集』 | |
# (isbn:9784491028538), pp.198-208. | |
def reverse3(n, flag_comp = false) | |
n = n.to_i | |
if flag_comp && (n < 100 || n > 999) | |
raise "outlying number" | |
end | |
na = (n + 1000).to_s.split(//)[-3, 3].map {|i| i.to_i} | |
# puts "#{n}:#{na.join(',')}" | |
if flag_comp && na[0] <= na[2] | |
raise "wrong number" | |
end | |
na[2] * 100 + na[1] * 10 + na[0] | |
end | |
def threeposition(n) | |
begin | |
n_rev = reverse3(n.to_i, true) | |
m = n.to_i - n_rev | |
m_rev = reverse3(m) | |
p = m + m_rev | |
puts "#{n}-#{n_rev}=#{m}, #{m}+#{m_rev}=#{p}" | |
rescue RuntimeError => msg | |
# puts "#{n}: #{msg}" | |
end | |
end | |
if __FILE__ == $0 | |
if ARGV.empty? | |
100.upto(999) {|i| threeposition(i)} | |
else | |
ARGV.each do |n| | |
threeposition(n) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment