Skip to content

Instantly share code, notes, and snippets.

@hunj
Created February 7, 2016 20:04
Show Gist options
  • Save hunj/87744a998fc1df2d8b6e to your computer and use it in GitHub Desktop.
Save hunj/87744a998fc1df2d8b6e to your computer and use it in GitHub Desktop.
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