Skip to content

Instantly share code, notes, and snippets.

@darkness51
Created December 15, 2014 22:50
Show Gist options
  • Save darkness51/b57be62dd867564d6321 to your computer and use it in GitHub Desktop.
Save darkness51/b57be62dd867564d6321 to your computer and use it in GitHub Desktop.
Perfect Number
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