-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy path277 A Modified Collatz sequence.pl
More file actions
60 lines (45 loc) · 949 Bytes
/
277 A Modified Collatz sequence.pl
File metadata and controls
60 lines (45 loc) · 949 Bytes
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
59
60
#!/usr/bin/perl
# Author: Daniel "Trizen" Șuteu
# Date: 25 August 2016
# License: GPLv3
# Website: https://github.com/trizen
# https://projecteuler.net/problem=277
# Runtime: 0.015s
use 5.010;
use strict;
sub collatz {
my ($n) = @_;
my $acc = '';
while (1) {
last if $n == 1;
if ($n % 3 == 0) {
$n /= 3;
$acc .= 'D';
}
elsif ($n % 3 == 1) {
$n = (4 * $n + 2) / 3;
$acc .= 'U';
}
elsif ($n % 3 == 2) {
$n = (2 * $n - 1) / 3;
$acc .= 'd';
}
}
$acc;
}
my $step = 1;
my $from = 10**15;
my $j = 1;
my $s = 'UDDDUdddDDUDDddDdDddDDUDDdUUDd';
my $l = length($s);
for (my $i = $from ; ; $i += $step) {
my $c = collatz($i);
if (substr($c, 0, $j) eq substr($s, 0, $j)) {
if ($j == $l) {
say $i;
last;
}
$step *= 3;
$j += 1;
}
}