Iterated digits squaring: Difference between revisions
→{{header|Perl 6}}: Replace nqp with fast algorithm example
Thundergnat (talk | contribs) (→{{header|Perl 6}}: Replace nqp with fast algorithm example) |
|||
Line 1,718:
{{out}}
<pre>85744333</pre>
Which is better, but is still pretty slow.
The biggest gains are often from choosing the right algorithm.
$n < 650▼
<lang perl6>sub
my
my
my
$nn = $nn div 10;
$sum = 0;
}
$n;▼
}
my
my @sums is default(0) = 1,0;
for 1 .. 100_000_000 -> int $n {▼
my $s;
for ($n*81 ... 1) -> $i {
for (1 .. 9) -> $j {
$s = $j²;
last if $s > $i;
@sums[$i] += @sums[$i - $s];
}
my $ends-with-one = sum flat @sums[(1 .. $k*81).grep: { endsWithOne($_) }], +endsWithOne(10**$k);
say 10**$k - $ends-with-one;</lang>
{{out}}
<pre>85744333</pre>
=={{header|Phix}}==
|