-
Notifications
You must be signed in to change notification settings - Fork 0
/
prog_fast.pl
88 lines (70 loc) · 3.73 KB
/
prog_fast.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/perl
# a(n) is the largest prime < 10^n obtained with the longest sum of consecutive primes.
# https://oeis.org/A342439
# The longest length of consecutive primes which sums to a prime (A342439(n)) < 10^n.
# https://oeis.org/A342440
# Algorithm from:
# https://blog.dreamshire.com/project-euler-50-solution/
use 5.010;
use strict;
use warnings;
use ntheory qw(:all);
foreach my $n (1 .. 100) {
my $limit = powint(10, $n);
my @prime_sum = (0);
for (my $p = 2 ; ; $p = next_prime($p)) {
my $sum = $prime_sum[-1] + $p;
last if ($sum >= $limit);
push @prime_sum, $sum;
}
my $terms = 1;
my $max_prime = 2;
my $start_p_index = 1;
foreach my $i (0 .. $#prime_sum) {
for (my $j = $#prime_sum ; $j >= $i + $terms ; --$j) {
my $n = $prime_sum[$j] - $prime_sum[$i];
if (($j - $i > $terms or $n > $max_prime) and $n < $limit and is_prime($n)) {
($terms, $max_prime, $start_p_index) = ($j - $i, $n, $i + 1);
last;
}
}
}
say "$max_prime is the sum of $terms consecutive primes with first prime = ", nth_prime($start_p_index);
}
__END__
# Results with the largest number of consecutive primes that sum to an n-digit prime and with the smallest starting prime:
5 is the sum of 2 consecutive primes with first prime = 2
41 is the sum of 6 consecutive primes with first prime = 2
953 is the sum of 21 consecutive primes with first prime = 7
9521 is the sum of 65 consecutive primes with first prime = 3
92951 is the sum of 183 consecutive primes with first prime = 3
997651 is the sum of 543 consecutive primes with first prime = 7
9951191 is the sum of 1587 consecutive primes with first prime = 5
99819619 is the sum of 4685 consecutive primes with first prime = 7
999715711 is the sum of 13935 consecutive primes with first prime = 11
9999419621 is the sum of 41708 consecutive primes with first prime = 2
99987684473 is the sum of 125479 consecutive primes with first prime = 19
999973156643 is the sum of 379317 consecutive primes with first prime = 5
9999946325147 is the sum of 1150971 consecutive primes with first prime = 5
99999863884699 is the sum of 3503790 consecutive primes with first prime = 2
999998764608469 is the sum of 10695879 consecutive primes with first prime = 7
9999994503821977 is the sum of 32729271 consecutive primes with first prime = 5
99999999469565483 is the sum of 100361001 consecutive primes with first prime = 5
# Results with the largest number of consecutive primes that sum to an n-digit prime and with the largest sum:
5 is the sum of 2 consecutive primes with first prime = 2
41 is the sum of 6 consecutive primes with first prime = 2
953 is the sum of 21 consecutive primes with first prime = 7
9521 is the sum of 65 consecutive primes with first prime = 3
92951 is the sum of 183 consecutive primes with first prime = 3
997651 is the sum of 543 consecutive primes with first prime = 7
9964597 is the sum of 1587 consecutive primes with first prime = 7
99819619 is the sum of 4685 consecutive primes with first prime = 7
999715711 is the sum of 13935 consecutive primes with first prime = 11
9999419621 is the sum of 41708 consecutive primes with first prime = 2
99987684473 is the sum of 125479 consecutive primes with first prime = 19
999973156643 is the sum of 379317 consecutive primes with first prime = 5
9999946325147 is the sum of 1150971 consecutive primes with first prime = 5
99999863884699 is the sum of 3503790 consecutive primes with first prime = 2
999999149973119 is the sum of 10695879 consecutive primes with first prime = 13
9999994503821977 is the sum of 32729271 consecutive primes with first prime = 5
99999999469565483 is the sum of 100361001 consecutive primes with first prime = 5