Skip to content

Instantly share code, notes, and snippets.

@katpadi katpadi/num_feels.rb
Last active Jan 9, 2018

Embed
What would you like to do?
Number Feels
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
You can’t perform that action at this time.