Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
SEGV/OOM when running with --profile
dogbert@dogbert-VirtualBox:~$ cat euler-14.pl6
# Longest Collatz sequence - Which starting number, under one million, produces the longest chain?
#real 2m18.424s
#user 2m17.600s
#sys 0m0.056s
# This is Rakudo version 2016.06-11-g2c61fda built on MoarVM version 2016.06
use v6;
my $maxChainLen = 0;
my $maxChainLenNum = 0;
my @cache is default(0);
for (1..1_000_000) -> $x {
my $num = $x;
my $chainLen = 0;
while $num > 1 {
if $num < 75_000 && (@cache[$num] != 0) {
$chainLen += @cache[$num];
last;
}
$chainLen++;
$num = ($num % 2 == 0) ?? $num div 2 !! 3 * $num + 1;
}
$chainLen++ if $num == 1;
@cache[$x] = $chainLen if $x < 75_000;
if $chainLen > $maxChainLen {
$maxChainLen = $chainLen;
$maxChainLenNum = $x;
}
}
say "Max chain length was $maxChainLen for number $maxChainLenNum";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment