Least m such that n! + m is prime: Difference between revisions
Content deleted Content added
SqrtNegInf (talk | contribs) Added Perl |
|||
Line 147:
First m > 11000 is 12619 at position 599.
First m > 12000 is 12619 at position 599.
</pre>
=={{header|Perl}}==
{{libheader|ntheory}}
<syntaxhighlight lang="perl" line>
use strict;
use warnings;
use ntheory qw<next_prime factorial vecfirstidx>;
my($n,@least_m) = 0;
do {
my $f = factorial($n++);
push @least_m, next_prime($f) - $f;
} until $least_m[-1] > 10000;
print "Least positive m such that n! + m is prime; first fifty:\n";
print sprintf(('%4d')x50, @least_m[0..49]) =~ s/.{40}\K(?=.)/\n/gr . "\n\n";
for my $n (map { 1000 * $_ } 1..10) {
my $key = vecfirstidx { $_ > $n } @least_m;
printf "First m > $n is %d at position %d\n", $least_m[$key], $key;
}
</syntaxhighlight>
{{out}}
<pre>
Least positive m such that n! + m is prime; first fifty:
1 1 1 1 5 7 7 11 23 17
11 1 29 67 19 43 23 31 37 89
29 31 31 97 131 41 59 1 67 223
107 127 79 37 97 61 131 1 43 97
53 1 97 71 47 239 101 233 53 83
First m > 1000 is 1069 at position 107
First m > 2000 is 3391 at position 192
First m > 3000 is 3391 at position 192
First m > 4000 is 4943 at position 284
First m > 5000 is 5233 at position 384
First m > 6000 is 6131 at position 388
First m > 7000 is 9067 at position 445
First m > 8000 is 9067 at position 445
First m > 9000 is 9067 at position 445
First m > 10000 is 12619 at position 599
</pre>
|