Cubic special primes: Difference between revisions

Added Lua
(Added Sidef)
(Added Lua)
 
(22 intermediate revisions by 14 users not shown)
Line 6:
where     '''n'''   <   '''15000'''.
<br><br>
 
=={{header|11l}}==
{{trans|FreeBASIC}}
 
<syntaxhighlight lang="11l">F is_prime(a)
I a == 2
R 1B
I a < 2 | a % 2 == 0
R 0B
L(i) (3 .. Int(sqrt(a))).step(2)
I a % i == 0
R 0B
R 1B
 
V p = 2
V n = 1
print(2, end' ‘ ’)
 
L p + n ^ 3 < 15000
I is_prime(p + n ^ 3)
p += n ^ 3
n = 1
print(p, end' ‘ ’)
E
n++</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|Action!}}==
{{libheader|Action! Sieve of Eratosthenes}}
<syntaxhighlight lang="action!">INCLUDE "H6:SIEVE.ACT"
 
PROC Main()
DEFINE MAX="14999"
BYTE ARRAY primes(MAX+1)
INT i=[2],next,count=[1],n=[1]
 
Put(125) PutE() ;clear the screen
Sieve(primes,MAX+1)
PrintI(i)
DO
next=i+n*n*n
IF next>=MAX THEN
EXIT
FI
IF primes(next) THEN
n=1 i=next count==+1
Put(32) PrintI(i)
ELSE
n==+1
FI
OD
PrintF("%E%EThere are %I cubic special primes",count)
RETURN</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Cubic_Special_Primes.png Screenshot from Atari 8-bit computer]
<pre>
2 3 11 19 83 1811 2027 2243 2251 2467 2531 2539 3539 3547 4547 5059 10891 12619 13619 13627 13691 13907 14419
 
There are 23 cubic special primes
</pre>
 
=={{header|ALGOL 68}}==
{{libheader|ALGOL 68-primes}}
<lang algol68>BEGIN # find a sequence of primes where the members differ by a cube #
<syntaxhighlight lang="algol68">BEGIN # find a sequence of primes where the members differ by a cube #
INT max prime = 15 000;
# sieve the primes to max prime #
PR read "primes.incl.a68" PR
[ 1 : max prime ]BOOL prime;
prime[ 1 ] := FALSE;BOOL prime[ 2= ]PRIMESIEVE :=max TRUEprime;
FOR i FROM 3 BY 2 TO UPB prime DO prime[ i ] := TRUE OD;
FOR i FROM 4 BY 2 TO UPB prime DO prime[ i ] := FALSE OD;
FOR i FROM 3 BY 2 TO ENTIER sqrt( max prime ) DO
IF prime[ i ] THEN FOR s FROM i * i BY i + i TO UPB prime DO prime[ s ] := FALSE OD FI
OD;
# construct a table of cubes, we will need at most the cube root of max prime #
[ 1 : ENTIER exp( ln( max prime ) / 3 ) ]INT cube;
Line 34 ⟶ 94:
OD;
print( ( newline ) )
END</langsyntaxhighlight>
{{out}}
<pre>
Line 41 ⟶ 101:
 
=={{header|ALGOL W}}==
<langsyntaxhighlight lang="algolw">begin % find a sequence of primes where the members differ by a cube %
integer MAX_PRIME, MAX_CUBE;
MAX_PRIME := 15000;
Line 75 ⟶ 135:
end;
write()
end.</langsyntaxhighlight>
{{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|Arturo}}==
{{trans|FreeBASIC}}
<syntaxhighlight lang="arturo">[p n]: [2 1]
prints -> 2
 
until -> 15000 =< p + n^3 [
if? prime? p + n^3 [
'p + n^3
n: 1
prints -> p
]
else -> 'n+1
]</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|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f CUBIC_SPECIAL_PRIMES.AWK
# converted from FreeBASIC
Line 115 ⟶ 193:
return(1)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 122 ⟶ 200:
13691 13907 14419
Cubic special primes 2-15000: 23
</pre>
 
=={{header|C}}==
{{trans|Wren}}
<syntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <math.h>
#include <locale.h>
 
bool *sieve(int limit) {
int i, p;
limit++;
// True denotes composite, false denotes prime.
bool *c = calloc(limit, sizeof(bool)); // all false by default
c[0] = true;
c[1] = true;
for (i = 4; i < limit; i += 2) c[i] = true;
p = 3; // Start from 3.
while (true) {
int p2 = p * p;
if (p2 >= limit) break;
for (i = p2; i < limit; i += 2 * p) c[i] = true;
while (true) {
p += 2;
if (!c[p]) break;
}
}
return c;
}
 
bool isCube(int n) {
int s = (int)cbrt((double)n);
return s*s*s == n;
}
 
int main() {
const int limit = 14999;
int i, j, p, pc = 0, gap = 1, count = 1, lastCubicSpecial = 3;
const char *fmt = "%'7d %'7d %'6d %'4d\n";
bool *c = sieve(limit);
for (i = 0; i < limit; ++i) {
if (!c[i]) ++pc;
}
int *primes = (int *)malloc(pc * sizeof(int));
for (i = 0, j = 0; i < limit; ++i) {
if (!c[i]) primes[j++] = i;
}
free(c);
printf("Cubic special primes under 15,000:\n");
printf(" Prime1 Prime2 Gap Cbrt\n");
setlocale(LC_NUMERIC, "");
printf(fmt, 2, 3, 1, 1);
for (i = 2; i < pc; ++i) {
p = primes[i];
gap = p - lastCubicSpecial;
if (isCube(gap)) {
printf(fmt, lastCubicSpecial, p, gap, (int)cbrt((double)gap));
lastCubicSpecial = p;
++count;
}
}
printf("\n%d such primes found.\n", count+1);
free(primes);
return 0;
}</syntaxhighlight>
 
{{out}}
<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>
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">
// Cubic Special Primes: Nigel Galloway. March 30th., 2021
let fN=let N=[for n in [0..25]->n*n*n] in let mutable n=2 in (fun g->match List.contains(g-n)N with true->n<-g; true |_->false)
primes32()|>Seq.takeWhile((>)16000)|>Seq.filter fN|>Seq.iter(printf "%d "); printfn ""
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 137 ⟶ 340:
=={{header|Factor}}==
{{works with|Factor|0.98}}
<langsyntaxhighlight lang="factor">USING: fry io kernel lists lists.lazy math math.functions
math.primes prettyprint ;
 
2 [ 1 lfrom swap '[ 3 ^ _ + ] lmap-lazy [ prime? ] lfilter car ]
lfrom-by [ 15000 < ] lwhile [ pprint bl ] leach nl</langsyntaxhighlight>
{{out}}
<pre>
Line 148 ⟶ 351:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">
#include once "isprime.bas"
 
Line 165 ⟶ 368:
loop until p + n^3 >= 15000
print
end</langsyntaxhighlight>
{{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
Line 172 ⟶ 375:
=={{header|Go}}==
{{trans|Wren}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 246 ⟶ 449:
}
fmt.Println("\n", count+1, "such primes found.")
}</langsyntaxhighlight>
 
{{out}}
<pre>
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}}==
'''Adapted from [[#Julia|Julia]]'''
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
 
For the definition of `is_prime` used here, see https://rosettacode.org/wiki/Additive_primes<syntaxhighlight lang="jq"># Output: an array
def cubic_special_primes:
. as $N
| sqrt as $maxidx
| {cprimes: [2], q: 0}
| until( .cprimes[-1] >= $N or .q >= $N;
label $out
| foreach range(1; $maxidx + 1) as $i (.;
.q = (.cprimes[-1] + ($i * $i * $i))
| if .q >= $N
then .emit = true
elif .q | is_prime
then .cprimes = .cprimes + [.q]
| .emit = true
else .
end;
select(.emit)) | {cprimes, q}, break $out )
| .cprimes ;
15000
| ("Cubic special primes < \(.):",
cubic_special_primes)</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|Julia}}==
<langsyntaxhighlight lang="julia">using Primes
 
function cubicspecialprimes(N = 15000)
Line 274 ⟶ 518:
println("Cubic special primes < 15000:")
foreach(p -> print(rpad(p[2], 6), p[1] % 10 == 0 ? "\n" : ""), enumerate(cubicspecialprimes()))
</langsyntaxhighlight>{{out}}
<pre>
Cubic special primes < 16000:
Line 281 ⟶ 525:
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>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">start = {2};
Do[
If[PrimeQ[i],
last = Last[start];
If[IntegerQ[Surd[i - last, 3]],
AppendTo[start, i]
]
]
,
{i, 3, 15000}
]
start</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|Nim}}==
{{trans|Go}}
<langsyntaxhighlight Nimlang="nim">import math, strformat
 
func sieve(limit: Positive): seq[bool] =
Line 320 ⟶ 620:
lastCubicSpecial = n
inc count
echo &"\n{count + 1} such primes found."</langsyntaxhighlight>
 
{{out}}
Line 352 ⟶ 652:
=={{header|Perl}}==
{{libheader|ntheory}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use feature 'say';
Line 365 ⟶ 665:
} until $sp[-1] >= 15000;
 
pop @sp and say join ' ', @sp;</langsyntaxhighlight>
{{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 371 ⟶ 671:
=={{header|Phix}}==
See [[Quadrat_Special_Primes#Phix]]
 
 
=={{header|Python}}==
<syntaxhighlight lang="python">#!/usr/bin/python
 
def isPrime(n):
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
return False
return True
 
if __name__ == '__main__':
p = 2
n = 1
 
print("2",end = " ")
while True:
if isPrime(p + n**3):
p += n**3
n = 1
print(p,end = " ")
else:
n += 1
if p + n**3 >= 15000:
break</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|Raku}}==
A two character difference from the [[Quadrat_Special_Primes#Raku|Quadrat Special Primes]] entry. (And it could have been one.)
<syntaxhighlight lang="raku" perl6line>my @sqp = 2, -> $previous {
my $next;
for (1..∞).map: *³ {
Line 384 ⟶ 712:
 
say "{+$_} matching numbers:\n", $_».fmt('%5d').batch(7).join: "\n" given
@sqp[^(@sqp.first: * > 15000, :k)];</langsyntaxhighlight>
{{out}}
<pre>23 matching numbers:
Line 393 ⟶ 721:
 
=={{header|REXX}}==
<langsyntaxhighlight lang="rexx">/*REXX program finds the smallest primes such that the difference of successive terms */
/*───────────────────────────────────────────────────── are the smallest cubic numbers. */
parse arg hi cols . /*obtain optional argument from the CL.*/
Line 443 ⟶ 771:
end /*k*/ /* [↑] only process numbers ≤ √ J */
#= #+1; @.#= j; s.#= j*j; !.j= 1 /*bump # of Ps; assign next P; P²; P# */
end /*j*/; return</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 458 ⟶ 786:
=={{header|Ring}}==
Also see [[Quadrat_Special_Primes#Ring]]
<langsyntaxhighlight lang="ring">
load "stdlib.ring"
 
Line 494 ⟶ 822:
see "done..." + nl
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 523 ⟶ 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>
 
=={{header|Ruby}}==
<syntaxhighlight lang="ruby">require 'prime'
 
res = [2]
 
until res.last > 15000 do
res << (1..).detect{|n| (res.last + n**3).prime? } ** 3 + res.last
end
 
puts res[..-2].join(" ")
</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|Sidef}}==
<langsyntaxhighlight lang="ruby">func cubic_primes(callback) {
 
var prev = 2
Line 543 ⟶ 901:
take(k)
})
}</langsyntaxhighlight>
{{out}}
<pre>
Line 552 ⟶ 910:
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight ecmascriptlang="wren">import "./math" for Int, Math
import "./fmt" for Fmt
 
var isCube = Fn.new { |n|
var c = Math.cbrt(n).round
return c*c*c == n
}
 
var primes = Int.primeSieve(14999)
Line 569 ⟶ 922:
for (p in primes.skip(2)) {
gap = p - lastCubicSpecial
if (isCubeInt.callisCube(gap)) {
Fmt.print("$,7d $,7d $,6d $4d", lastCubicSpecial, p, gap, Mathgap.cbrt(gap).roundtruncate)
lastCubicSpecial = p
count = count + 1
}
}
System.print("\n%(count+1) such primes found.")</langsyntaxhighlight>
 
{{out}}
Line 605 ⟶ 958:
 
23 such primes found.
</pre>
 
=={{header|XPL0}}==
<syntaxhighlight lang="xpl0">func IsPrime(N); \Return 'true' if N is prime
int N, I;
[if N <= 2 then return N = 2;
if (N&1) = 0 then \even >2\ return false;
for I:= 3 to sqrt(N) do
[if rem(N/I) = 0 then return false;
I:= I+1;
];
return true;
];
 
int N, C, T;
[N:= 2;
repeat C:= 1;
loop [T:= N + C*C*C;
if IsPrime(T) then
[IntOut(0, N);
ChOut(0, ^ );
N:= T;
quit;
]
else C:= C+1;
];
until N > 15000;
]</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>
3,032

edits