Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
12
Task/Happy-numbers/Perl/happy-numbers-1.pl
Normal file
12
Task/Happy-numbers/Perl/happy-numbers-1.pl
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
use List::Util qw(sum);
|
||||
|
||||
sub ishappy {
|
||||
my $s = shift;
|
||||
while ($s > 6 && $s != 89) {
|
||||
$s = sum(map { $_*$_ } split(//,$s));
|
||||
}
|
||||
$s == 1;
|
||||
}
|
||||
|
||||
my $n = 0;
|
||||
print join(" ", map { 1 until ishappy(++$n); $n; } 1..8), "\n";
|
||||
13
Task/Happy-numbers/Perl/happy-numbers-2.pl
Normal file
13
Task/Happy-numbers/Perl/happy-numbers-2.pl
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
use List::Util qw(sum);
|
||||
sub is_happy {
|
||||
my ($n) = @_;
|
||||
my %seen;
|
||||
while (1) {
|
||||
$n = sum map { $_ ** 2 } split //, $n;
|
||||
return 1 if $n == 1;
|
||||
return 0 if $seen{$n}++;
|
||||
}
|
||||
}
|
||||
|
||||
my $n;
|
||||
is_happy( ++$n ) and print "$n " or redo for 1..8;
|
||||
Loading…
Add table
Add a link
Reference in a new issue