-
Notifications
You must be signed in to change notification settings - Fork 34
/
collatz_function.pl
executable file
·58 lines (41 loc) · 1.25 KB
/
collatz_function.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/perl
# Daniel "Trizen" Șuteu
# Date: 06 July 2018
# https://github.com/trizen
# The following 1628-digit number: 46785696846401151 * 3^3377 requires 41763 steps to get down to 1.
# Collatz function on higher powers of 3 multiplied with n = 46785696846401151:
# collatz(n * 3^8818) = 101856
# collatz(n * 3^9071) = 106610
# collatz(n * 3^9296) = 108210
# collatz(n * 3^9586) = 110042
# collatz(n * 3^9660) = 113569
# collatz(n * 3^9870) = 113951
# See also:
# https://oeis.org/A006877
# https://oeis.org/A006577
use 5.010;
use strict;
use warnings;
use Math::GMPz;
sub collatz {
my ($n) = @_;
$n = Math::GMPz->new("$n");
state $two = Math::GMPz::Rmpz_init_set_ui(2);
my $count = Math::GMPz::Rmpz_remove($n, $n, $two);
while (Math::GMPz::Rmpz_cmp_ui($n, 1) > 0) {
Math::GMPz::Rmpz_mul_ui($n, $n, 3);
Math::GMPz::Rmpz_add_ui($n, $n, 1);
$count += 1 + Math::GMPz::Rmpz_remove($n, $n, $two);
}
return $count;
}
my $factor = Math::GMPz->new("46785696846401151");
my $base = Math::GMPz->new(3);
my $max = 0;
foreach my $n (0 .. 2500) {
my $t = collatz($factor * $base**$n);
if ($t > $max) {
say "collatz($factor * $base^$n) = $t";
$max = $t;
}
}