Last active
August 13, 2018 14:46
-
-
Save dogbert17/f663fabba14ffe6fda2fc33182b78c46 to your computer and use it in GitHub Desktop.
Perf regression
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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