-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy path443 GCD sequence.pl
More file actions
46 lines (33 loc) · 877 Bytes
/
443 GCD sequence.pl
File metadata and controls
46 lines (33 loc) · 877 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
#!/usr/bin/perl
# Daniel "Trizen" Șuteu
# Date: 03 September 2019
# https://github.com/trizen
# https://projecteuler.net/problem=443
# Runtime: 4.988s
use 5.020;
use ntheory qw(:all);
use experimental qw(signatures);
sub GCD_sequence ($upto) {
my $sum = 13;
for (my $n = 5 ; $n <= $upto ; ++$n) {
if ($n >= 100 and gcd($n, $sum) > 1) {
my $k = $n + ($n - 1);
$sum += gcd($n, $sum);
if (is_prime($k)) {
if ($k >= $upto) {
$sum += $upto - $n;
last;
}
$sum += $k - ($n - 1);
$n = $k;
}
}
else {
$sum += gcd($n, $sum);
}
}
return $sum;
}
say GCD_sequence(1000); #=> 2524
say GCD_sequence(1000000); #=> 2624152
say GCD_sequence(1000000000000000);