Find prime numbers of the form n*n*n+2: Difference between revisions
Find prime numbers of the form n*n*n+2 (view source)
Revision as of 07:47, 16 April 2024
, 1 month agoAdded Easylang
Not a robot (talk | contribs) (Add COBOL) |
(Added Easylang) |
||
(16 intermediate revisions by 13 users not shown) | |||
Line 3:
;Task: Find prime numbers of the form <big> n<sup>''3''</sup>+2</big>, where 0 < n < 200
<br><br>
=={{header|11l}}==
{{trans|Python}}
<syntaxhighlight lang="11l">
F isPrime(n)
L(i) 2 .. Int(n ^ 0.5)
I n % i == 0
R 0B
R 1B
L(n) 1..199
I isPrime(n ^ 3 + 2)
print(n"\t"(n ^ 3 + 2))
</syntaxhighlight>
{{out}}
<pre>
1 3
3 29
5 127
29 24391
45 91127
63 250049
65 274627
69 328511
71 357913
83 571789
105 1157627
113 1442899
123 1860869
129 2146691
143 2924209
153 3581579
171 5000213
173 5177719
189 6751271
</pre>
=={{header|Ada}}==
<
procedure Find_Primes is
Line 20 ⟶ 58:
if A mod 3 = 0 then return False; end if;
D := 5;
while D * D <= A loop
if A mod D = 0 then
return False;
Line 45 ⟶ 83:
end if;
end loop;
end Find_Primes;</
{{out}}
<pre> N N**3+2
Line 70 ⟶ 108:
=={{header|ALGOL 68}}==
<
FOR n TO 199 DO
INT candidate = ( n * n * n ) + 2;
Line 84 ⟶ 122:
FI
OD
END</
{{out}}
<pre>
Line 109 ⟶ 147:
=={{header|ALGOL W}}==
<
for n := 1 until 199 do begin
integer candidate;
Line 127 ⟶ 165:
end if_isPrime
end for_n
end.</
{{out}}
<pre>
Line 150 ⟶ 188:
189: 6751271
</pre>
=={{header|Arturo}}==
<syntaxhighlight lang="arturo">primes: []
loop 1..199 'i [
num: 2 + i^3
if prime? num ->
'primes ++ @[to :string i, to :string num]
]
loop primes [i, num][
prints pad i 4
print pad num 9
]</syntaxhighlight>
{{out}}
<pre> 1 3
3 29
5 127
29 24391
45 91127
63 250049
65 274627
69 328511
71 357913
83 571789
105 1157627
113 1442899
123 1860869
129 2146691
143 2924209
153 3581579
171 5000213
173 5177719
189 6751271</pre>
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f FIND_PRIME_NUMBERS_OF_THE_FORM_NNN2.AWK
BEGIN {
Line 178 ⟶ 251:
return(1)
}
</syntaxhighlight>
{{out}}
<pre>
Line 205 ⟶ 278:
=={{header|C}}==
{{trans|Wren}}
<
#include <stdbool.h>
#include <locale.h>
Line 235 ⟶ 308:
}
return 0;
}</
{{out}}
Line 261 ⟶ 334:
=={{header|C++}}==
<
#include <iostream>
Line 287 ⟶ 360:
std::cout << std::setw(3) << n << std::setw(9) << p << '\n';
}
}</
{{out}}
Line 313 ⟶ 386:
=={{header|CLU}}==
<
if n<2 then return(false) end
if n//2=0 then return(n=2) end
Line 344 ⟶ 417:
stream$putl(po, "")
end
end start_up</
{{out}}
<pre>n = 1 => n^3 + 2 = 3
Line 367 ⟶ 440:
=={{header|COBOL}}==
<
PROGRAM-ID. N3-PLUS-2-PRIMES.
Line 437 ⟶ 510:
CHECK-PRIME-DONE.
EXIT.</
{{out}}
<pre>N = 1 => N ** 3 + 2 = 3
Line 460 ⟶ 533:
=={{header|Cowgol}}==
<
sub is_prime(n: uint32): (p: uint8) is
Line 495 ⟶ 568:
end if;
n := n+1;
end loop;</
{{out}}
<pre>n = 1 => n^3 + 2 = 3
Line 520 ⟶ 593:
{{libheader| System.SysUtils}}
{{libheader| PrimTrial}}
<syntaxhighlight lang="delphi">
program Find_prime_numbers_of_the_form_n_n_n_plus_2;
Line 548 ⟶ 621:
end;
readln;
end.</
{{out}}
<pre>n = 1 => n^3 + 2 = 3
Line 569 ⟶ 642:
n = 173 => n^3 + 2 = 5,177,719
n = 189 => n^3 + 2 = 6,751,271</pre>
=={{header|Draco}}==
<syntaxhighlight lang="draco">proc nonrec is_prime(ulong n) bool:
ulong d;
bool prime;
if n<=4 then n=2 or n=3
elif n&1=0 or n%3=0 then false
else
d := 5;
prime := true;
while prime and d*d <= n do
if n%d=0 then prime := false fi;
d := d+2;
if n%d=0 then prime := false fi;
d := d+4
od;
prime
fi
corp
proc nonrec main() void:
word n;
ulong p;
for n from 1 upto 200 do
p := make(n,ulong);
p := p*p*p + 2;
if is_prime(p) then
writeln("n = ", n:3, " => n^3 + 2 = ", p:7)
fi
od
corp</syntaxhighlight>
{{out}}
<pre>n = 1 => n^3 + 2 = 3
n = 3 => n^3 + 2 = 29
n = 5 => n^3 + 2 = 127
n = 29 => n^3 + 2 = 24391
n = 45 => n^3 + 2 = 91127
n = 63 => n^3 + 2 = 250049
n = 65 => n^3 + 2 = 274627
n = 69 => n^3 + 2 = 328511
n = 71 => n^3 + 2 = 357913
n = 83 => n^3 + 2 = 571789
n = 105 => n^3 + 2 = 1157627
n = 113 => n^3 + 2 = 1442899
n = 123 => n^3 + 2 = 1860869
n = 129 => n^3 + 2 = 2146691
n = 143 => n^3 + 2 = 2924209
n = 153 => n^3 + 2 = 3581579
n = 171 => n^3 + 2 = 5000213
n = 173 => n^3 + 2 = 5177719
n = 189 => n^3 + 2 = 6751271</pre>
=={{header|EasyLang}}==
<syntaxhighlight>
fastfunc isprim num .
i = 2
while i <= sqrt num
if num mod i = 0
return 0
.
i += 1
.
return 1
.
for n = 1 to 199
p = pow n 3 + 2
if isprim p = 1
write p & " "
.
.
</syntaxhighlight>
{{out}}
<pre>
3 29 127 24391 91127 250049 274627 328511 357913 571789 1157627 1442899 1860869 2146691 2924209 3581579 5000213 5177719 6751271
</pre>
=={{header|F_Sharp|F#}}==
This task uses [[Extensible_prime_generator#The_functions|Extensible Prime Generator (F#)]].<br>
<
[1..2..200]|>Seq.filter(fun n->isPrime(2+pown n 3))|>Seq.iter(fun n->printfn "n=%3d -> %d" n (2+pown n 3))
</syntaxhighlight>
{{out}}
<pre>
Line 597 ⟶ 745:
n=189 -> 6751271
</pre>
=={{header|Factor}}==
Using the parity optimization from the Wren entry:
{{works with|Factor|0.99 2021-02-05}}
<
math.ranges sequences tools.memory.private ;
Line 606 ⟶ 755:
dup 3 ^ 2 + dup prime?
[ commas "n = %3d => n³ + 2 = %9s\n" printf ] [ 2drop ] if
] each</
Or, using local variables:
{{trans|Wren}}
{{works with|Factor|0.99 2021-02-05}}
<
tools.memory.private ;
Line 620 ⟶ 769:
[ n p commas "n = %3d => n³ + 2 = %9s\n" printf ] when
] each
]</
{{out}}
<pre>
Line 645 ⟶ 794:
=={{header|Fermat}}==
<
{{out}}
<pre>
Line 671 ⟶ 820:
=={{header|Forth}}==
{{works with|Gforth}}
<
dup 2 < if drop false exit then
dup 2 mod 0= if 2 = exit then
Line 696 ⟶ 845:
main
bye</
{{out}}
Line 723 ⟶ 872:
=={{header|FreeBASIC}}==
Use the code from [[Primality by trial division#FreeBASIC]] as an include.
<
for n as uinteger = 1 to 200
Line 729 ⟶ 878:
print n, n^3+2
end if
next n</
{{out}}
<pre>
Line 751 ⟶ 900:
173 5177719
189 6751271
</pre>
=={{header|Frink}}==
<syntaxhighlight lang="frink">for n = 1 to 199
if isPrime[n^3 + 2]
println["$n\t" + n^3+2]</syntaxhighlight>
{{out}}
<pre>
1 3
3 29
5 127
29 24391
45 91127
63 250049
65 274627
69 328511
71 357913
83 571789
105 1157627
113 1442899
123 1860869
129 2146691
143 2924209
153 3581579
171 5000213
173 5177719
189 6751271
</pre>
=={{header|Fōrmulæ}}==
{{FormulaeEntry|page=https://formulae.org/?script=examples/Find_prime_numbers_of_the_form_n%C2%B3%2B2}}
'''Solution'''
[[File:Fōrmulæ - Find prime numbers of the form n³ + 2 01.png]]
[[File:Fōrmulæ - Find prime numbers of the form n³ + 2 02.png]]
=={{header|Go}}==
<
import "fmt"
Line 813 ⟶ 991:
}
}
}</
{{out}}
Line 839 ⟶ 1,017:
=={{header|J}}==
<
{{out}}
<pre> 1 3
Line 867 ⟶ 1,045:
Using a definition of `is_prime` such as can be found at
[[Safe_primes_and_unsafe_primes]]:
<syntaxhighlight lang="jq">
range(1;200) | pow(.; 3) + 2 | select(is_prime)
</syntaxhighlight>
{{out}}
<pre>
Line 894 ⟶ 1,072:
=={{header|Julia}}==
<
using Primes, Formatting
Line 915 ⟶ 1,093:
filterprintresults(isncubedplus2prime, 0, 200, tostring)
</
<pre>
n = 1 => n³ + 2 = 3
Line 940 ⟶ 1,118:
</pre>
=== One-liner version ===
<
[3, 29, 127, 24391, 91127, 250049, 274627, 328511, 357913, 571789, 1157627, 1442899, 1860869, 2146691, 2924209, 3581579, 5000213, 5177719, 6751271]</pre>
=={{header|MAD}}==
<
INTERNAL FUNCTION(P)
Line 970 ⟶ 1,148:
END OF CONDITIONAL
LOOP CONTINUE
END OF PROGRAM</
{{out}}
<pre>N = 1 N*N*N + 2 = 3
Line 993 ⟶ 1,171:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
{{out}}
<pre>{3, 29, 127, 24391, 91127, 250049, 274627, 328511, 357913, 571789, 1157627, 1442899, 1860869, 2146691, 2924209, 3581579, 5000213, 5177719, 6751271}</pre>
=={{header|Nim}}==
<
func isPrime(n: Positive): bool =
Line 1,015 ⟶ 1,193:
let p = n * n * n + 2
if p.isPrime:
echo ($n).align(3), " → ", p</
{{out}}
Line 1,039 ⟶ 1,217:
=={{header|PARI/GP}}==
<
{{out}}
<pre>
Line 1,061 ⟶ 1,239:
173 5177719
189 6751271
</pre>
=={{header|Pascal}}==
==={{header|Free Pascal}}===
{{trans|Delphi}}
{{libheader| PrimTrial}}
<syntaxhighlight lang="pascal">
program Find_prime_numbers_of_the_form_n_n_n_plus_2;
{$IFDEF FPC}
{$MODE DELPHI} {$Optimization ON,ALL} {$COPERATORS ON}{$CODEALIGN proc=16}
{$ENDIF}
{$IFDEF WINDOWS}
{$APPTYPE CONSOLE}
{$ENDIF}
uses
PrimTrial;
type
myString = String[31];
function Numb2USA(n:Uint64):myString;
const
//extend s by the count of comma to be inserted
deltaLength : array[0..24] of byte =
(0,0,0,0,1,1,1,2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,7,7);
var
pI :pChar;
i,j : NativeInt;
Begin
str(n,result);
i := length(result);
//extend s by the count of comma to be inserted
// j := i+ (i-1) div 3;
j := i+deltaLength[i];
if i<> j then
Begin
setlength(result,j);
pI := @result[1];
dec(pI);
while i > 3 do
Begin
//copy 3 digits
pI[j] := pI[i];
pI[j-1] := pI[i-1];
pI[j-2] := pI[i-2];
// insert comma
pI[j-3] := ',';
dec(i,3);
dec(j,4);
end;
end;
end;
function n3_2(n:Uint32):Uint64;inline;
begin
n3_2 := UInt64(n)*n*n+2;
end;
const
limit =200;//trunc(exp(ln((HIGH(UInt32)-2))/3));
var
p : Uint64;
n : Uint32;
begin
n := 1;
repeat
p := n3_2(n);
if isPrime(p) then
writeln('n = ', Numb2USA(n):4, ' => n^3 + 2 = ', Numb2USA(p): 10);
inc(n,2);// n must be odd for n > 0
until n > Limit;
{$IFDEF WINDOWS}
readln;
{$IFEND}
end.</syntaxhighlight>
{{out}}
<pre>
n = 1 => n^3 + 2 = 3
n = 3 => n^3 + 2 = 29
n = 5 => n^3 + 2 = 127
n = 29 => n^3 + 2 = 24,391
n = 45 => n^3 + 2 = 91,127
n = 63 => n^3 + 2 = 250,049
n = 65 => n^3 + 2 = 274,627
n = 69 => n^3 + 2 = 328,511
n = 71 => n^3 + 2 = 357,913
n = 83 => n^3 + 2 = 571,789
n = 105 => n^3 + 2 = 1,157,627
n = 113 => n^3 + 2 = 1,442,899
n = 123 => n^3 + 2 = 1,860,869
n = 129 => n^3 + 2 = 2,146,691
n = 143 => n^3 + 2 = 2,924,209
n = 153 => n^3 + 2 = 3,581,579
n = 171 => n^3 + 2 = 5,000,213
n = 173 => n^3 + 2 = 5,177,719
n = 189 => n^3 + 2 = 6,751,271
</pre>
=={{header|Perl}}==
<
use warnings;
use feature 'say';
Line 1,080 ⟶ 1,354:
}
print "\n";
}</
{{out}}
Line 1,099 ⟶ 1,373:
=={{header|Phix}}==
<!--<
<span style="color: #008080;">function</span> <span style="color: #000000;">pn3p2</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">n3p2</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">)+</span><span style="color: #000000;">2</span>
Line 1,107 ⟶ 1,381:
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Found %d primes of the form n^3+2:\n"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">))</span>
<span style="color: #7060A8;">papply</span><span style="color: #0000FF;">(</span><span style="color: #004600;">true</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">printf</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,{</span><span style="color: #008000;">"n = %3d => n^3+2 = %,9d\n"</span><span style="color: #0000FF;">},</span><span style="color: #000000;">res</span><span style="color: #0000FF;">})</span>
<!--</
{{out}}
<pre>
Line 1,133 ⟶ 1,407:
=={{header|Plain English}}==
<
Start up.
Put 1 into a counter.
Line 1,146 ⟶ 1,420:
Write "Done." on the console.
Wait for the escape key.
Shut down.</
{{out}}
<pre>
Line 1,169 ⟶ 1,443:
189 6751271
Done.
</pre>
=={{header|Python}}==
<syntaxhighlight lang="python">#!/usr/bin/python
def isPrime(n):
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
return False
return True
if __name__ == '__main__':
for n in range(1, 200):
if isPrime(n**3+2):
print(f'{n}\t{n**3+2}');</syntaxhighlight>
{{out}}
<pre>1 3
3 29
5 127
29 24391
45 91127
63 250049
65 274627
69 328511
71 357913
83 571789
105 1157627
113 1442899
123 1860869
129 2146691
143 2924209
153 3581579
171 5000213
173 5177719
189 6751271</pre>
=={{header|Quackery}}==
<code>prime</code> is defined at [[Miller–Rabin primality test#Quackery]].
<syntaxhighlight lang="Quackery"> [ dip number$
over size -
space swap of
swap join echo$ ] is recho ( n n --> )
199 times
[ i^ 1+ 3 ** 2 +
dup prime iff
[ i^ 1+ 4 recho
sp 7 recho cr ]
else drop ]</syntaxhighlight>
{{out}}
<pre> 1 3
3 29
5 127
29 24391
45 91127
63 250049
65 274627
69 328511
71 357913
83 571789
105 1157627
113 1442899
123 1860869
129 2146691
143 2924209
153 3581579
171 5000213
173 5177719
189 6751271
</pre>
=={{header|Raku}}==
<syntaxhighlight lang="raku"
say ((1…199)»³ »+»2).grep: *.is-prime</
{{out}}
<pre>
Line 1,189 ⟶ 1,537:
Since the task's requirements are pretty straight-forward and easy, a little extra code was added for presentation
<br>(title and title separator line, the count of primes found, and commatization of the numbers).
<
parse arg LO HI hp . /*obtain optional argument from the CL.*/
if LO=='' | LO=="," then LO= 0 /*Not specified? Then use the default.*/
Line 1,236 ⟶ 1,584:
end /*k*/ /* [↑] only process numbers ≤ √ J */
#= #+1; @.#= j; s.#= j*j; !.j= 1 /*bump # of Ps; assign next P; P²; P# */
end /*j*/; return</
{{out|output|text= when using the default inputs:}}
<pre>
Line 1,266 ⟶ 1,614:
=={{header|Ring}}==
<
load "stdlib.ring"
Line 1,279 ⟶ 1,627:
see "done..." + nl
</syntaxhighlight>
{{out}}
<pre>
Line 1,305 ⟶ 1,653:
</pre>
=={{header|RPL}}==
{{works with|HP|49g}}
≪ { }
1 200 '''FOR''' n
n 3 ^ 2 +
'''IF''' DUP ISPRIME? '''THEN''' + '''ELSE''' DROP '''END'''
'''NEXT'''
≫ '<span style="color:blue">TASK</span>' STO
{{out}}
<pre>
1: {3 29 127 24391 91127 250049 274627 328511 357913 571789 1157627 1442899 1860869 2146691 2924209 3581579 5000213 5177719 6751271}
</pre>
=={{header|Ruby}}==
<syntaxhighlight lang="ruby">require 'prime'
p (1..200).filter_map{|n| cand = n**3 + 2; cand if cand.prime? }
</syntaxhighlight>
{{out}}
<pre>[3, 29, 127, 24391, 91127, 250049, 274627, 328511, 357913, 571789, 1157627, 1442899, 1860869, 2146691, 2924209, 3581579, 5000213, 5177719, 6751271]</pre>
=={{header|Rust}}==
<
use primes::is_prime;
Line 1,329 ⟶ 1,697:
println!("Found {} such prime numbers where {} < n < {}.", count,begin,end);
}</
{{out}}
<pre>
Line 1,358 ⟶ 1,726:
=={{header|Seed7}}==
Credit for <code>isPrime</code> function: [http://seed7.sourceforge.net/algorith/math.htm#isPrime]
<
const func boolean: isPrime (in integer: number) is func
Line 1,392 ⟶ 1,760:
end if;
end for;
end func;</
{{out}}
<pre>
Line 1,419 ⟶ 1,787:
=={{header|Sidef}}==
<
{{out}}
<pre>
Line 1,426 ⟶ 1,794:
=={{header|Swift}}==
<
func isPrime(_ n: Int) -> Bool {
Line 1,457 ⟶ 1,825:
print(String(format: "%3d%9d", n, p))
}
}</
{{out}}
Line 1,484 ⟶ 1,852:
=={{header|Wren}}==
{{libheader|Wren-math}}
{{libheader|Wren-
{{libheader|Wren-fmt}}
If ''n'' is even then ''n³ + 2'' is also even, so we only need to examine odd values of ''n'' here.
<
import "./
import "./fmt" for Fmt
var limit = 200
Line 1,495 ⟶ 1,863:
var p = n*n*n + 2
if (Int.isPrime(p)) Fmt.print("n = $3d => n³ + 2 = $,9d", n, p)
}</
{{out}}
Line 1,521 ⟶ 1,889:
=={{header|XPL0}}==
<
int N, I;
[if N <= 1 then return false;
Line 1,538 ⟶ 1,906:
]
];
]</
{{out}}
|