Created
June 16, 2018 13:42
-
-
Save dogbert17/2ea5e9408055934f88d7205d83e0422c to your computer and use it in GitHub Desktop.
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..1_000_000) -> $i { | |
$sum89++ if @cache[get-sq-sum($i)] == 89; | |
} | |
say $sum89; | |
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