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

Added Easylang
(added Arturo)
(Added Easylang)
 
(7 intermediate revisions by 6 users not shown)
Line 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 907 ⟶ 931:
=={{header|Fōrmulæ}}==
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Find_prime_numbers_of_the_form_n%C2%B3%2B2}}
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 &mdash;i.e. XML, JSON&mdash; they are intended for storage and transfer purposes more than visualization and edition.
 
'''Solution'''
Programs in Fōrmulæ are created/edited online in its [https://formulae.org 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.
 
[[File:Fōrmulæ - Find prime numbers of the form n³ + 2 01.png]]
In '''[https://formulae.org/?example=Find_prime_numbers_of_the_form_n%C2%B3%2B2 this]''' page you can see the program(s) related to this task and their results.
 
[[File:Fōrmulæ - Find prime numbers of the form n³ + 2 02.png]]
 
=={{header|Go}}==
Line 1,213 ⟶ 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,360 ⟶ 1,482:
=={{header|Quackery}}==
 
<code>isprimeprime</code> is defined at [[PrimalityMiller–Rabin byprimality trial divisiontest#Quackery]].
 
<syntaxhighlight lang="Quackery"> [ dip number$
Line 1,369 ⟶ 1,491:
199 times
[ i^ 1+ 3 ** 2 +
dup isprimeprime iff
[ i^ 1+ 4 recho
sp 7 recho cr ]
Line 1,531 ⟶ 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,713 ⟶ 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="ecmascriptwren">import "./math" for Int
import "./iterate" for Stepped
import "./fmt" for Fmt
 
var limit = 200
1,995

edits