We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 989cd3d commit 31c1f20Copy full SHA for 31c1f20
euler/prob014-felher.pl
@@ -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