Skip to content

Commit 31c1f20

Browse files
committed
[euler] add solution for problem 14
1 parent 989cd3d commit 31c1f20

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

euler/prob014-felher.pl

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
use v6;
2+
3+
# this program takes quite a few minutes on my machine
4+
5+
my Int multi sub collatz(Int $n where * %% 2) { $n div 2; }
6+
my Int multi sub collatz(Int $n ) { 3 * $n + 1; }
7+
8+
my Int sub get-length(Int $n) {
9+
state Int %length{Int} = 1 => 1;
10+
my $result = %length{$n} // 1 + get-length collatz $n;
11+
%length{$n} = $result;
12+
$result;
13+
}
14+
15+
my $max = 0;
16+
my $start = 0;
17+
for 1 ..^ 1_000_000 -> $n {
18+
say $n; #just for progress
19+
my $length = get-length $n;
20+
21+
if $length > $max {
22+
$start = $n;
23+
$max = $length;
24+
}
25+
}
26+
27+
say $max;
28+
say $start;

0 commit comments

Comments
 (0)