Created
February 7, 2016 20:04
-
-
Save hunj/87744a998fc1df2d8b6e to your computer and use it in GitHub Desktop.
perfect number!
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
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