Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Fixnum
- def happy?
- n = self
- [1, 7].include? Enumerator.new {|y|
- loop {
- y << (n = n.to_s.chars.map {|x| x.to_i ** 2}.reduce(:+))
- }
- }.lazy.select{|y| y < 10}.take(1).first
- end
- end
- raise "Errado" unless [1, 10, 100, 130, 97, 7, 1112].collect(&:happy?).all?
- raise "Errado" unless ![2, 3, 4, 5, 6, 8, 8].collect(&:happy?).any?
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement