aboutsummaryrefslogtreecommitdiff
path: root/challenge-164/kjetillll/perl/ch-2.pl
blob: a7f0af036f2afcf270c3d293173f4f3fc2086d99 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#!/usr/bin/perl
use List::Util 'sum'; use strict; use warnings;

sub is_happy {
    my $n = shift;
    my(%seen,$happy);
    $happy = sub {
	my $n = shift;
	$n==1 ? 1 : $seen{$n}++ ? 0 : &$happy(sum( map $_**2, $n=~/\d/g ));
    };
    &$happy( $n );
}

my($want, $count, $n) = (8, 0, 0);

is_happy(++$n)
  and print "Found happy number ".++$count.": $n\n"
    while $count < $want;