Cubic special primes: Difference between revisions

Added Lua
(Added C)
(Added Lua)
 
(7 intermediate revisions by 4 users not shown)
Line 270:
<pre>
Same as Wren example.
</pre>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
 
This program makes extensive use of the [[Extensible_prime_generator#Delphi|Delphi Prime-Generator Object]]
 
<syntaxhighlight lang="Delphi">
procedure CubicSpecialPrimes(Memo: TMemo);
const Limit = 15000;
var Sieve: TPrimeSieve;
var N,I,PP, Count: integer;
var S: string;
begin
Sieve:=TPrimeSieve.Create;
try
{Get all primes up to limit}
Sieve.Intialize(Limit);
N:=1;
Count:=0;
I:=1;
S:='';
while true do
begin
{Find first cube increment that is prime}
PP:=N +I*I*I;
if PP>Limit then break;
if Sieve.Flags[PP] then
begin
Inc(Count);
S:=S+Format('%6D',[PP]);
if (Count mod 5) = 0 then S:=S+CRLF;
{Step to next cube position}
I:=1;
N:=PP;
end
else Inc(I);
end;
Memo.Lines.Add(Format('There are %d cubic special primes',[count]));
Memo.Lines.Add(S);
finally Sieve.Free; end;
end;
 
 
</syntaxhighlight>
{{out}}
<pre>
There are 23 cubic special primes
2 3 11 19 83
1811 2027 2243 2251 2467
2531 2539 3539 3547 4547
5059 10891 12619 13619 13627
13691 13907 14419
Elapsed Time: 2.178 ms.
</pre>
 
Line 400 ⟶ 455:
Same as Wren example.
</pre>
 
=={{header|J}}==
<syntaxhighlight lang="j">cubes=. 3 ^~ 1 , 10 + i: 8j8
nextp=. ({.@#~ 1&p:)@(+&cubes)
nextp^:(14e3&>)^:a: 2</syntaxhighlight>
{{out}}
<pre>2 3 11 19 83 1811 2027 2243 2251 2467 2531 2539 3539 3547 4547 5059 10891 12619 13619 13627 13691 13907 14419</pre>
 
=={{header|jq}}==
Line 460 ⟶ 524:
2531 2539 3539 3547 4547 5059 10891 12619 13619 13627
13691 13907 14419
</pre>
 
=={{header|Lua}}==
<syntaxhighlight lang="lua">
do -- find a sequence of primes where the members differ by a cube
 
local maxPrime = 15000
-- sieve the primes to maxPrime
local isPrime = {}
for i = 1, maxPrime do isPrime[ i ] = i % 2 ~= 0 end
isPrime[ 1 ] = false
isPrime[ 2 ] = true
for s = 3, math.floor( math.sqrt( maxPrime ) ), 2 do
if isPrime[ s ] then
for p = s * s, maxPrime, s do isPrime[ p ] = false end
end
end
 
-- construct a table of cubes, we will need at most the cube root of maxPrime
local cube = {}
for i = 1, math.floor( ( maxPrime ^ ( 1 / 3 ) ) ) do cube[ i ] = i * i * i end
 
-- find the prime sequence
io.write( "2" )
local p = 2
while p < maxPrime do
-- find a prime that is p + a cube
local q, i = 0, 1
repeat
q, i = p + cube[ i ], i + 1
until q > maxPrime or isPrime[ q ]
if q <= maxPrime then io.write( " ", q ) end
p = q
end
io.write( "\n" )
end
</syntaxhighlight>
{{out}}
<pre>
2 3 11 19 83 1811 2027 2243 2251 2467 2531 2539 3539 3547 4547 5059 10891 12619 13619 13627 13691 13907 14419
</pre>
 
Line 747 ⟶ 851:
Found 23 of the smallest primes < 15,000 such that the difference of successive terma are the smallest cubic numbers
done...
</pre>
 
=={{header|RPL}}==
{{works with|HP|49}}
« { } 2 1
'''DO''' DUP2 3 ^ +
'''IF''' DUP ISPRIME?
'''THEN''' 4 ROLL 4 ROLL + ROT DROP SWAP 1
'''ELSE''' DROP 1 + '''END'''
'''UNTIL''' OVER 15000 > '''END'''
DROP2
» '<span style="color:blue">TASK</span>' STO
{{out}}
<pre>
1: { 2 3 11 19 83 1811 2027 2243 2251 2467 2531 2539 3539 3547 4547 5059 10891 12619 13619 13627 13691 13907 14419 }
</pre>
 
Line 763 ⟶ 882:
<pre>2 3 11 19 83 1811 2027 2243 2251 2467 2531 2539 3539 3547 4547 5059 10891 12619 13619 13627 13691 13907 14419
</pre>
 
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">func cubic_primes(callback) {
Line 790 ⟶ 910:
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./math" for Int
import "./fmt" for Fmt
 
var primes = Int.primeSieve(14999)
3,032

edits