Last active
January 9, 2018 11:26
-
-
Save katpadi/d845e6fee85e883bf58dd2fe6275c33c to your computer and use it in GitHub Desktop.
Number Feels
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
class NumFeels | |
attr_reader :number | |
def initialize(number) | |
@number = number | |
end | |
def happy? | |
fail ArgumentError if number < 0 | |
sum = 0 | |
i = number | |
while [1,4,0].exclude? i | |
sums = [] | |
i.to_s.split('').each do |digit| | |
sums << digit.to_i**2 | |
end | |
sum = sums.inject(0, &:+) | |
return true if sum == 1 | |
i = sum | |
end | |
false | |
end | |
def perfect? | |
factors = proper_factors_of(number) | |
sum = factors.inject(0, &:+) | |
sum == number | |
end | |
def friendly? | |
end | |
def solitary? | |
end | |
private | |
def proper_factors_of(n) | |
(1..n/2).select{|e| (n%e).zero? } | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment