Cubic special primes: Difference between revisions

Added Lua
No edit summary
(Added Lua)
 
(6 intermediate revisions by 4 users not shown)
Line 275:
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
//https://rosettacode.org/wiki/Extensible_prime_generator#Delphi
 
This program makes extensive use of the [[Extensible_prime_generator#Delphi|Delphi Prime-Generator Object]]
 
<syntaxhighlight lang="Delphi">
Line 326:
Elapsed Time: 2.178 ms.
</pre>
 
 
=={{header|F_Sharp|F#}}==
Line 456 ⟶ 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 516 ⟶ 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 803 ⟶ 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 819 ⟶ 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 846 ⟶ 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