Find prime numbers of the form n*n*n+2: Difference between revisions
Content added Content deleted
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}} |