Created
December 22, 2013 10:36
-
-
Save elzup/8080703 to your computer and use it in GitHub Desktop.
Rubyの練習
完全数をコンソール出力
This file contains 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
MAX = 1000 | |
def sum(*list) | |
s = 0 | |
if list.size == 1 then | |
return list | |
end | |
for val in list do | |
s += val | |
end | |
return s | |
end | |
def getDivisors(k) | |
a = [1] | |
# print 1.to_s | |
for x in 2...k do | |
if k % x == 0 then | |
# print "," + x.to_s | |
a.push(x) | |
end | |
end | |
# puts "" | |
return a | |
end | |
def isPerfect? (n) | |
if n < 5 then return false end | |
list = getDivisors(n) | |
s = 0 | |
for val in list do | |
s += val | |
end | |
# puts n.to_s + ":" + s.to_s | |
return n == s | |
end | |
counter = 0 | |
list = [] | |
MAX.times do |x| | |
if isPerfect?(x) then | |
puts "\"" + x.to_s + "\"" | |
list.push(x) | |
counter += 1 | |
end | |
if x % 10 == 0 then | |
puts "-----" + x.to_s | |
end | |
end | |
puts "count: " + counter.to_s | |
puts list.join(', ') | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment