happy_numbers.sf 331 B

12345678910111213141516171819202122
  1. #!/usr/bin/ruby
  2. #
  3. ## https://rosettacode.org/wiki/Happy_numbers
  4. #
  5. func happy(n) is cached {
  6. static seen = Hash();
  7. return true if n.is_one;
  8. return false if seen.has_key(n);
  9. seen{n} = 1;
  10. happy(n.digits »**» 2 -> sum)
  11. }
  12. var count = 0;
  13. { |i|
  14. happy(i) ? say i : next;
  15. ++count == 8 && break;
  16. } * Inf;