Find prime numbers of the form n*n*n+2: Difference between revisions
m (Ada: Fix Is_Prime) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 5: | Line 5: | ||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
< |
<syntaxhighlight lang="ada">with Ada.Text_Io; |
||
procedure Find_Primes is |
procedure Find_Primes is |
||
Line 45: | Line 45: | ||
end if; |
end if; |
||
end loop; |
end loop; |
||
end Find_Primes;</ |
end Find_Primes;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> N N**3+2 |
<pre> N N**3+2 |
||
Line 70: | Line 70: | ||
=={{header|ALGOL 68}}== |
=={{header|ALGOL 68}}== |
||
< |
<syntaxhighlight lang="algol68">BEGIN # Find n such that n^3 + 2 is a prime for n < 200 # |
||
FOR n TO 199 DO |
FOR n TO 199 DO |
||
INT candidate = ( n * n * n ) + 2; |
INT candidate = ( n * n * n ) + 2; |
||
Line 84: | Line 84: | ||
FI |
FI |
||
OD |
OD |
||
END</ |
END</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 109: | Line 109: | ||
=={{header|ALGOL W}}== |
=={{header|ALGOL W}}== |
||
< |
<syntaxhighlight lang="algolw">begin % Find n such that n^3 + 2 is a prime for n < 200 % |
||
for n := 1 until 199 do begin |
for n := 1 until 199 do begin |
||
integer candidate; |
integer candidate; |
||
Line 127: | Line 127: | ||
end if_isPrime |
end if_isPrime |
||
end for_n |
end for_n |
||
end.</ |
end.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 152: | Line 152: | ||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
<syntaxhighlight lang="awk"> |
|||
<lang AWK> |
|||
# syntax: GAWK -f FIND_PRIME_NUMBERS_OF_THE_FORM_NNN2.AWK |
# syntax: GAWK -f FIND_PRIME_NUMBERS_OF_THE_FORM_NNN2.AWK |
||
BEGIN { |
BEGIN { |
||
Line 178: | Line 178: | ||
return(1) |
return(1) |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 205: | Line 205: | ||
=={{header|C}}== |
=={{header|C}}== |
||
{{trans|Wren}} |
{{trans|Wren}} |
||
< |
<syntaxhighlight lang="c">#include <stdio.h> |
||
#include <stdbool.h> |
#include <stdbool.h> |
||
#include <locale.h> |
#include <locale.h> |
||
Line 235: | Line 235: | ||
} |
} |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 261: | Line 261: | ||
=={{header|C++}}== |
=={{header|C++}}== |
||
< |
<syntaxhighlight lang="cpp">#include <iomanip> |
||
#include <iostream> |
#include <iostream> |
||
Line 287: | Line 287: | ||
std::cout << std::setw(3) << n << std::setw(9) << p << '\n'; |
std::cout << std::setw(3) << n << std::setw(9) << p << '\n'; |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 313: | Line 313: | ||
=={{header|CLU}}== |
=={{header|CLU}}== |
||
< |
<syntaxhighlight lang="clu">is_prime = proc (n: int) returns (bool) |
||
if n<2 then return(false) end |
if n<2 then return(false) end |
||
if n//2=0 then return(n=2) end |
if n//2=0 then return(n=2) end |
||
Line 344: | Line 344: | ||
stream$putl(po, "") |
stream$putl(po, "") |
||
end |
end |
||
end start_up</ |
end start_up</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>n = 1 => n^3 + 2 = 3 |
<pre>n = 1 => n^3 + 2 = 3 |
||
Line 367: | Line 367: | ||
=={{header|COBOL}}== |
=={{header|COBOL}}== |
||
< |
<syntaxhighlight lang="cobol"> IDENTIFICATION DIVISION. |
||
PROGRAM-ID. N3-PLUS-2-PRIMES. |
PROGRAM-ID. N3-PLUS-2-PRIMES. |
||
Line 437: | Line 437: | ||
CHECK-PRIME-DONE. |
CHECK-PRIME-DONE. |
||
EXIT.</ |
EXIT.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>N = 1 => N ** 3 + 2 = 3 |
<pre>N = 1 => N ** 3 + 2 = 3 |
||
Line 460: | Line 460: | ||
=={{header|Cowgol}}== |
=={{header|Cowgol}}== |
||
< |
<syntaxhighlight lang="cowgol">include "cowgol.coh"; |
||
sub is_prime(n: uint32): (p: uint8) is |
sub is_prime(n: uint32): (p: uint8) is |
||
Line 495: | Line 495: | ||
end if; |
end if; |
||
n := n+1; |
n := n+1; |
||
end loop;</ |
end loop;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>n = 1 => n^3 + 2 = 3 |
<pre>n = 1 => n^3 + 2 = 3 |
||
Line 520: | Line 520: | ||
{{libheader| System.SysUtils}} |
{{libheader| System.SysUtils}} |
||
{{libheader| PrimTrial}} |
{{libheader| PrimTrial}} |
||
<syntaxhighlight lang="delphi"> |
|||
<lang Delphi> |
|||
program Find_prime_numbers_of_the_form_n_n_n_plus_2; |
program Find_prime_numbers_of_the_form_n_n_n_plus_2; |
||
Line 548: | Line 548: | ||
end; |
end; |
||
readln; |
readln; |
||
end.</ |
end.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>n = 1 => n^3 + 2 = 3 |
<pre>n = 1 => n^3 + 2 = 3 |
||
Line 572: | Line 572: | ||
=={{header|F_Sharp|F#}}== |
=={{header|F_Sharp|F#}}== |
||
This task uses [[Extensible_prime_generator#The_functions|Extensible Prime Generator (F#)]].<br> |
This task uses [[Extensible_prime_generator#The_functions|Extensible Prime Generator (F#)]].<br> |
||
< |
<syntaxhighlight lang="fsharp"> |
||
[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)) |
[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> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 600: | Line 600: | ||
Using the parity optimization from the Wren entry: |
Using the parity optimization from the Wren entry: |
||
{{works with|Factor|0.99 2021-02-05}} |
{{works with|Factor|0.99 2021-02-05}} |
||
< |
<syntaxhighlight lang="factor">USING: formatting kernel math math.functions math.primes |
||
math.ranges sequences tools.memory.private ; |
math.ranges sequences tools.memory.private ; |
||
Line 606: | Line 606: | ||
dup 3 ^ 2 + dup prime? |
dup 3 ^ 2 + dup prime? |
||
[ commas "n = %3d => n³ + 2 = %9s\n" printf ] [ 2drop ] if |
[ commas "n = %3d => n³ + 2 = %9s\n" printf ] [ 2drop ] if |
||
] each</ |
] each</syntaxhighlight> |
||
Or, using local variables: |
Or, using local variables: |
||
{{trans|Wren}} |
{{trans|Wren}} |
||
{{works with|Factor|0.99 2021-02-05}} |
{{works with|Factor|0.99 2021-02-05}} |
||
< |
<syntaxhighlight lang="factor">USING: formatting kernel math math.primes math.ranges sequences |
||
tools.memory.private ; |
tools.memory.private ; |
||
Line 620: | Line 620: | ||
[ n p commas "n = %3d => n³ + 2 = %9s\n" printf ] when |
[ n p commas "n = %3d => n³ + 2 = %9s\n" printf ] when |
||
] each |
] each |
||
]</ |
]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 645: | Line 645: | ||
=={{header|Fermat}}== |
=={{header|Fermat}}== |
||
< |
<syntaxhighlight lang="fermat">for n=1,199 do if Isprime(n^3+2)=1 then !!(n,n^3+2) fi od</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 671: | Line 671: | ||
=={{header|Forth}}== |
=={{header|Forth}}== |
||
{{works with|Gforth}} |
{{works with|Gforth}} |
||
< |
<syntaxhighlight lang="forth">: prime? ( n -- flag ) |
||
dup 2 < if drop false exit then |
dup 2 < if drop false exit then |
||
dup 2 mod 0= if 2 = exit then |
dup 2 mod 0= if 2 = exit then |
||
Line 696: | Line 696: | ||
main |
main |
||
bye</ |
bye</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 723: | Line 723: | ||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
Use the code from [[Primality by trial division#FreeBASIC]] as an include. |
Use the code from [[Primality by trial division#FreeBASIC]] as an include. |
||
< |
<syntaxhighlight lang="freebasic">#include"isprime.bas" |
||
for n as uinteger = 1 to 200 |
for n as uinteger = 1 to 200 |
||
Line 729: | Line 729: | ||
print n, n^3+2 |
print n, n^3+2 |
||
end if |
end if |
||
next n</ |
next n</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 754: | Line 754: | ||
=={{header|Frink}}== |
=={{header|Frink}}== |
||
< |
<syntaxhighlight lang="frink">for n = 1 to 199 |
||
if isPrime[n^3 + 2] |
if isPrime[n^3 + 2] |
||
println["$n\t" + n^3+2]</ |
println["$n\t" + n^3+2]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 789: | Line 789: | ||
=={{header|Go}}== |
=={{header|Go}}== |
||
< |
<syntaxhighlight lang="go">package main |
||
import "fmt" |
import "fmt" |
||
Line 840: | Line 840: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 866: | Line 866: | ||
=={{header|J}}== |
=={{header|J}}== |
||
< |
<syntaxhighlight lang="j">([,.2+]^3:)@([#~1:p:2+]^3:) }.i.200x</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> 1 3 |
<pre> 1 3 |
||
Line 894: | Line 894: | ||
Using a definition of `is_prime` such as can be found at |
Using a definition of `is_prime` such as can be found at |
||
[[Safe_primes_and_unsafe_primes]]: |
[[Safe_primes_and_unsafe_primes]]: |
||
<syntaxhighlight lang="jq"> |
|||
<lang jq> |
|||
range(1;200) | pow(.; 3) + 2 | select(is_prime) |
range(1;200) | pow(.; 3) + 2 | select(is_prime) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 921: | Line 921: | ||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
< |
<syntaxhighlight lang="julia"># Formatting output as in Go example. |
||
using Primes, Formatting |
using Primes, Formatting |
||
Line 942: | Line 942: | ||
filterprintresults(isncubedplus2prime, 0, 200, tostring) |
filterprintresults(isncubedplus2prime, 0, 200, tostring) |
||
</ |
</syntaxhighlight>{{out}} |
||
<pre> |
<pre> |
||
n = 1 => n³ + 2 = 3 |
n = 1 => n³ + 2 = 3 |
||
Line 967: | Line 967: | ||
</pre> |
</pre> |
||
=== One-liner version === |
=== One-liner version === |
||
< |
<syntaxhighlight lang="julia">using Primes; println(filter(isprime, map(x -> x^3 + 2, 1:199)))</syntaxhighlight>{{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|MAD}}== |
=={{header|MAD}}== |
||
< |
<syntaxhighlight lang="mad"> NORMAL MODE IS INTEGER |
||
INTERNAL FUNCTION(P) |
INTERNAL FUNCTION(P) |
||
Line 997: | Line 997: | ||
END OF CONDITIONAL |
END OF CONDITIONAL |
||
LOOP CONTINUE |
LOOP CONTINUE |
||
END OF PROGRAM</ |
END OF PROGRAM</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>N = 1 N*N*N + 2 = 3 |
<pre>N = 1 N*N*N + 2 = 3 |
||
Line 1,020: | Line 1,020: | ||
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
||
< |
<syntaxhighlight lang="mathematica">Select[Range[199]^3 + 2, PrimeQ]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>{3, 29, 127, 24391, 91127, 250049, 274627, 328511, 357913, 571789, 1157627, 1442899, 1860869, 2146691, 2924209, 3581579, 5000213, 5177719, 6751271}</pre> |
<pre>{3, 29, 127, 24391, 91127, 250049, 274627, 328511, 357913, 571789, 1157627, 1442899, 1860869, 2146691, 2924209, 3581579, 5000213, 5177719, 6751271}</pre> |
||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
< |
<syntaxhighlight lang="nim">import strutils |
||
func isPrime(n: Positive): bool = |
func isPrime(n: Positive): bool = |
||
Line 1,042: | Line 1,042: | ||
let p = n * n * n + 2 |
let p = n * n * n + 2 |
||
if p.isPrime: |
if p.isPrime: |
||
echo ($n).align(3), " → ", p</ |
echo ($n).align(3), " → ", p</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,066: | Line 1,066: | ||
=={{header|PARI/GP}}== |
=={{header|PARI/GP}}== |
||
< |
<syntaxhighlight lang="parigp">for(N=1,200,if(isprime(N^3+2),print(N," ",N^3+2)))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,091: | Line 1,091: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
< |
<syntaxhighlight lang="perl">use strict; |
||
use warnings; |
use warnings; |
||
use feature 'say'; |
use feature 'say'; |
||
Line 1,107: | Line 1,107: | ||
} |
} |
||
print "\n"; |
print "\n"; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,126: | Line 1,126: | ||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
<!--< |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<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: #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> |
<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,134: | Line 1,134: | ||
<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;">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> |
<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> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,160: | Line 1,160: | ||
=={{header|Plain English}}== |
=={{header|Plain English}}== |
||
< |
<syntaxhighlight lang="plainenglish">To run: |
||
Start up. |
Start up. |
||
Put 1 into a counter. |
Put 1 into a counter. |
||
Line 1,173: | Line 1,173: | ||
Write "Done." on the console. |
Write "Done." on the console. |
||
Wait for the escape key. |
Wait for the escape key. |
||
Shut down.</ |
Shut down.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,200: | Line 1,200: | ||
=={{header|Python}}== |
=={{header|Python}}== |
||
< |
<syntaxhighlight lang="python">#!/usr/bin/python |
||
def isPrime(n): |
def isPrime(n): |
||
Line 1,211: | Line 1,211: | ||
for n in range(1, 200): |
for n in range(1, 200): |
||
if isPrime(n**3+2): |
if isPrime(n**3+2): |
||
print(f'{n}\t{n**3+2}');</ |
print(f'{n}\t{n**3+2}');</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>1 3 |
<pre>1 3 |
||
Line 1,235: | Line 1,235: | ||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
<lang |
<syntaxhighlight lang="raku" line># 20210315 Raku programming solution |
||
say ((1…199)»³ »+»2).grep: *.is-prime</ |
say ((1…199)»³ »+»2).grep: *.is-prime</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,252: | Line 1,252: | ||
Since the task's requirements are pretty straight-forward and easy, a little extra code was added for presentation |
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). |
<br>(title and title separator line, the count of primes found, and commatization of the numbers). |
||
< |
<syntaxhighlight lang="rexx">/*REXX program finds and displays n primes of the form: n**3 + 2. */ |
||
parse arg LO HI hp . /*obtain optional argument from the CL.*/ |
parse arg LO HI hp . /*obtain optional argument from the CL.*/ |
||
if LO=='' | LO=="," then LO= 0 /*Not specified? Then use the default.*/ |
if LO=='' | LO=="," then LO= 0 /*Not specified? Then use the default.*/ |
||
Line 1,299: | Line 1,299: | ||
end /*k*/ /* [↑] only process numbers ≤ √ J */ |
end /*k*/ /* [↑] only process numbers ≤ √ J */ |
||
#= #+1; @.#= j; s.#= j*j; !.j= 1 /*bump # of Ps; assign next P; P²; P# */ |
#= #+1; @.#= j; s.#= j*j; !.j= 1 /*bump # of Ps; assign next P; P²; P# */ |
||
end /*j*/; return</ |
end /*j*/; return</syntaxhighlight> |
||
{{out|output|text= when using the default inputs:}} |
{{out|output|text= when using the default inputs:}} |
||
<pre> |
<pre> |
||
Line 1,329: | Line 1,329: | ||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
< |
<syntaxhighlight lang="ring"> |
||
load "stdlib.ring" |
load "stdlib.ring" |
||
Line 1,342: | Line 1,342: | ||
see "done..." + nl |
see "done..." + nl |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,370: | Line 1,370: | ||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
< |
<syntaxhighlight lang="rust">// 202100327 Rust programming solution |
||
use primes::is_prime; |
use primes::is_prime; |
||
Line 1,392: | Line 1,392: | ||
println!("Found {} such prime numbers where {} < n < {}.", count,begin,end); |
println!("Found {} such prime numbers where {} < n < {}.", count,begin,end); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,421: | Line 1,421: | ||
=={{header|Seed7}}== |
=={{header|Seed7}}== |
||
Credit for <code>isPrime</code> function: [http://seed7.sourceforge.net/algorith/math.htm#isPrime] |
Credit for <code>isPrime</code> function: [http://seed7.sourceforge.net/algorith/math.htm#isPrime] |
||
< |
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i"; |
||
const func boolean: isPrime (in integer: number) is func |
const func boolean: isPrime (in integer: number) is func |
||
Line 1,455: | Line 1,455: | ||
end if; |
end if; |
||
end for; |
end for; |
||
end func;</ |
end func;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,482: | Line 1,482: | ||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
< |
<syntaxhighlight lang="ruby">1..^200 -> map { _**3 + 2 }.grep {.is_prime}.say</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,489: | Line 1,489: | ||
=={{header|Swift}}== |
=={{header|Swift}}== |
||
< |
<syntaxhighlight lang="swift">import Foundation |
||
func isPrime(_ n: Int) -> Bool { |
func isPrime(_ n: Int) -> Bool { |
||
Line 1,520: | Line 1,520: | ||
print(String(format: "%3d%9d", n, p)) |
print(String(format: "%3d%9d", n, p)) |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,550: | Line 1,550: | ||
{{libheader|Wren-fmt}} |
{{libheader|Wren-fmt}} |
||
If ''n'' is even then ''n³ + 2'' is also even, so we only need to examine odd values of ''n'' here. |
If ''n'' is even then ''n³ + 2'' is also even, so we only need to examine odd values of ''n'' here. |
||
< |
<syntaxhighlight lang="ecmascript">import "/math" for Int |
||
import "/trait" for Stepped |
import "/trait" for Stepped |
||
import "/fmt" for Fmt |
import "/fmt" for Fmt |
||
Line 1,558: | Line 1,558: | ||
var p = n*n*n + 2 |
var p = n*n*n + 2 |
||
if (Int.isPrime(p)) Fmt.print("n = $3d => n³ + 2 = $,9d", n, p) |
if (Int.isPrime(p)) Fmt.print("n = $3d => n³ + 2 = $,9d", n, p) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,584: | Line 1,584: | ||
=={{header|XPL0}}== |
=={{header|XPL0}}== |
||
< |
<syntaxhighlight lang="xpl0">func IsPrime(N); \Return 'true' if N is a prime number |
||
int N, I; |
int N, I; |
||
[if N <= 1 then return false; |
[if N <= 1 then return false; |
||
Line 1,601: | Line 1,601: | ||
] |
] |
||
]; |
]; |
||
]</ |
]</syntaxhighlight> |
||
{{out}} |
{{out}} |
Revision as of 12:58, 27 August 2022
- Task
- Find prime numbers of the form n3+2, where 0 < n < 200
Ada
with Ada.Text_Io;
procedure Find_Primes is
type Number is new Long_Integer range 0 .. Long_Integer'Last;
package Number_Io is new Ada.Text_Io.Integer_Io (Number);
function Is_Prime (A : Number) return Boolean is
D : Number;
begin
if A < 2 then return False; end if;
if A in 2 .. 3 then return True; end if;
if A mod 2 = 0 then return False; end if;
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;
end if;
D := D + 2;
if A mod D = 0 then
return False;
end if;
D := D + 4;
end loop;
return True;
end Is_Prime;
P : Number;
begin
Ada.Text_Io.Put_Line (" N N**3+2");
Ada.Text_Io.Put_Line ("------------");
for N in Number range 1 .. 199 loop
P := N**3 + 2;
if Is_Prime (P) then
Number_Io.Put (N, Width => 3); Ada.Text_Io.Put (" ");
Number_Io.Put (P, Width => 7);
Ada.Text_Io.New_Line;
end if;
end loop;
end Find_Primes;
- Output:
N N**3+2 ------------ 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
ALGOL 68
BEGIN # Find n such that n^3 + 2 is a prime for n < 200 #
FOR n TO 199 DO
INT candidate = ( n * n * n ) + 2;
# there will only be 199 candidates, so a primality check by trial #
# division should be OK #
BOOL is prime := TRUE;
FOR f FROM 2 TO ENTIER sqrt( candidate )
WHILE is prime := candidate MOD f /= 0
DO SKIP OD;
IF is prime THEN
# n^3 + 2 is prime #
print( ( whole( n, -4 ), ": ", whole( candidate, -8 ), newline ) )
FI
OD
END
- Output:
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
ALGOL W
begin % Find n such that n^3 + 2 is a prime for n < 200 %
for n := 1 until 199 do begin
integer candidate;
logical isPrime;
candidate := ( n * n * n ) + 2;
% there will only be 199 candidates, so a primality check by trial %
% division should be OK %
isPrime := true;
for f := 2 until entier( sqrt( candidate ) ) do begin
isPrime := candidate rem f not = 0;
if not isPrime then goto endPrimalityCheck
end for_f ;
endPrimalityCheck:
if isPrime then begin
% n^3 + 2 is prime %
write( i_w := 4, s_w := 0, n, ": ", i_w := 8, candidate )
end if_isPrime
end for_n
end.
- Output:
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
AWK
# syntax: GAWK -f FIND_PRIME_NUMBERS_OF_THE_FORM_NNN2.AWK
BEGIN {
start = 1
stop = 200
for (n=start; n<=stop; n++) {
p = n*n*n + 2
if (is_prime(p)) {
printf("%3d %'10d\n",n,p)
count++
}
}
printf("Prime numbers %d-%d of the form n*n*n+2: %d\n",start,stop,count)
exit(0)
}
function is_prime(x, i) {
if (x <= 1) {
return(0)
}
for (i=2; i<=int(sqrt(x)); i++) {
if (x % i == 0) {
return(0)
}
}
return(1)
}
- Output:
1 3 3 29 5 127 29 24,391 45 91,127 63 250,049 65 274,627 69 328,511 71 357,913 83 571,789 105 1,157,627 113 1,442,899 123 1,860,869 129 2,146,691 143 2,924,209 153 3,581,579 171 5,000,213 173 5,177,719 189 6,751,271 Prime numbers 1-200 of the form n*n*n+2: 19
C
#include <stdio.h>
#include <stdbool.h>
#include <locale.h>
bool isPrime(int n) {
int d;
if (n < 2) return false;
if (!(n%2)) return n == 2;
if (!(n%3)) return n == 3;
d = 5;
while (d*d <= n) {
if (!(n%d)) return false;
d += 2;
if (!(n%d)) return false;
d += 4;
}
return true;
}
int main() {
int n, p;
const int limit = 200;
setlocale(LC_ALL, "");
for (n = 1; n < limit; ++n) {
p = n*n*n + 2;
if (isPrime(p)) {
printf("n = %3d => n³ + 2 = %'9d\n", n, p);
}
}
return 0;
}
- Output:
n = 1 => n³ + 2 = 3 n = 3 => n³ + 2 = 29 n = 5 => n³ + 2 = 127 n = 29 => n³ + 2 = 24,391 n = 45 => n³ + 2 = 91,127 n = 63 => n³ + 2 = 250,049 n = 65 => n³ + 2 = 274,627 n = 69 => n³ + 2 = 328,511 n = 71 => n³ + 2 = 357,913 n = 83 => n³ + 2 = 571,789 n = 105 => n³ + 2 = 1,157,627 n = 113 => n³ + 2 = 1,442,899 n = 123 => n³ + 2 = 1,860,869 n = 129 => n³ + 2 = 2,146,691 n = 143 => n³ + 2 = 2,924,209 n = 153 => n³ + 2 = 3,581,579 n = 171 => n³ + 2 = 5,000,213 n = 173 => n³ + 2 = 5,177,719 n = 189 => n³ + 2 = 6,751,271
C++
#include <iomanip>
#include <iostream>
bool is_prime(unsigned int n) {
if (n < 2)
return false;
if (n % 2 == 0)
return n == 2;
if (n % 3 == 0)
return n == 3;
for (unsigned int p = 5; p * p <= n; p += 4) {
if (n % p == 0)
return false;
p += 2;
if (n % p == 0)
return false;
}
return true;
}
int main() {
for (unsigned int n = 1; n < 200; n += 2) {
auto p = n * n * n + 2;
if (is_prime(p))
std::cout << std::setw(3) << n << std::setw(9) << p << '\n';
}
}
- Output:
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
CLU
is_prime = proc (n: int) returns (bool)
if n<2 then return(false) end
if n//2=0 then return(n=2) end
if n//3=0 then return(n=3) end
d: int := 5
while d*d <= n do
if n//d=0 then return(false) end
d := d+2
if n//d=0 then return(false) end
d := d+4
end
return(true)
end is_prime
n3plus2_primes = iter (max: int) yields (int,int)
for n: int in int$from_to(1, max) do
p: int := n**3 + 2
if is_prime(p) then yield(n,p) end
end
end n3plus2_primes
start_up = proc ()
po: stream := stream$primary_output()
for n, p: int in n3plus2_primes(200) do
stream$puts(po, "n = ")
stream$putright(po, int$unparse(n), 3)
stream$puts(po, " => n^3 + 2 = ")
stream$putright(po, int$unparse(p), 7)
stream$putl(po, "")
end
end start_up
- Output:
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
COBOL
IDENTIFICATION DIVISION.
PROGRAM-ID. N3-PLUS-2-PRIMES.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 VARIABLES.
03 N PIC 9(3).
03 N3PLUS2 PIC 9(7).
03 DIVISOR PIC 9(4).
03 DIV-SQ PIC 9(8).
03 DIV-CHECK PIC 9(4)V9(4).
03 FILLER REDEFINES DIV-CHECK.
05 FILLER PIC 9(4).
05 FILLER PIC 9(4).
88 DIVISIBLE VALUE ZERO.
03 FILLER REDEFINES N3PLUS2.
05 FILLER PIC 9(6).
05 FILLER PIC 9.
88 EVEN VALUE 0, 2, 4, 6, 8.
03 PRIME-FLAG PIC X.
88 PRIME VALUE '*'.
01 FORMAT.
03 FILLER PIC X(4) VALUE "N = ".
03 N-OUT PIC ZZ9.
03 FILLER PIC X(17) VALUE " => N ** 3 + 2 = ".
03 N3PLUS2-OUT PIC Z(6)9.
PROCEDURE DIVISION.
BEGIN.
PERFORM TRY-N VARYING N FROM 1 BY 1
UNTIL N IS GREATER THAN 200.
STOP RUN.
TRY-N.
COMPUTE N3PLUS2 = N ** 3 + 2.
PERFORM CHECK-PRIME.
IF PRIME,
MOVE N TO N-OUT,
MOVE N3PLUS2 TO N3PLUS2-OUT,
DISPLAY FORMAT.
CHECK-PRIME SECTION.
BEGIN.
MOVE SPACE TO PRIME-FLAG.
IF N3PLUS2 IS LESS THAN 5, GO TO TRIVIAL.
IF EVEN, GO TO CHECK-PRIME-DONE.
DIVIDE N3PLUS2 BY 3 GIVING DIV-CHECK.
IF DIVISIBLE, GO TO CHECK-PRIME-DONE.
MOVE ZERO TO DIV-SQ.
MOVE 5 TO DIVISOR.
MOVE '*' TO PRIME-FLAG.
PERFORM CHECK-DIVISOR
UNTIL NOT PRIME OR DIV-SQ IS GREATER THAN N3PLUS2.
GO TO CHECK-PRIME-DONE.
CHECK-DIVISOR.
MULTIPLY DIVISOR BY DIVISOR GIVING DIV-SQ.
DIVIDE N3PLUS2 BY DIVISOR GIVING DIV-CHECK.
IF DIVISIBLE, MOVE SPACE TO PRIME-FLAG.
ADD 2 TO DIVISOR.
DIVIDE N3PLUS2 BY DIVISOR GIVING DIV-CHECK.
IF DIVISIBLE, MOVE SPACE TO PRIME-FLAG.
ADD 4 TO DIVISOR.
TRIVIAL.
IF N3PLUS2 IS EQUAL TO 2 OR EQUAL TO 3,
MOVE '*' TO PRIME-FLAG.
CHECK-PRIME-DONE.
EXIT.
- Output:
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
Cowgol
include "cowgol.coh";
sub is_prime(n: uint32): (p: uint8) is
p := 0;
if n<=4 then
if n==2 or n==3 then
p := 1;
end if;
return;
end if;
if n&1 == 0 or n%3 == 0 then
return;
end if;
var d: uint32 := 5;
while d*d <= n loop
if n%d==0 then return; end if;
d := d+2;
if n%d==0 then return; end if;
d := d+4;
end loop;
p := 1;
end sub;
var n: uint32 := 1;
while n < 200 loop
var p: uint32 := n*n*n + 2;
if is_prime(p) != 0 then
print("n = ");
print_i32(n);
print("\t=> n^3 + 2 = ");
print_i32(p);
print_nl();
end if;
n := n+1;
end loop;
- Output:
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
Delphi
program Find_prime_numbers_of_the_form_n_n_n_plus_2;
{$APPTYPE CONSOLE}
uses
System.SysUtils,
PrimTrial;
function Commatize(n: NativeInt): string;
var
fmt: TFormatSettings;
begin
fmt := TFormatSettings.Create('en-Us');
Result := double(n).ToString(ffNumber, 64, 0, fmt);
end;
const
limit = 200;
begin
for var n := 1 to limit - 1 do
begin
var p := n * n * n + 2;
if isPrime(p) then
writeln('n = ', n: 3, ' => n^3 + 2 = ', Commatize(p): 9);
end;
readln;
end.
- Output:
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
F#
This task uses Extensible Prime Generator (F#).
[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))
- Output:
n= 1 -> 3 n= 3 -> 29 n= 5 -> 127 n= 29 -> 24391 n= 45 -> 91127 n= 63 -> 250049 n= 65 -> 274627 n= 69 -> 328511 n= 71 -> 357913 n= 83 -> 571789 n=105 -> 1157627 n=113 -> 1442899 n=123 -> 1860869 n=129 -> 2146691 n=143 -> 2924209 n=153 -> 3581579 n=171 -> 5000213 n=173 -> 5177719 n=189 -> 6751271
Factor
Using the parity optimization from the Wren entry:
USING: formatting kernel math math.functions math.primes
math.ranges sequences tools.memory.private ;
1 199 2 <range> [
dup 3 ^ 2 + dup prime?
[ commas "n = %3d => n³ + 2 = %9s\n" printf ] [ 2drop ] if
] each
Or, using local variables:
USING: formatting kernel math math.primes math.ranges sequences
tools.memory.private ;
[let
199 :> limit
1 limit 2 <range> [| n |
n n n * * 2 + :> p
p prime?
[ n p commas "n = %3d => n³ + 2 = %9s\n" printf ] when
] each
]
- Output:
n = 1 => n³ + 2 = 3 n = 3 => n³ + 2 = 29 n = 5 => n³ + 2 = 127 n = 29 => n³ + 2 = 24,391 n = 45 => n³ + 2 = 91,127 n = 63 => n³ + 2 = 250,049 n = 65 => n³ + 2 = 274,627 n = 69 => n³ + 2 = 328,511 n = 71 => n³ + 2 = 357,913 n = 83 => n³ + 2 = 571,789 n = 105 => n³ + 2 = 1,157,627 n = 113 => n³ + 2 = 1,442,899 n = 123 => n³ + 2 = 1,860,869 n = 129 => n³ + 2 = 2,146,691 n = 143 => n³ + 2 = 2,924,209 n = 153 => n³ + 2 = 3,581,579 n = 171 => n³ + 2 = 5,000,213 n = 173 => n³ + 2 = 5,177,719 n = 189 => n³ + 2 = 6,751,271
Fermat
for n=1,199 do if Isprime(n^3+2)=1 then !!(n,n^3+2) fi od
- Output:
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
Forth
: prime? ( n -- flag )
dup 2 < if drop false exit then
dup 2 mod 0= if 2 = exit then
dup 3 mod 0= if 3 = exit then
5
begin
2dup dup * >=
while
2dup mod 0= if 2drop false exit then
2 +
2dup mod 0= if 2drop false exit then
4 +
repeat
2drop true ;
: main
200 1 do
i i i * * 2 + dup prime? if
i 3 .r 9 .r cr
else
drop
then
2 +loop ;
main
bye
- Output:
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
FreeBASIC
Use the code from Primality by trial division#FreeBASIC as an include.
#include"isprime.bas"
for n as uinteger = 1 to 200
if isprime(n^3+2) then
print n, n^3+2
end if
next n
- Output:
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
Frink
for n = 1 to 199
if isPrime[n^3 + 2]
println["$n\t" + n^3+2]
- Output:
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
Fōrmulæ
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text. Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation —i.e. XML, JSON— they are intended for storage and transfer purposes more than visualization and edition.
Programs in Fōrmulæ are created/edited online in its website, However they run on execution servers. By default remote servers are used, but they are limited in memory and processing power, since they are intended for demonstration and casual use. A local server can be downloaded and installed, it has no limitations (it runs in your own computer). Because of that, example programs can be fully visualized and edited, but some of them will not run if they require a moderate or heavy computation/memory resources, and no local server is being used.
In this page you can see the program(s) related to this task and their results.
Go
package main
import "fmt"
func isPrime(n int) bool {
switch {
case n < 2:
return false
case n%2 == 0:
return n == 2
case n%3 == 0:
return n == 3
default:
d := 5
for d*d <= n {
if n%d == 0 {
return false
}
d += 2
if n%d == 0 {
return false
}
d += 4
}
return true
}
}
func commatize(n int) string {
s := fmt.Sprintf("%d", n)
if n < 0 {
s = s[1:]
}
le := len(s)
for i := le - 3; i >= 1; i -= 3 {
s = s[0:i] + "," + s[i:]
}
if n >= 0 {
return s
}
return "-" + s
}
func main() {
const limit = 200
for n := 1; n < limit; n++ {
p := n*n*n + 2
if isPrime(p) {
fmt.Printf("n = %3d => n³ + 2 = %9s\n", n, commatize(p))
}
}
}
- Output:
n = 1 => n³ + 2 = 3 n = 3 => n³ + 2 = 29 n = 5 => n³ + 2 = 127 n = 29 => n³ + 2 = 24,391 n = 45 => n³ + 2 = 91,127 n = 63 => n³ + 2 = 250,049 n = 65 => n³ + 2 = 274,627 n = 69 => n³ + 2 = 328,511 n = 71 => n³ + 2 = 357,913 n = 83 => n³ + 2 = 571,789 n = 105 => n³ + 2 = 1,157,627 n = 113 => n³ + 2 = 1,442,899 n = 123 => n³ + 2 = 1,860,869 n = 129 => n³ + 2 = 2,146,691 n = 143 => n³ + 2 = 2,924,209 n = 153 => n³ + 2 = 3,581,579 n = 171 => n³ + 2 = 5,000,213 n = 173 => n³ + 2 = 5,177,719 n = 189 => n³ + 2 = 6,751,271
J
([,.2+]^3:)@([#~1:p:2+]^3:) }.i.200x
- Output:
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
jq
Works with gojq, the Go implementation of jq
Using a definition of `is_prime` such as can be found at Safe_primes_and_unsafe_primes:
range(1;200) | pow(.; 3) + 2 | select(is_prime)
- Output:
3 29 127 24391 91127 250049 274627 328511 357913 571789 1157627 1442899 1860869 2146691 2924209 3581579 5000213 5177719 6751271
Julia
# Formatting output as in Go example.
using Primes, Formatting
isncubedplus2prime(x) = begin fx = x * x * x + 2; (isprime(fx), fx) end
tostring(x, fx) = "n = " * lpad(x, 3) * " => n³ + 2 =" * lpad(format(fx, commas=true), 10)
function filterprintresults(x_to_bool_and_fx, start, stop, stringify=(x, fx)->"$x $fx", doprint=true)
ncount = 0
println("Filtering $x_to_bool_and_fx for integers between $start and $stop:\n")
for n in start+1:stop-1
isone, result = x_to_bool_and_fx(n)
if isone
doprint && println(stringify(n, result))
ncount += 1
end
end
println("\nThe total found was $ncount.")
end
filterprintresults(isncubedplus2prime, 0, 200, tostring)
- Output:
n = 1 => n³ + 2 = 3 n = 3 => n³ + 2 = 29 n = 5 => n³ + 2 = 127 n = 29 => n³ + 2 = 24,391 n = 45 => n³ + 2 = 91,127 n = 63 => n³ + 2 = 250,049 n = 65 => n³ + 2 = 274,627 n = 69 => n³ + 2 = 328,511 n = 71 => n³ + 2 = 357,913 n = 83 => n³ + 2 = 571,789 n = 105 => n³ + 2 = 1,157,627 n = 113 => n³ + 2 = 1,442,899 n = 123 => n³ + 2 = 1,860,869 n = 129 => n³ + 2 = 2,146,691 n = 143 => n³ + 2 = 2,924,209 n = 153 => n³ + 2 = 3,581,579 n = 171 => n³ + 2 = 5,000,213 n = 173 => n³ + 2 = 5,177,719 n = 189 => n³ + 2 = 6,751,271 The total found was 19.
One-liner version
using Primes; println(filter(isprime, map(x -> x^3 + 2, 1:199)))
- Output:
[3, 29, 127, 24391, 91127, 250049, 274627, 328511, 357913, 571789, 1157627, 1442899, 1860869, 2146691, 2924209, 3581579, 5000213, 5177719, 6751271]
MAD
NORMAL MODE IS INTEGER
INTERNAL FUNCTION(P)
ENTRY TO PRIME.
WHENEVER P.L.2, FUNCTION RETURN 0B
WHENEVER P.E.P/2*2, FUNCTION RETURN P.E.2
WHENEVER P.E.P/3*3, FUNCTION RETURN P.E.3
D = 5
CHKDIV WHENEVER D*D.LE.P
WHENEVER P.E.P/D*D, FUNCTION RETURN 0B
D = D+2
WHENEVER P.E.P/D*D, FUNCTION RETURN 0B
D = D+4
TRANSFER TO CHKDIV
END OF CONDITIONAL
FUNCTION RETURN 1B
END OF FUNCTION
VECTOR VALUES FMT = $4HN = ,I3,S4,12HN*N*N + 2 = ,I7*$
THROUGH LOOP, FOR N=1, 1, N.GE.200
M = N*N*N + 2
WHENEVER PRIME.(M)
PRINT FORMAT FMT,N,M
END OF CONDITIONAL
LOOP CONTINUE
END OF PROGRAM
- Output:
N = 1 N*N*N + 2 = 3 N = 3 N*N*N + 2 = 29 N = 5 N*N*N + 2 = 127 N = 29 N*N*N + 2 = 24391 N = 45 N*N*N + 2 = 91127 N = 63 N*N*N + 2 = 250049 N = 65 N*N*N + 2 = 274627 N = 69 N*N*N + 2 = 328511 N = 71 N*N*N + 2 = 357913 N = 83 N*N*N + 2 = 571789 N = 105 N*N*N + 2 = 1157627 N = 113 N*N*N + 2 = 1442899 N = 123 N*N*N + 2 = 1860869 N = 129 N*N*N + 2 = 2146691 N = 143 N*N*N + 2 = 2924209 N = 153 N*N*N + 2 = 3581579 N = 171 N*N*N + 2 = 5000213 N = 173 N*N*N + 2 = 5177719 N = 189 N*N*N + 2 = 6751271
Mathematica/Wolfram Language
Select[Range[199]^3 + 2, PrimeQ]
- Output:
{3, 29, 127, 24391, 91127, 250049, 274627, 328511, 357913, 571789, 1157627, 1442899, 1860869, 2146691, 2924209, 3581579, 5000213, 5177719, 6751271}
Nim
import strutils
func isPrime(n: Positive): bool =
if n < 2: return false
if n mod 2 == 0: return n == 2
if n mod 3 == 0: return n == 3
var d = 5
while d * d <= n:
if n mod d == 0: return false
inc d, 2
if n mod d == 0: return false
inc d, 4
result = true
for n in 1..<200:
let p = n * n * n + 2
if p.isPrime:
echo ($n).align(3), " → ", p
- Output:
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
PARI/GP
for(N=1,200,if(isprime(N^3+2),print(N," ",N^3+2)))
- Output:
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
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";
}
- Output:
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
Phix
function pn3p2(integer n) integer n3p2 = power(n,3)+2 return iff(is_prime(n3p2)?{n,n3p2}:0) end function sequence res = filter(apply(tagset(199,1,2),pn3p2),"!=",0) printf(1,"Found %d primes of the form n^3+2:\n",length(res)) papply(true,printf,{1,{"n = %3d => n^3+2 = %,9d\n"},res})
- Output:
Found 19 primes of the form n^3+2: 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
Plain English
To run:
Start up.
Put 1 into a counter.
Loop.
Put the counter into a number.
Raise the number to 3.
Add 2 to the number.
If the number is prime, write the counter then " " then the number on the console.
Add 2 to the counter.
If the counter is greater than 200, break.
Repeat.
Write "Done." on the console.
Wait for the escape key.
Shut down.
- Output:
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 Done.
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}');
- Output:
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
Raku
# 20210315 Raku programming solution
say ((1…199)»³ »+»2).grep: *.is-prime
- Output:
(3 29 127 24391 91127 250049 274627 328511 357913 571789 1157627 1442899 1860869 2146691 2924209 3581579 5000213 5177719 6751271)
REXX
Since REXX doesn't have a isPrime function, this REXX program generates a number of primes such that some
numbers can be tested for primality directly, other numbers have to be tested by trial division for primality.
A suitable number was calculated to generate a number of primes such that about half of the computing time used to
test the numbers for primality could be directly determined their primality, the other half of the computing time used
would use trial division.
Since the task's requirements are pretty straight-forward and easy, a little extra code was added for presentation
(title and title separator line, the count of primes found, and commatization of the numbers).
/*REXX program finds and displays n primes of the form: n**3 + 2. */
parse arg LO HI hp . /*obtain optional argument from the CL.*/
if LO=='' | LO=="," then LO= 0 /*Not specified? Then use the default.*/
if HI=='' | HI=="," then HI= 200 /* " " " " " " */
if hp=='' | hp=="," then hp= 19 /* " " " " " " */
h= max(iSqrt(HI**3), hp**3) /*a high prime to generate primes to. */
w= length( commas(HI**3) ) + 3
call genP /*build array of semaphores for primes.*/
say right('n', 20) ' (n**3 + 2)' /*display a title for the output list. */
say left('', 20 + w + 20, '─') /*display a sep for the output list. */
finds= 0 /*# of triplet strange primes (so far).*/
do j=LO+1 by 2 to HI-1 /*look for primes of form of: n**3 + 2 */
x= j**3 + 2
if x<=@.# then if \!.x then iterate /*Not a semaphore prime? Then skip it.*/
else nop /*the NOP matches up with the "THEN".*/
else do k=2 while @.k**2<=x /*perform a primality test by division.*/
if x//@.k==0 then iterate j
end /*k*/
finds= finds + 1 /*bump # primes found of form: n**3+2 */
say right(commas(j), 20) right( commas(x), w)
end /*j*/
say left('', 20 + w + 20, '─'); say /*display a sep for the output list. */
say 'Found ' commas(finds) ' primes in the form of: n**3 + 2'
exit 0 /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
commas: parse arg ?; do jc=length(?)-3 to 1 by -3; ?=insert(',', ?, jc); end; return ?
/*──────────────────────────────────────────────────────────────────────────────────────*/
iSqrt: procedure; parse arg x; r=0; q=1; do while q<=x; q=q*4; end
do while q>1; q=q%4; _=x-r-q; r=r%2; if _>=0 then do;x=_;r=r+q; end; end
return r
/*──────────────────────────────────────────────────────────────────────────────────────*/
genP: !.= 0; pad= left('', 9) /*placeholders for primes; width of #'s*/
@.1=2; @.2=3; @.3=5; @.4=7; @.5=11 /*define some low primes. */
!.2=1; !.3=1; !.5=1; !.7=1; !.11=1 /* " " " " flags. */
#=5; s.#= @.# **2 /*number of primes so far; prime². */
/* [↓] generate more primes ≤ h. */
do j=@.#+2 by 2 to h /*find odd primes from here on. */
parse var j '' -1 _; if _==5 then iterate /*J divisible by 5? (right dig)*/
if j// 3==0 then iterate /*" " " 3? */
if j// 7==0 then iterate /*" " " 7? */
/* [↑] the above five lines saves time*/
do k=5 while s.k<=j /* [↓] divide by the known odd primes.*/
if j // @.k == 0 then iterate j /*Is J ÷ X? Then not prime. ___ */
end /*k*/ /* [↑] only process numbers ≤ √ J */
#= #+1; @.#= j; s.#= j*j; !.j= 1 /*bump # of Ps; assign next P; P²; P# */
end /*j*/; return
- output when using the default inputs:
n (n**3 + 2) ──────────────────────────────────────────────────── 1 3 3 29 5 127 29 24,391 45 91,127 63 250,049 65 274,627 69 328,511 71 357,913 83 571,789 105 1,157,627 113 1,442,899 123 1,860,869 129 2,146,691 143 2,924,209 153 3,581,579 171 5,000,213 173 5,177,719 189 6,751,271 ──────────────────────────────────────────────────── Found 19 primes in the form of: n**3 + 2
Ring
load "stdlib.ring"
see "working..." + nl
for n = 1 to 200 step 2
pr = pow(n,3)+2
if isprime(pr)
see "n = " + n + " => n³+2 = " + pr + nl
ok
next
see "done..." + nl
- Output:
working... n = 1 => n³+2 = 3 n = 3 => n³+2 = 29 n = 5 => n³+2 = 127 n = 29 => n³+2 = 24391 n = 45 => n³+2 = 91127 n = 63 => n³+2 = 250049 n = 65 => n³+2 = 274627 n = 69 => n³+2 = 328511 n = 71 => n³+2 = 357913 n = 83 => n³+2 = 571789 n = 105 => n³+2 = 1157627 n = 113 => n³+2 = 1442899 n = 123 => n³+2 = 1860869 n = 129 => n³+2 = 2146691 n = 143 => n³+2 = 2924209 n = 153 => n³+2 = 3581579 n = 171 => n³+2 = 5000213 n = 173 => n³+2 = 5177719 n = 189 => n³+2 = 6751271 done...
Rust
// 202100327 Rust programming solution
use primes::is_prime;
fn main() {
let mut count = 0;
let begin = 0;
let end = 200;
println!("Find prime numbers of the form");
println!(" n => n³ + 2 ");
for n in begin+1..end-1 {
let m = n*n*n+2;
if is_prime(m) {
println!("{:4} => {}", n, m);
count += 1;
}
}
println!("Found {} such prime numbers where {} < n < {}.", count,begin,end);
}
- Output:
Find prime numbers of the form n => n³ + 2 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 Found 19 such prime numbers where 0 < n < 200.
Seed7
Credit for isPrime
function: [1]
$ include "seed7_05.s7i";
const func boolean: isPrime (in integer: number) is func
result
var boolean: prime is FALSE;
local
var integer: upTo is 0;
var integer: testNum is 3;
begin
if number = 2 then
prime := TRUE;
elsif odd(number) and number > 2 then
upTo := sqrt(number);
while number rem testNum <> 0 and testNum <= upTo do
testNum +:= 2;
end while;
prime := testNum > upTo;
end if;
end func;
const proc: main is func
local
const integer: limit is 199;
var integer: n is 1;
var integer: p is 0;
begin
writeln(" n n**3+2");
writeln("------------");
for n range 1 to limit step 2 do
p := n ** 3 + 2;
if isPrime(p) then
writeln(n lpad 3 <& p lpad 9);
end if;
end for;
end func;
- Output:
n n**3+2 ------------ 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
Sidef
1..^200 -> map { _**3 + 2 }.grep {.is_prime}.say
- Output:
[3, 29, 127, 24391, 91127, 250049, 274627, 328511, 357913, 571789, 1157627, 1442899, 1860869, 2146691, 2924209, 3581579, 5000213, 5177719, 6751271]
Swift
import Foundation
func isPrime(_ n: Int) -> Bool {
if n < 2 {
return false
}
if n % 2 == 0 {
return n == 2
}
if n % 3 == 0 {
return n == 3
}
var p = 5
while p * p <= n {
if n % p == 0 {
return false
}
p += 2
if n % p == 0 {
return false
}
p += 4
}
return true
}
for n in 1...200 {
let p = n * n * n + 2
if isPrime(p) {
print(String(format: "%3d%9d", n, p))
}
}
- Output:
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
Wren
If n is even then n³ + 2 is also even, so we only need to examine odd values of n here.
import "/math" for Int
import "/trait" for Stepped
import "/fmt" for Fmt
var limit = 200
for (n in Stepped.new(1...limit, 2)) {
var p = n*n*n + 2
if (Int.isPrime(p)) Fmt.print("n = $3d => n³ + 2 = $,9d", n, p)
}
- Output:
n = 1 => n³ + 2 = 3 n = 3 => n³ + 2 = 29 n = 5 => n³ + 2 = 127 n = 29 => n³ + 2 = 24,391 n = 45 => n³ + 2 = 91,127 n = 63 => n³ + 2 = 250,049 n = 65 => n³ + 2 = 274,627 n = 69 => n³ + 2 = 328,511 n = 71 => n³ + 2 = 357,913 n = 83 => n³ + 2 = 571,789 n = 105 => n³ + 2 = 1,157,627 n = 113 => n³ + 2 = 1,442,899 n = 123 => n³ + 2 = 1,860,869 n = 129 => n³ + 2 = 2,146,691 n = 143 => n³ + 2 = 2,924,209 n = 153 => n³ + 2 = 3,581,579 n = 171 => n³ + 2 = 5,000,213 n = 173 => n³ + 2 = 5,177,719 n = 189 => n³ + 2 = 6,751,271
XPL0
func IsPrime(N); \Return 'true' if N is a prime number
int N, I;
[if N <= 1 then return false;
for I:= 2 to sqrt(N) do
if rem(N/I) = 0 then return false;
return true;
];
int N;
[for N:= 1 to 199 do
[if IsPrime(N*N*N+2) then
[IntOut(0, N);
ChOut(0, 9\tab\);
IntOut(0, N*N*N+2);
CrLf(0);
]
];
]
- Output:
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