Created
December 15, 2014 22:50
-
-
Save darkness51/b57be62dd867564d6321 to your computer and use it in GitHub Desktop.
Perfect Number
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
class Perfection | |
def self.instance | |
@instance ||= Perfection.new | |
end | |
def is_perfect(candidate) | |
ret_val = false | |
divisors = get_divisors(candidate) | |
sum = 0 | |
divisors.each do |number| | |
sum += number | |
end | |
ret_val = true if sum == candidate | |
ret_val | |
end | |
def get_divisors(candidate) | |
divisors = [] | |
1.upto(candidate - 1) do |i| | |
foo = candidate / i | |
if (foo*i) == candidate | |
divisors << i | |
end | |
end | |
divisors | |
end | |
end | |
print "Introduce a number to see if is perfect: " | |
n = gets.chomp | |
n = n.to_i | |
puts "The number #{n} Perfection.instance.is_perfect(n) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment