Created
March 15, 2018 22:29
-
-
Save ysnerdem/95e46b80c77be2ecc796ce0e5f92d806 to your computer and use it in GitHub Desktop.
Deficient Numbers
#3 different method
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 is_deficient(num) | |
sum = 0 | |
(1..num).each {|i| sum += i if num % i == 0} | |
if sum < (2 * num) | |
"#{num} is deficient number" | |
else | |
"#{num} is not deficient number" | |
end | |
end | |
def find_deficients(range) | |
(1..range).each do |num| | |
sum = 0 | |
(1..num).each {|i| sum += i if num % i == 0} | |
if sum < (2 * num) | |
puts num | |
end | |
end | |
end | |
def is_deficient_v2(num) | |
sum = 0 | |
print "Factors: " | |
(1..num).each do |i| | |
if num % i == 0 | |
print "#{i}, " | |
sum += i | |
end | |
end | |
print "\nSum of factors: #{sum}" | |
if sum < (num * 2) | |
print "\n#{num} is deficient number" | |
else | |
print "\n#{num} is not deficient number" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment