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
m (→{{header|Wren}}: Wren-trait -> Wren-iterate) |
(Added Easylang) |
||
(8 intermediate revisions by 7 users not shown) | |||
Line 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}}==
Line 658 ⟶ 693:
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#}}==
Line 872 ⟶ 931:
=={{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}}==
Line 1,178 ⟶ 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>
Line 1,325 ⟶ 1,482:
=={{header|Quackery}}==
<code>
<syntaxhighlight lang="Quackery"> [ dip number$
Line 1,334 ⟶ 1,491:
199 times
[ i^ 1+ 3 ** 2 +
dup
[ i^ 1+ 4 recho
sp 7 recho cr ]
Line 1,496 ⟶ 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}}==
Line 1,678 ⟶ 1,855:
{{libheader|Wren-fmt}}
If ''n'' is even then ''n³ + 2'' is also even, so we only need to examine odd values of ''n'' here.
<syntaxhighlight lang="
import "./iterate" for Stepped
import "./fmt" for Fmt
var limit = 200
|