Find prime numbers of the form n*n*n+2: Difference between revisions

Content added Content deleted
(Realize in F#)
(Added Perl)
Line 261: Line 261:
<lang julia>using Primes; println(filter(isprime, map(x -> x^3 + 2, 1:199)))</lang>{{out}}<pre>
<lang julia>using Primes; println(filter(isprime, map(x -> x^3 + 2, 1:199)))</lang>{{out}}<pre>
[3, 29, 127, 24391, 91127, 250049, 274627, 328511, 357913, 571789, 1157627, 1442899, 1860869, 2146691, 2924209, 3581579, 5000213, 5177719, 6751271]</pre>
[3, 29, 127, 24391, 91127, 250049, 274627, 328511, 357913, 571789, 1157627, 1442899, 1860869, 2146691, 2924209, 3581579, 5000213, 5177719, 6751271]</pre>

=={{header|Perl}}==
<lang perl>use strict;
use warnings;
use feature 'say';

# basic task results
say join ' ', grep { is_prime $_ } map { $_**3 + 2 } grep { 0 != $_%2 } 1..199;

# generalize a bit, how many primes over a range of exponents and offsets?
use Math::AnyNum ':all'; # in order to handle large values
say ' ' . sprintf '%4d'x11 , 1..10;
for my $e (1..10) {
printf '%2d ', $e;
for my $o (1..10) {
printf '%4d', scalar grep { is_prime $_ } map { $_**$e + $o } 1..199;
}
print "\n";
}</lang>

{{out}}
<pre>3 29 127 24391 91127 250049 274627 328511 357913 571789 1157627 1442899 1860869 2146691 2924209 3581579 5000213 5177719 6751271

1 2 3 4 5 6 7 8 9 10
1 46 45 44 44 43 43 42 42 42 42
2 34 17 29 34 12 19 49 19 24 32
3 1 19 26 25 23 17 18 0 28 20
4 30 13 20 1 7 12 28 7 6 11
5 1 12 14 14 11 7 15 17 12 3
6 1 5 19 11 3 2 24 0 7 11
7 1 10 8 8 7 9 7 6 9 8
8 7 7 7 1 2 5 9 5 1 8
9 1 5 7 7 5 5 6 0 9 6
10 1 3 3 9 3 1 5 3 2 1
</pre>


=={{header|Phix}}==
=={{header|Phix}}==