Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
perfect number!
def perfect_number? number
return number == divisors_of(number)
end
def divisors_of number
arr = []
(1..number).each do |iter|
if number % iter == 0
arr << iter
end
end
arr
end
def sum array
sum = 0
for elem in array do
sum += elem
end
sum
end
def perfect_numbers_in low, high
arr = []
(low..high).each do |iter|
if perfect_number? iter
arr << iter
end
end
arr
end
p perfect_numbers_in 1, 1000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment