Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AlexDaniel/227c34f20d638d0b339947c744d05bc8 to your computer and use it in GitHub Desktop.
Save AlexDaniel/227c34f20d638d0b339947c744d05bc8 to your computer and use it in GitHub Desktop.
Perf regression
use v6;
my Int @cache is default(0);
my $sum89 = 0;
for (1..567) -> $i {
@cache[$i] = calc-chain($i);
}
for (1..400_000) -> $i {
$sum89++ if @cache[get-sq-sum($i)] == 89;
}
#say $sum89;
my $x = now - INIT now;
say $x;
#exit 42 if $x > 3.4;
sub calc-chain($n) {
my $sq-sum = get-sq-sum($n);
$sq-sum != 1|89 ?? calc-chain($sq-sum) !! $sq-sum;
}
sub get-sq-sum(int $i is copy) {
my $sum = 0;
while $i > 0 {
$sum += ($i % 10) ** 2;
$i = $i div 10;
}
$sum;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment