Skip to content

Instantly share code, notes, and snippets.

@dogbert17
Last active August 13, 2018 14:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dogbert17/f663fabba14ffe6fda2fc33182b78c46 to your computer and use it in GitHub Desktop.
Save dogbert17/f663fabba14ffe6fda2fc33182b78c46 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;
say now - INIT now;
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