Cuban primes: Difference between revisions

111,924 bytes added ,  2 months ago
m
m (Add leading zeros)
 
(97 intermediate revisions by 41 users not shown)
Line 1:
{{task|Prime Numbers}}
 
The name   '''cuban'''   has nothing to do with   [https://en.wikipedia.org/wiki/Cuba Cuba  (the country)],   but has to do with the
fact that cubes &nbsp; (3<sup>rd</sup> powers) &nbsp; play a role in its definition.
 
Line 19:
::* &nbsp; show the first &nbsp; 200 &nbsp; cuban primes &nbsp; (in a multi─line horizontal format).
::* &nbsp; show the &nbsp; 100,000<sup>th</sup> &nbsp; cuban prime.
::* &nbsp; show all cuban primes with commas &nbsp; (if appropriate).
::* &nbsp; show all output here.
 
 
Note that &nbsp; '''cuban prime''' &nbsp; isn't capitalized &nbsp; (as it doesn't refer to the nation of Cuba).
 
 
;Also see:
:* &nbsp; MathWorldWikipedia entry: &nbsp; &nbsp; [http[wp://mathworld.wolfram.com/CubanPrime.html <u>Cuban_prime|cuban prime</u>]].
:* &nbsp; MathWorld entry: &nbsp; [http://mathworld.wolfram.com/CubanPrime.html cuban prime].
:* &nbsp; The OEIS entry: &nbsp; [http://oeis.org/A002407 <u>A002407</u>]. &nbsp; &nbsp; The &nbsp; 100,000<sup>th</sup> &nbsp; cuban prime can be verified in the &nbsp; 2<sup>nd</sup> &nbsp; ''example'' &nbsp; on this OEIS web page.
:* &nbsp; The OEIS entry: &nbsp; &nbsp; [[oeis:A002407|A002407]]. &nbsp; &nbsp; The &nbsp; 100,000<sup>th</sup> &nbsp; cuban prime can be verified in the &nbsp; 2<sup>nd</sup> &nbsp; ''example'' &nbsp; on this OEIS web page.
<br><br>
 
=={{header|ALGOL 68}}==
{{libheader|ALGOL 68-primes}}
<lang algol68>BEGIN
<syntaxhighlight lang="algol68">
BEGIN
# find some cuban primes (using the definition: a prime p is a cuban prime if #
# p = n^3 - ( n - 1 )^3 #
# for some n > 0) #
PR read "primes.incl.a68" PR # include prime utilities #
 
# returns a string representation of n with commas #
PROC commatise = ( LONG INT n )STRING:
Line 52 ⟶ 55:
END # commatise # ;
 
# sieve the primes #
INT sieve max = 2 000 000;
[]BOOL sieve max= ]BOOLPRIMESIEVE sieve max; FOR i TO UPB sieve DO # sieve[ ithe ]primes :=to TRUEmax OD;sieve #
sieve[ 1 ] := FALSE;
FOR s FROM 2 TO ENTIER sqrt( sieve max ) DO
IF sieve[ s ] THEN
FOR p FROM s * s BY s TO sieve max DO sieve[ p ] := FALSE OD
FI
OD;
# count the primes, we can ignore 2, as we know it isn't a cuban prime #
sieve[ 2 ] := FALSE;
INT prime count := 0;
FOR s TO UPB sieve DO IF sieve[ s ] THEN prime count +:= 1 FI OD;
# construct a list of the primes #
[ 1 : prime count ]INT primes;
INT prime pos := LWB primes;
FOR s FROM LWB sieve TO UPB sieve DO
IF sieve[ s ] THEN primes[ prime pos ] := s; prime pos +:= 1 FI
OD;
 
# find the cuban primes #
Line 77 ⟶ 63:
INT max cuban = 100 000; # mximum number of cubans to find #
INT print limit = 200; # show all cubans up to this one #
print( ( "First ", commatise( print limit ), " cuban primes: ", newline ) );
LONG INT prev cube := 1;
FOR n FROM 2 WHILE
Line 85 ⟶ 71:
IF ODD p THEN
# 2 is not a cuban prime so we only test odd numbers #
BOOLIF IF p <= UPB sieve THEN sieve[ SHORTEN p ] ELSE is probably prime( p ) := TRUE;FI
THEN
INT max factor = SHORTEN ENTIER long sqrt( p );
FOR f FROM LWB primes WHILE is prime AND primes[ f ] <= max factor DO
is prime := p MOD primes[ f ] /= 0
OD;
IF is prime THEN
# have a cuban prime #
IF ( cuban count +:= 1; ) <= print limit THEN
IF cuban count <= print limit THEN
# must show this cuban #
STRING p formatted = commatise( p );
Line 106 ⟶ 87:
IF cuban count MOD 10 /= 0 THEN print( ( newline ) ) FI;
print( ( "The ", commatise( max cuban ), " cuban prime is: ", commatise( final cuban ), newline ) )
END</lang>
</syntaxhighlight>
{{out}}
<pre>
First 200 cuban primes:
7 19 37 61 127 271 331 397 547 631
919 1,657 1,801 1,951 2,269 2,437 2,791 3,169 3,571 4,219
Line 133 ⟶ 115:
</pre>
 
=={{header|C++AppleScript}}==
The shortcut for calculating the difference between successive cube pairs is filched from other solutions below. Most of the running time's spent checking for primes. The isPrime() handler's been tuned for this particular task, but even so the script takes around 85 minutes to complete on my current machine!
{{trans|C#}}
<lang Cpp>#include <iostream>
#include <vector>
#include <chrono>
#include <climits>
#include <cmath>
 
<syntaxhighlight lang="applescript">on isPrime(n)
using namespace std;
-- Most of the numbers tested in this script will be huge
-- and none will be less than 7 or divisible by 2, 3, or 5.
(* if (n < 7) then return (n is in {2, 3, 5})
if ((n mod 2) * (n mod 3) * (n mod 5) = 0) then return false *)
repeat with i from 7 to (n ^ 0.5 div 1) by 30
if ((n mod i) * (n mod (i + 4)) * (n mod (i + 6)) * (n mod (i + 10)) * ¬
(n mod (i + 12)) * (n mod (i + 16)) * (n mod (i + 22)) * (n mod (i + 24)) = 0) then ¬
return false
end repeat
return true
end isPrime
 
on join(lst, delim)
vector <long long> primes{ 3, 5 };
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to delim
set txt to lst as text
set AppleScript's text item delimiters to astid
return txt
end join
 
on intToText(int, separator)
int main()
set groups to {}
{
repeat while (int > 999)
cout.imbue(locale(""));
set groups's beginning to ((1000 + (int mod 1000 as integer)) as text)'s text 2 thru 4
const int cutOff = 200, bigUn = 100000,
set int chunksto =int 50,div little = bigUn / chunks;1000
end repeat
const char tn[] = " cuban prime";
set groups's beginning to int
cout << "The first " << cutOff << tn << "s:" << endl;
return join(groups, separator)
int c = 0;
end intToText
bool showEach = true;
long long u = 0, v = 1;
auto st = chrono::system_clock::now();
 
on task()
for (long long i = 1; i <= LLONG_MAX; i++)
set output to {"The first 200 cuban primes are:"}
{
set inc to 0
bool found = false;
set candidate to 1
long long mx = (long long)(ceil(sqrt(v += (u += 6))));
set counter to 0
for (long long item : primes)
set row to {}
{
repeat until (counter = 200)
if (item > mx) break;
if (v % item == 0) { found =set true;inc break;to }inc + 6
set candidate to candidate + inc
}
if (isPrime(candidate)) then
if (!found)
set counter to counter + 1
{
set end of row to (" " & intToText(candidate, ","))'s text -11 thru -1
c += 1; if (showEach)
if ((counter) mod 8 = 0) then
{
set end of output to join(row, "")
for (long long z = primes.back() + 2; z <= v - 2; z += 2)
set row to {}
{
end if
bool fnd = false;
end if
for (long long item : primes)
end repeat
{
repeat until (counter = 100000)
if (item > mx) break;
set inc to inc + 6
if (z % item == 0) { fnd = true; break; }
set candidate to candidate + inc
}
if (isPrime(candidate)) then set counter to counter + 1
if (!fnd) primes.push_back(z);
end repeat
}
set end of output to linefeed & "The 100,000th is " & intToText(candidate, ",")
primes.push_back(v); cout.width(11); cout << v;
return join(output, linefeed)
if (c % 10 == 0) cout << endl;
end task
if (c == cutOff)
 
{
task()</syntaxhighlight>
showEach = false;
 
cout << "\nProgress to the " << bigUn << "th" << tn << ": ";
{{output}}
}
<syntaxhighlight lang="applescript">"The first 200 cuban primes are:
}
7 19 37 61 127 271 331 397
if (c % little == 0) { cout << "."; if (c == bigUn) break; }
547 631 919 1,657 1,801 1,951 2,269 2,437
}
2,791 3,169 3,571 4,219 4,447 5,167 5,419 6,211
}
7,057 7,351 8,269 9,241 10,267 11,719 12,097 13,267
cout << "\nThe " << c << "th" << tn << " is " << v;
13,669 16,651 19,441 19,927 22,447 23,497 24,571 25,117
chrono::duration<double> elapsed_seconds = chrono::system_clock::now() - st;
26,227 27,361 33,391 35,317 42,841 45,757 47,251 49,537
cout << "\nComputation time was " << elapsed_seconds.count() << " seconds" << endl;
50,311 55,897 59,221 60,919 65,269 70,687 73,477 74,419
return 0;
75,367 81,181 82,171 87,211 88,237 89,269 92,401 96,661
}</lang>
102,121 103,231 104,347 110,017 112,327 114,661 115,837 126,691
129,169 131,671 135,469 140,617 144,541 145,861 151,201 155,269
163,567 169,219 170,647 176,419 180,811 189,757 200,467 202,021
213,067 231,019 234,361 241,117 246,247 251,431 260,191 263,737
267,307 276,337 279,991 283,669 285,517 292,969 296,731 298,621
310,087 329,677 333,667 337,681 347,821 351,919 360,187 368,551
372,769 374,887 377,011 383,419 387,721 398,581 407,377 423,001
436,627 452,797 459,817 476,407 478,801 493,291 522,919 527,941
553,411 574,219 584,767 590,077 592,741 595,411 603,457 608,851
611,557 619,711 627,919 650,071 658,477 666,937 689,761 692,641
698,419 707,131 733,591 742,519 760,537 769,627 772,669 784,897
791,047 812,761 825,301 837,937 847,477 863,497 879,667 886,177
895,987 909,151 915,769 925,741 929,077 932,419 939,121 952,597
972,991 976,411 986,707 990,151 997,057 1,021,417 1,024,921 1,035,469
1,074,607 1,085,407 1,110,817 1,114,471 1,125,469 1,155,061 1,177,507 1,181,269
1,215,397 1,253,887 1,281,187 1,285,111 1,324,681 1,328,671 1,372,957 1,409,731
1,422,097 1,426,231 1,442,827 1,451,161 1,480,519 1,484,737 1,527,247 1,570,357
 
The 100,000th is 1,792,617,147,127"</syntaxhighlight>
 
=={{header|Arturo}}==
<syntaxhighlight lang="arturo">cubes: map 1..780000 'x -> x^3
 
cubans: []
i: 1
while [100000 > size cubans][
num: cubes\[i] - cubes\[i-1]
if prime? num ->
'cubans ++ num
inc 'i
]
 
first200primes: first.n: 200 cubans
 
loop split.every: 10 first200primes 'x ->
print map x 's -> pad to :string s 8
 
print ""
print ["The 100000th Cuban prime is" last cubans]</syntaxhighlight>
 
{{out}}
 
<pre> 7 19 37 61 127 271 331 397 547 631
919 1657 1801 1951 2269 2437 2791 3169 3571 4219
4447 5167 5419 6211 7057 7351 8269 9241 10267 11719
12097 13267 13669 16651 19441 19927 22447 23497 24571 25117
26227 27361 33391 35317 42841 45757 47251 49537 50311 55897
59221 60919 65269 70687 73477 74419 75367 81181 82171 87211
88237 89269 92401 96661 102121 103231 104347 110017 112327 114661
115837 126691 129169 131671 135469 140617 144541 145861 151201 155269
163567 169219 170647 176419 180811 189757 200467 202021 213067 231019
234361 241117 246247 251431 260191 263737 267307 276337 279991 283669
285517 292969 296731 298621 310087 329677 333667 337681 347821 351919
360187 368551 372769 374887 377011 383419 387721 398581 407377 423001
436627 452797 459817 476407 478801 493291 522919 527941 553411 574219
584767 590077 592741 595411 603457 608851 611557 619711 627919 650071
658477 666937 689761 692641 698419 707131 733591 742519 760537 769627
772669 784897 791047 812761 825301 837937 847477 863497 879667 886177
895987 909151 915769 925741 929077 932419 939121 952597 972991 976411
986707 990151 997057 1021417 1024921 1035469 1074607 1085407 1110817 1114471
1125469 1155061 1177507 1181269 1215397 1253887 1281187 1285111 1324681 1328671
1372957 1409731 1422097 1426231 1442827 1451161 1480519 1484737 1527247 1570357
 
The 100000th Cuban prime is 1792617147127</pre>
 
=={{header|BASIC}}==
==={{header|BASIC256}}===
<syntaxhighlight lang="vb">function isprime(v)
if v mod 2 = 0 then return v = 2
for d = 3 To Int(Sqr(v))+1 Step 2
if v mod d = 0 then return false
next d3
return True
end function
 
function diff_cubes(n)
return 3*n*(n+1) + 1
end function
 
function padto(n, s)
outstr = ""
k = length(string(n))
for i = 1 to s-k
outstr = " " + outstr
next i
return outstr + string(n)
end function
 
print "Los primeros 200 primos cubanos son: "
 
nc = 0
i = 1
while nc < 100000
di = diff_cubes(i)
if isprime(di) then
nc += 1
if nc <= 200 then
print padto(di,8);" ";
if nc mod 10 = 0 then print
end if
if nc = 100000 then
print: print
print "El 100.000º primo cubano es ", di
exit while
end if
end if
i += 1
end while</syntaxhighlight>
 
==={{header|FreeBASIC}}===
<syntaxhighlight lang="vb">function isprime( n as ulongint ) as boolean
if n mod 2 = 0 then return false
for i as uinteger = 3 to int(sqr(n))+1 step 2
if n mod i = 0 then return false
next i
return true
end function
 
function diff_cubes( n as uinteger ) as ulongint
return 3*n*(n+1) + 1
end function
 
function padto( n as uinteger, s as integer ) as string
dim as string outstr=""
dim as integer k = len(str(n))
for i as integer = 1 to s-k
outstr = " " + outstr
next i
return outstr + str(n)
end function
 
dim as integer nc = 0, i = 1, di
while nc < 100000
di = diff_cubes(i)
if isprime(di) then
nc += 1
if nc <= 200 then
print padto(di,8);" ";
if nc mod 10 = 0 then print
end if
if nc = 100000 then
print : print : print di
exit while
end if
end if
i += 1
wend</syntaxhighlight>
{{out}}
<pre>
7 19 37 61 127 271 331 397 547 631
919 1657 1801 1951 2269 2437 2791 3169 3571 4219
4447 5167 5419 6211 7057 7351 8269 9241 10267 11719
12097 13267 13669 16651 19441 19927 22447 23497 24571 25117
26227 27361 33391 35317 42841 45757 47251 49537 50311 55897
59221 60919 65269 70687 73477 74419 75367 81181 82171 87211
88237 89269 92401 96661 102121 103231 104347 110017 112327 114661
115837 126691 129169 131671 135469 140617 144541 145861 151201 155269
163567 169219 170647 176419 180811 189757 200467 202021 213067 231019
234361 241117 246247 251431 260191 263737 267307 276337 279991 283669
285517 292969 296731 298621 310087 329677 333667 337681 347821 351919
360187 368551 372769 374887 377011 383419 387721 398581 407377 423001
436627 452797 459817 476407 478801 493291 522919 527941 553411 574219
584767 590077 592741 595411 603457 608851 611557 619711 627919 650071
658477 666937 689761 692641 698419 707131 733591 742519 760537 769627
772669 784897 791047 812761 825301 837937 847477 863497 879667 886177
895987 909151 915769 925741 929077 932419 939121 952597 972991 976411
986707 990151 997057 1021417 1024921 1035469 1074607 1085407 1110817 1114471
1125469 1155061 1177507 1181269 1215397 1253887 1281187 1285111 1324681 1328671
1372957 1409731 1422097 1426231 1442827 1451161 1480519 1484737 1527247 1570357
 
 
1792617147127
</pre>
 
 
==={{header|Visual Basic .NET}}===
====Corner Cutting Version====
This language doesn't have a built-in for a ''IsPrime()'' function, so I was surprised to find that this runs so quickly. It builds a list of primes while it is creating the output table. Since the last item on the table is larger than the square root of the 100,000<sup>th</sup> cuban prime, there is no need to continue adding to the prime list while checking up to the 100,000<sup>th</sup> cuban prime. I found a bit of a shortcut, if you skip the iterator by just the right amount, only one value is tested for the final result. It's hard-coded in the program, so if another final cuban prime were to be selected for output, the program would need a re-write. If not skipping ahead to the answer, it takes a few seconds over a minute to eventually get to it (see Snail Version below).
<syntaxhighlight lang="vbnet">Module Module1
Dim primes As List(Of Long) = {3L, 5L}.ToList()
 
Sub Main(args As String())
Const cutOff As Integer = 200, bigUn As Integer = 100000,
tn As String = " cuban prime"
Console.WriteLine("The first {0:n0}{1}s:", cutOff, tn)
Dim c As Integer = 0, showEach As Boolean = True, skip As Boolean = True,
v As Long = 0, st As DateTime = DateTime.Now
For i As Long = 1 To Long.MaxValue
v = 3 * i : v = v * i + v + 1
Dim found As Boolean = False, mx As Integer = Math.Ceiling(Math.Sqrt(v))
For Each item In primes
If item > mx Then Exit For
If v Mod item = 0 Then found = True : Exit For
Next : If Not found Then
c += 1 : If showEach Then
For z = primes.Last + 2 To v - 2 Step 2
Dim fnd As Boolean = False
For Each item In primes
If item > mx Then Exit For
If z Mod item = 0 Then fnd = True : Exit For
Next : If Not fnd Then primes.Add(z)
Next : primes.Add(v) : Console.Write("{0,11:n0}", v)
If c Mod 10 = 0 Then Console.WriteLine()
If c = cutOff Then showEach = False
Else
If skip Then skip = False : i += 772279 : c = bigUn - 1
End If
If c = bigUn Then Exit For
End If
Next
Console.WriteLine("{1}The {2:n0}th{3} is {0,17:n0}", v, vbLf, c, tn)
Console.WriteLine("Computation time was {0} seconds", (DateTime.Now - st).TotalSeconds)
If System.Diagnostics.Debugger.IsAttached Then Console.ReadKey()
End Sub
End Module</syntaxhighlight>
{{out}}
<pre>The first 200 cuban primes:
7 19 37 61 127 271 331 397 547 631
919 1,657 1,801 1,951 2,269 2,437 2,791 3,169 3,571 4,219
4,447 5,167 5,419 6,211 7,057 7,351 8,269 9,241 10,267 11,719
12,097 13,267 13,669 16,651 19,441 19,927 22,447 23,497 24,571 25,117
26,227 27,361 33,391 35,317 42,841 45,757 47,251 49,537 50,311 55,897
59,221 60,919 65,269 70,687 73,477 74,419 75,367 81,181 82,171 87,211
88,237 89,269 92,401 96,661 102,121 103,231 104,347 110,017 112,327 114,661
115,837 126,691 129,169 131,671 135,469 140,617 144,541 145,861 151,201 155,269
163,567 169,219 170,647 176,419 180,811 189,757 200,467 202,021 213,067 231,019
234,361 241,117 246,247 251,431 260,191 263,737 267,307 276,337 279,991 283,669
285,517 292,969 296,731 298,621 310,087 329,677 333,667 337,681 347,821 351,919
360,187 368,551 372,769 374,887 377,011 383,419 387,721 398,581 407,377 423,001
436,627 452,797 459,817 476,407 478,801 493,291 522,919 527,941 553,411 574,219
584,767 590,077 592,741 595,411 603,457 608,851 611,557 619,711 627,919 650,071
658,477 666,937 689,761 692,641 698,419 707,131 733,591 742,519 760,537 769,627
772,669 784,897 791,047 812,761 825,301 837,937 847,477 863,497 879,667 886,177
895,987 909,151 915,769 925,741 929,077 932,419 939,121 952,597 972,991 976,411
986,707 990,151 997,057 1,021,417 1,024,921 1,035,469 1,074,607 1,085,407 1,110,817 1,114,471
1,125,469 1,155,061 1,177,507 1,181,269 1,215,397 1,253,887 1,281,187 1,285,111 1,324,681 1,328,671
1,372,957 1,409,731 1,422,097 1,426,231 1,442,827 1,451,161 1,480,519 1,484,737 1,527,247 1,570,357
 
The 100,000th cuban prime is 1,792,617,147,127
Computation time was 0.2989494 seconds</pre>
====Snail Version====
This one doesn't take any shortcuts. It could be sped up (Execution time about 15 seconds) by threading chunks of the search for the 100,000<sup>th</sup> cuban prime, but you would have to take a guess about how far to go, which would be hard-coded, so one might as well use the short-cut version if you are willing to overlook that difficulty.
<syntaxhighlight lang="vbnet">Module Program
Dim primes As List(Of Long) = {3L, 5L}.ToList()
 
Sub Main(args As String())
Dim taskList As New List(Of Task(Of Integer))
Const cutOff As Integer = 200, bigUn As Integer = 100000,
chunks As Integer = 50, little As Integer = bigUn / chunks,
tn As String = " cuban prime"
Console.WriteLine("The first {0:n0}{1}s:", cutOff, tn)
Dim c As Integer = 0, showEach As Boolean = True,
u As Long = 0, v As Long = 1,
st As DateTime = DateTime.Now
For i As Long = 1 To Long.MaxValue
u += 6 : v += u
Dim found As Boolean = False, mx As Integer = Math.Ceiling(Math.Sqrt(v))
For Each item In primes
If item > mx Then Exit For
If v Mod item = 0 Then found = True : Exit For
Next : If Not found Then
c += 1 : If showEach Then
For z = primes.Last + 2 To v - 2 Step 2
Dim fnd As Boolean = False
For Each item In primes
If item > mx Then Exit For
If z Mod item = 0 Then fnd = True : Exit For
Next : If Not fnd Then primes.Add(z)
Next : primes.Add(v) : Console.Write("{0,11:n0}", v)
If c Mod 10 = 0 Then Console.WriteLine()
If c = cutOff Then showEach = False : _
Console.Write("{0}Progress to the {1:n0}th{2}: ", vbLf, bigUn, tn)
End If
If c Mod little = 0 Then Console.Write(".") : If c = bigUn Then Exit For
End If
Next
Console.WriteLine("{1}The {2:n0}th{3} is {0,17:n0}", v, vbLf, c, tn)
Console.WriteLine("Computation time was {0} seconds", (DateTime.Now - st).TotalSeconds)
If System.Diagnostics.Debugger.IsAttached Then Console.ReadKey()
End Sub
End Module</syntaxhighlight>
{{out}}
<pre>The first 200 cuban primes:
Line 221 ⟶ 494:
Progress to the 100,000th cuban prime: ..................................................
The 100,000th cuban prime is 1,792,617,147,127
Computation time was 3549.56445868152 seconds</pre>
====k > 1 Version====
A VB.NET version of the [http://www.rosettacode.org/wiki/Cuban_primes#Raku Raku] version where k > 1, linked at [https://tio.run/##fVVtb9owEP7OrzjlU9KmGbSaJiExqSuthFboNND2cTKJAxaJzWIHqFB/O71zQqghW6SQ3HHP43t5Djbzm1gV/HAYq6TMOFSPXgfwGooc1oXIuYZ7DRO@hWehjf@SwkgavuBF0LFx03KOHmF@2Fg/sM4jQZYbQtcIGMDt524IacYW2sfvAvrym1IZZ7LBPakC5BkKZspy9WGUwkSZmkIGMFtyCX8a8JFg5RJIuMK7JpkavkarX5OsAgyYFSVHz4TvTMNFBvqITrTn4xzrZCbqzKoORixJyAUOtzUeZUIt7EDTzDET0mfFwvZ9agohF35wauuDktpAXJqXND3LCltr5AmFLg8D56xOw3NGE7tgxGZ3wDSIyhXC2jGdUjcfsCFoO@MhM3yGpyDX8TWaqG2Da9VIy6huwSjodatRryBXCdxRflU/sXojJM4KkU5K1BbUUfS7EIY/C8l970kUmNq@@7bvvWnYLnnBkXAA@9u3vhfWLaSWhbAKHLLYHtg6@h6NvjajMdv9YlnJHTBdGaUsUHTV3admkuMaE7DG1cB@0kcf2zkAH99uEBfAJ1hd8NHAUlWiVk4Lg6AnlmkeQr5zcxwzs4weuMhIOtaY/i2MvwmCC2Kq8JHFS8Cu5UhRC/Yirha4jfpKB9ppPO6EuZjEh/AN/aJUoGaCVRnNxv2Tot6@41ZZFBG0nhXDNY0G4x0d@N6@G37pSysBHPkmRD4/tlmhxKzqN/NnFIHntTSnroLUUO/bUYNnUgv@Vwht@CjtnNfWaTVaZPyg8nVpmBFKgqEF2@JaoqpB81jJRGNd/seNQxlpE0QzZVg2rUJOpWE501eNA4mGgi2k0kbEOhryebkgQY/0vTEoB564pf7kLPnOX@vNPf5m0bP6yzgc3gE Try It Online!]
 
=={{header|Bracmat}}==
{{trans|julia}}
<syntaxhighlight lang="bracmat">( ( cubanprimes
=
. !arg:(?N.?bigN)
& :?cubans
& (0.1.1):(?cube100k.?cube1.?count)
& 0:?i
& whl
' ( 1+!i:?i
& !i+1:?j
& !j^3:?cube2
& !cube2+-1*!cube1:?diff
& ( !diff^1/2:~(!diff^?)
| ( !count:~>!N
& !diff !cubans:?cubans
|
)
& ( !count:<!bigN
| !diff:?cube100k&~
)
& 1+!count:?count
)
& !cube2:?cube1
)
& ( columnwidth
= cols
. !arg:(%@:@(?:? [?cols)) ?
& div$(-1+!cols.3)*4+1
)
& columnwidth$!cubans:?colwidth
& \n:?table
& 0:?col
& ( format
= n col cif R
. !arg:(?n.?col)
& -1:?cif
& vap
$ ( (
=
. ( mod$(1+!cif:?cif.3):0
& -2+!col:?col
& ","
| -1+!col:?col&
)
!arg
)
. rev$!n
)
: "," ?R
& rev$(str$!R):?R
& whl
' ( !col+-1:?col:>-2
& " " !R:?R
)
& str$!R
)
& whl
' ( !cubans:%?cuban ?cubans
& mod$(1+!col:?col.10):?col
& (!col:0&\n|" ")
format$(!cuban.!colwidth)
!table
: ?table
)
& out$(str$("The first " !N " cuban primes are: " !table))
& out
$ ( str
$ ( "The 100,000th cuban prime is "
format$(!cube100k.columnwidth$!cube100k)
)
)
)
& cubanprimes$(200.100000)
)</syntaxhighlight>
{{out}}
<pre>The first 200 cuban primes are:
7 19 37 61 127 271 331 397 547 631
919 1,657 1,801 1,951 2,269 2,437 2,791 3,169 3,571 4,219
4,447 5,167 5,419 6,211 7,057 7,351 8,269 9,241 10,267 11,719
12,097 13,267 13,669 16,651 19,441 19,927 22,447 23,497 24,571 25,117
26,227 27,361 33,391 35,317 42,841 45,757 47,251 49,537 50,311 55,897
59,221 60,919 65,269 70,687 73,477 74,419 75,367 81,181 82,171 87,211
88,237 89,269 92,401 96,661 102,121 103,231 104,347 110,017 112,327 114,661
115,837 126,691 129,169 131,671 135,469 140,617 144,541 145,861 151,201 155,269
163,567 169,219 170,647 176,419 180,811 189,757 200,467 202,021 213,067 231,019
234,361 241,117 246,247 251,431 260,191 263,737 267,307 276,337 279,991 283,669
285,517 292,969 296,731 298,621 310,087 329,677 333,667 337,681 347,821 351,919
360,187 368,551 372,769 374,887 377,011 383,419 387,721 398,581 407,377 423,001
436,627 452,797 459,817 476,407 478,801 493,291 522,919 527,941 553,411 574,219
584,767 590,077 592,741 595,411 603,457 608,851 611,557 619,711 627,919 650,071
658,477 666,937 689,761 692,641 698,419 707,131 733,591 742,519 760,537 769,627
772,669 784,897 791,047 812,761 825,301 837,937 847,477 863,497 879,667 886,177
895,987 909,151 915,769 925,741 929,077 932,419 939,121 952,597 972,991 976,411
986,707 990,151 997,057 1,021,417 1,024,921 1,035,469 1,074,607 1,085,407 1,110,817 1,114,471
1,125,469 1,155,061 1,177,507 1,181,269 1,215,397 1,253,887 1,281,187 1,285,111 1,324,681 1,328,671
1,372,957 1,409,731 1,422,097 1,426,231 1,442,827 1,451,161 1,480,519 1,484,737 1,527,247 1,570,357
 
The 100,000th cuban prime is 1,792,617,147,127</pre>
 
=={{header|C}}==
{{trans|C++}}
<syntaxhighlight lang="c">#include <limits.h>
#include <math.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
 
typedef long long llong_t;
struct PrimeArray {
llong_t *ptr;
size_t size;
size_t capacity;
};
 
struct PrimeArray allocate() {
struct PrimeArray primes;
 
primes.size = 0;
primes.capacity = 10;
primes.ptr = malloc(primes.capacity * sizeof(llong_t));
 
return primes;
}
 
void deallocate(struct PrimeArray *primes) {
free(primes->ptr);
primes->ptr = NULL;
}
 
void push_back(struct PrimeArray *primes, llong_t p) {
if (primes->size >= primes->capacity) {
size_t new_capacity = (3 * primes->capacity) / 2 + 1;
llong_t *temp = realloc(primes->ptr, new_capacity * sizeof(llong_t));
if (NULL == temp) {
fprintf(stderr, "Failed to reallocate the prime array.");
exit(1);
} else {
primes->ptr = temp;
primes->capacity = new_capacity;
}
}
 
primes->ptr[primes->size++] = p;
}
 
int main() {
const int cutOff = 200, bigUn = 100000, chunks = 50, little = bigUn / chunks;
struct PrimeArray primes = allocate();
int c = 0;
bool showEach = true;
llong_t u = 0, v = 1, i;
 
push_back(&primes, 3);
push_back(&primes, 5);
 
printf("The first %d cuban primes:\n", cutOff);
for (i = 1; i < LLONG_MAX; ++i) {
bool found = false;
llong_t mx = ceil(sqrt(v += (u += 6)));
llong_t j;
 
for (j = 0; j < primes.size; ++j) {
if (primes.ptr[j] > mx) {
break;
}
if (v % primes.ptr[j] == 0) {
found = true;
break;
}
}
if (!found) {
c += 1;
if (showEach) {
llong_t z;
for (z = primes.ptr[primes.size - 1] + 2; z <= v - 2; z += 2) {
bool fnd = false;
 
for (j = 0; j < primes.size; ++j) {
if (primes.ptr[j] > mx) {
break;
}
if (z % primes.ptr[j] == 0) {
fnd = true;
break;
}
}
if (!fnd) {
push_back(&primes, z);
}
}
push_back(&primes, v);
printf("%11lld", v);
if (c % 10 == 0) {
printf("\n");
}
if (c == cutOff) {
showEach = false;
printf("\nProgress to the %dth cuban prime: ", bigUn);
}
}
if (c % little == 0) {
printf(".");
if (c == bigUn) {
break;
}
}
}
}
printf("\nThe %dth cuban prime is %lld\n", c, v);
 
deallocate(&primes);
return 0;
}</syntaxhighlight>
{{out}}
<pre>The first 200 cuban primes:
7 19 37 61 127 271 331 397 547 631
919 1657 1801 1951 2269 2437 2791 3169 3571 4219
4447 5167 5419 6211 7057 7351 8269 9241 10267 11719
12097 13267 13669 16651 19441 19927 22447 23497 24571 25117
26227 27361 33391 35317 42841 45757 47251 49537 50311 55897
59221 60919 65269 70687 73477 74419 75367 81181 82171 87211
88237 89269 92401 96661 102121 103231 104347 110017 112327 114661
115837 126691 129169 131671 135469 140617 144541 145861 151201 155269
163567 169219 170647 176419 180811 189757 200467 202021 213067 231019
234361 241117 246247 251431 260191 263737 267307 276337 279991 283669
285517 292969 296731 298621 310087 329677 333667 337681 347821 351919
360187 368551 372769 374887 377011 383419 387721 398581 407377 423001
436627 452797 459817 476407 478801 493291 522919 527941 553411 574219
584767 590077 592741 595411 603457 608851 611557 619711 627919 650071
658477 666937 689761 692641 698419 707131 733591 742519 760537 769627
772669 784897 791047 812761 825301 837937 847477 863497 879667 886177
895987 909151 915769 925741 929077 932419 939121 952597 972991 976411
986707 990151 997057 1021417 1024921 1035469 1074607 1085407 1110817 1114471
1125469 1155061 1177507 1181269 1215397 1253887 1281187 1285111 1324681 1328671
1372957 1409731 1422097 1426231 1442827 1451161 1480519 1484737 1527247 1570357
 
Progress to the 100000th cuban prime: ..................................................
The 100000th cuban prime is 1792617147127</pre>
 
===using GMP===
So that we can go arbitrarily large, and it's faster to boot.
<syntaxhighlight lang="c">#include <gmp.h>
#include <stdio.h>
 
typedef unsigned long int uint;
 
int main(void)
{
mpz_t a, b;
mpz_init(a);
mpz_init(b);
 
int found = 0;
int col = 0;
for (uint n = 1; ; n++) {
mpz_ui_pow_ui(a, n, 3);
mpz_ui_pow_ui(b, n + 1, 3);
mpz_sub(a, b, a);
 
if (!mpz_probab_prime_p(a, 5)) continue;
 
if (++found <= 200) {
gmp_printf("%10Zu", a);
if (++col == 8) {
putchar('\n');
col = 0;
}
} else if (found == 100000) {
gmp_printf("100000th: %Zu\n", a);
} else if (found == 1000000) {
gmp_printf("1000000th: %Zu\n", a);
break;
}
}
return 0;
}
</syntaxhighlight>
 
=={{header|C sharp|C#}}==
{{trans|Visual Basic .NET}}
(of the Snail Version)
<langsyntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
using System.Linq;
Line 284 ⟶ 838:
if (System.Diagnostics.Debugger.IsAttached) Console.ReadKey();
}
}</langsyntaxhighlight>
{{out}}
<pre>The first 200 cuban primes:
Line 311 ⟶ 865:
The 100,000th cuban prime is 1,792,617,147,127
Computation time was 63.578673 seconds</pre>
 
=={{header|C++}}==
{{trans|C#}}
<syntaxhighlight lang="cpp">#include <iostream>
#include <vector>
#include <chrono>
#include <climits>
#include <cmath>
 
using namespace std;
 
vector <long long> primes{ 3, 5 };
 
int main()
{
cout.imbue(locale(""));
const int cutOff = 200, bigUn = 100000,
chunks = 50, little = bigUn / chunks;
const char tn[] = " cuban prime";
cout << "The first " << cutOff << tn << "s:" << endl;
int c = 0;
bool showEach = true;
long long u = 0, v = 1;
auto st = chrono::system_clock::now();
 
for (long long i = 1; i <= LLONG_MAX; i++)
{
bool found = false;
long long mx = (long long)(ceil(sqrt(v += (u += 6))));
for (long long item : primes)
{
if (item > mx) break;
if (v % item == 0) { found = true; break; }
}
if (!found)
{
c += 1; if (showEach)
{
for (long long z = primes.back() + 2; z <= v - 2; z += 2)
{
bool fnd = false;
for (long long item : primes)
{
if (item > mx) break;
if (z % item == 0) { fnd = true; break; }
}
if (!fnd) primes.push_back(z);
}
primes.push_back(v); cout.width(11); cout << v;
if (c % 10 == 0) cout << endl;
if (c == cutOff)
{
showEach = false;
cout << "\nProgress to the " << bigUn << "th" << tn << ": ";
}
}
if (c % little == 0) { cout << "."; if (c == bigUn) break; }
}
}
cout << "\nThe " << c << "th" << tn << " is " << v;
chrono::duration<double> elapsed_seconds = chrono::system_clock::now() - st;
cout << "\nComputation time was " << elapsed_seconds.count() << " seconds" << endl;
return 0;
}</syntaxhighlight>
{{out}}
<pre>The first 200 cuban primes:
7 19 37 61 127 271 331 397 547 631
919 1,657 1,801 1,951 2,269 2,437 2,791 3,169 3,571 4,219
4,447 5,167 5,419 6,211 7,057 7,351 8,269 9,241 10,267 11,719
12,097 13,267 13,669 16,651 19,441 19,927 22,447 23,497 24,571 25,117
26,227 27,361 33,391 35,317 42,841 45,757 47,251 49,537 50,311 55,897
59,221 60,919 65,269 70,687 73,477 74,419 75,367 81,181 82,171 87,211
88,237 89,269 92,401 96,661 102,121 103,231 104,347 110,017 112,327 114,661
115,837 126,691 129,169 131,671 135,469 140,617 144,541 145,861 151,201 155,269
163,567 169,219 170,647 176,419 180,811 189,757 200,467 202,021 213,067 231,019
234,361 241,117 246,247 251,431 260,191 263,737 267,307 276,337 279,991 283,669
285,517 292,969 296,731 298,621 310,087 329,677 333,667 337,681 347,821 351,919
360,187 368,551 372,769 374,887 377,011 383,419 387,721 398,581 407,377 423,001
436,627 452,797 459,817 476,407 478,801 493,291 522,919 527,941 553,411 574,219
584,767 590,077 592,741 595,411 603,457 608,851 611,557 619,711 627,919 650,071
658,477 666,937 689,761 692,641 698,419 707,131 733,591 742,519 760,537 769,627
772,669 784,897 791,047 812,761 825,301 837,937 847,477 863,497 879,667 886,177
895,987 909,151 915,769 925,741 929,077 932,419 939,121 952,597 972,991 976,411
986,707 990,151 997,057 1,021,417 1,024,921 1,035,469 1,074,607 1,085,407 1,110,817 1,114,471
1,125,469 1,155,061 1,177,507 1,181,269 1,215,397 1,253,887 1,281,187 1,285,111 1,324,681 1,328,671
1,372,957 1,409,731 1,422,097 1,426,231 1,442,827 1,451,161 1,480,519 1,484,737 1,527,247 1,570,357
 
Progress to the 100,000th cuban prime: ..................................................
The 100,000th cuban prime is 1,792,617,147,127
Computation time was 35.5644 seconds</pre>
 
=={{header|Common Lisp}}==
{{works with|sbcl|1.1.14.debian}}
{{works with|clisp|2.49}}
 
For some reason, this solution is very slow: it takes about 10 minutes to find the 100,000th
cuban number on sbcl and longer than I had the patience to wait on clisp.
I thought this was due to my own <code>primep</code> function, so I tried to use
<code>cl-primality:primep</code> from quicklisp. To my surprise, however, that was taking enen longer, so I went back to my own function. On the other hand, Common Lisp makes it a breeze to format the output
with multiple columns, commas and all.
 
<syntaxhighlight lang="lisp">;;; Show the first 200 and the 100,000th cuban prime.
;;; Cuban primes are the difference of 2 consecutive cubes.
 
(defun primep (n)
(cond ((< n 4) t)
((evenp n) nil)
((zerop (mod n 3)) nil)
(t (loop for i from 5 upto (isqrt n) by 6
when (or
(zerop (mod n i))
(zerop (mod n (+ i 2))))
return nil
finally (return t)))))
 
(defun cube (n) (* n n n))
 
(defun cuban (n)
(loop for i from 1
for j from 2
for cube-diff = (- (cube j) (cube i))
when (primep cube-diff)
collect cube-diff into cuban-primes
and count i into counter
when (= counter n)
return cuban-primes))
 
 
(format t "~a~%" "1st to 200th cuban prime numbers:")
(format t
"~{~<~%~,120:;~10:d ~>~}~%"
(cuban 200))
 
 
(format t "~%100,000th cuban prime number = ~:d"
(car (last (cuban 100000))))
 
(princ #\newline)</syntaxhighlight>
{{out}}
<pre>
1st to 200th cuban prime numbers:
7 19 37 61 127 271 331 397 547 631
919 1,657 1,801 1,951 2,269 2,437 2,791 3,169 3,571 4,219
4,447 5,167 5,419 6,211 7,057 7,351 8,269 9,241 10,267 11,719
12,097 13,267 13,669 16,651 19,441 19,927 22,447 23,497 24,571 25,117
26,227 27,361 33,391 35,317 42,841 45,757 47,251 49,537 50,311 55,897
59,221 60,919 65,269 70,687 73,477 74,419 75,367 81,181 82,171 87,211
88,237 89,269 92,401 96,661 102,121 103,231 104,347 110,017 112,327 114,661
115,837 126,691 129,169 131,671 135,469 140,617 144,541 145,861 151,201 155,269
163,567 169,219 170,647 176,419 180,811 189,757 200,467 202,021 213,067 231,019
234,361 241,117 246,247 251,431 260,191 263,737 267,307 276,337 279,991 283,669
285,517 292,969 296,731 298,621 310,087 329,677 333,667 337,681 347,821 351,919
360,187 368,551 372,769 374,887 377,011 383,419 387,721 398,581 407,377 423,001
436,627 452,797 459,817 476,407 478,801 493,291 522,919 527,941 553,411 574,219
584,767 590,077 592,741 595,411 603,457 608,851 611,557 619,711 627,919 650,071
658,477 666,937 689,761 692,641 698,419 707,131 733,591 742,519 760,537 769,627
772,669 784,897 791,047 812,761 825,301 837,937 847,477 863,497 879,667 886,177
895,987 909,151 915,769 925,741 929,077 932,419 939,121 952,597 972,991 976,411
986,707 990,151 997,057 1,021,417 1,024,921 1,035,469 1,074,607 1,085,407 1,110,817 1,114,471
1,125,469 1,155,061 1,177,507 1,181,269 1,215,397 1,253,887 1,281,187 1,285,111 1,324,681 1,328,671
1,372,957 1,409,731 1,422,097 1,426,231 1,442,827 1,451,161 1,480,519 1,484,737 1,527,247 1,570,357
 
100,000th cuban prime number = 1,792,617,147,127
</pre>
 
=={{header|D}}==
{{trans|C#}}
<syntaxhighlight lang="d">import std.math;
import std.stdio;
 
void main() {
long[] primes = [3, 5];
 
immutable cutOff = 200;
immutable bigUn = 100_000;
immutable chunks = 50;
immutable little = bigUn / chunks;
immutable tn = " cuban prime";
writefln("The first %s%ss:", cutOff, tn);
int c;
bool showEach = true;
long u;
long v = 1;
for (long i = 1; i > 0; ++i) {
bool found;
u += 6;
v += u;
int mx = cast(int)ceil(sqrt(cast(real)v));
foreach (item; primes) {
if (item > mx) break;
if (v % item == 0) {
found = true;
break;
}
}
if (!found) {
c++;
if (showEach) {
for (auto z = primes[$-1] + 2; z <= v - 2; z += 2) {
bool fnd;
foreach (item; primes) {
if (item > mx) break;
if (z % item == 0) {
fnd = true;
break;
}
}
if (!fnd) {
primes ~= z;
}
}
primes ~= v;
writef("%11d", v);
if (c % 10 == 0) writeln;
if (c == cutOff) {
showEach = false;
writef("\nProgress to the %sth%s: ", bigUn, tn);
}
}
if (c % little == 0) {
write('.');
if (c == bigUn) {
break;
}
}
}
}
writefln("\nThe %sth%s is %17s", c, tn, v);
}</syntaxhighlight>
{{out}}
<pre>The first 200 cuban primes:
7 19 37 61 127 271 331 397 547 631
919 1657 1801 1951 2269 2437 2791 3169 3571 4219
4447 5167 5419 6211 7057 7351 8269 9241 10267 11719
12097 13267 13669 16651 19441 19927 22447 23497 24571 25117
26227 27361 33391 35317 42841 45757 47251 49537 50311 55897
59221 60919 65269 70687 73477 74419 75367 81181 82171 87211
88237 89269 92401 96661 102121 103231 104347 110017 112327 114661
115837 126691 129169 131671 135469 140617 144541 145861 151201 155269
163567 169219 170647 176419 180811 189757 200467 202021 213067 231019
234361 241117 246247 251431 260191 263737 267307 276337 279991 283669
285517 292969 296731 298621 310087 329677 333667 337681 347821 351919
360187 368551 372769 374887 377011 383419 387721 398581 407377 423001
436627 452797 459817 476407 478801 493291 522919 527941 553411 574219
584767 590077 592741 595411 603457 608851 611557 619711 627919 650071
658477 666937 689761 692641 698419 707131 733591 742519 760537 769627
772669 784897 791047 812761 825301 837937 847477 863497 879667 886177
895987 909151 915769 925741 929077 932419 939121 952597 972991 976411
986707 990151 997057 1021417 1024921 1035469 1074607 1085407 1110817 1114471
1125469 1155061 1177507 1181269 1215397 1253887 1281187 1285111 1324681 1328671
1372957 1409731 1422097 1426231 1442827 1451161 1480519 1484737 1527247 1570357
 
Progress to the 100000th cuban prime: ..................................................
The 100000th cuban prime is 1792617147127</pre>
=={{header|Delphi}}==
See [https://www.rosettacode.org/wiki/Cuban_primes#Pascal Pascal].
 
=={{header|EasyLang}}==
<syntaxhighlight>
fastfunc isprim_odd num .
i = 3
while i <= sqrt num
if num mod i = 0
return 0
.
i += 2
.
return 1
.
numfmt 0 7
i = 1
while cnt < 100000
di = 3 * i * (i + 1) + 1
if isprim_odd di = 1
cnt += 1
if cnt <= 200
write di & " "
if cnt mod 5 = 0
print ""
.
.
.
i += 1
.
print ""
print di
</syntaxhighlight>
 
{{out}}
<pre>
7 19 37 61 127 271 331 397
547 631 919 1657 1801 1951 2269 2437
2791 3169 3571 4219 4447 5167 5419 6211
7057 7351 8269 9241 10267 11719 12097 13267
13669 16651 19441 19927 22447 23497 24571 25117
26227 27361 33391 35317 42841 45757 47251 49537
50311 55897 59221 60919 65269 70687 73477 74419
75367 81181 82171 87211 88237 89269 92401 96661
102121 103231 104347 110017 112327 114661 115837 126691
129169 131671 135469 140617 144541 145861 151201 155269
163567 169219 170647 176419 180811 189757 200467 202021
213067 231019 234361 241117 246247 251431 260191 263737
267307 276337 279991 283669 285517 292969 296731 298621
310087 329677 333667 337681 347821 351919 360187 368551
372769 374887 377011 383419 387721 398581 407377 423001
436627 452797 459817 476407 478801 493291 522919 527941
553411 574219 584767 590077 592741 595411 603457 608851
611557 619711 627919 650071 658477 666937 689761 692641
698419 707131 733591 742519 760537 769627 772669 784897
791047 812761 825301 837937 847477 863497 879667 886177
895987 909151 915769 925741 929077 932419 939121 952597
972991 976411 986707 990151 997057 1021417 1024921 1035469
1074607 1085407 1110817 1114471 1125469 1155061 1177507 1181269
1215397 1253887 1281187 1285111 1324681 1328671 1372957 1409731
1422097 1426231 1442827 1451161 1480519 1484737 1527247 1570357
 
1792617147127
</pre>
 
=={{header|F_Sharp|F#}}==
===The functions===
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_function Extensible Prime Generator (F#)]
<syntaxhighlight lang="fsharp">
// Generate cuban primes. Nigel Galloway: June 9th., 2019
let cubans=Seq.unfold(fun n->Some(n*n*n,n+1L)) 1L|>Seq.pairwise|>Seq.map(fun(n,g)->g-n)|>Seq.filter(isPrime64)
let cL=let g=System.Globalization.CultureInfo("en-GB") in (fun (n:int64)->n.ToString("N0",g))
</syntaxhighlight>
===The Task===
<syntaxhighlight lang="fsharp">
cubans|>Seq.take 200|>List.ofSeq|>List.iteri(fun n g->if n%8=7 then printfn "%12s" (cL(g)) else printf "%12s" (cL(g)))
</syntaxhighlight>
{{out}}
<pre>
7 19 37 61 127 271 331 397
547 631 919 1,657 1,801 1,951 2,269 2,437
2,791 3,169 3,571 4,219 4,447 5,167 5,419 6,211
7,057 7,351 8,269 9,241 10,267 11,719 12,097 13,267
13,669 16,651 19,441 19,927 22,447 23,497 24,571 25,117
26,227 27,361 33,391 35,317 42,841 45,757 47,251 49,537
50,311 55,897 59,221 60,919 65,269 70,687 73,477 74,419
75,367 81,181 82,171 87,211 88,237 89,269 92,401 96,661
102,121 103,231 104,347 110,017 112,327 114,661 115,837 126,691
129,169 131,671 135,469 140,617 144,541 145,861 151,201 155,269
163,567 169,219 170,647 176,419 180,811 189,757 200,467 202,021
213,067 231,019 234,361 241,117 246,247 251,431 260,191 263,737
267,307 276,337 279,991 283,669 285,517 292,969 296,731 298,621
310,087 329,677 333,667 337,681 347,821 351,919 360,187 368,551
372,769 374,887 377,011 383,419 387,721 398,581 407,377 423,001
436,627 452,797 459,817 476,407 478,801 493,291 522,919 527,941
553,411 574,219 584,767 590,077 592,741 595,411 603,457 608,851
611,557 619,711 627,919 650,071 658,477 666,937 689,761 692,641
698,419 707,131 733,591 742,519 760,537 769,627 772,669 784,897
791,047 812,761 825,301 837,937 847,477 863,497 879,667 886,177
895,987 909,151 915,769 925,741 929,077 932,419 939,121 952,597
972,991 976,411 986,707 990,151 997,057 1,021,417 1,024,921 1,035,469
1,074,607 1,085,407 1,110,817 1,114,471 1,125,469 1,155,061 1,177,507 1,181,269
1,215,397 1,253,887 1,281,187 1,285,111 1,324,681 1,328,671 1,372,957 1,409,731
1,422,097 1,426,231 1,442,827 1,451,161 1,480,519 1,484,737 1,527,247 1,570,357
</pre>
<syntaxhighlight lang="fsharp">
printfn "\n\n%s" (cL(Seq.item 99999 cubans))
</syntaxhighlight>
{{out}}
<pre>
1,792,617,147,127
</pre>
 
=={{header|Factor}}==
{{trans|Sidef}}
<langsyntaxhighlight lang="factor">USING: formatting grouping io kernel lists lists.lazy math
math.primes sequences tools.memory.private ;
IN: rosetta-code.cuban-primes
Line 326 ⟶ 1,246:
 
1e5 cuban-primes last commas "100,000th cuban prime is: %s\n"
printf</langsyntaxhighlight>
{{out}}
<pre>
Line 351 ⟶ 1,271:
 
100,000th cuban prime is: 1,792,617,147,127
</pre>
 
=={{header|Forth}}==
Uses [https://rosettacode.org/wiki/Miller%E2%80%93Rabin_primality_test#Forth Miller Rabin Primality Test]
<syntaxhighlight lang="forth">
include ./miller-rabin.fs
 
\ commatized print
\
: d.,r ( d n -- ) \ write double precision int, commatized.
>r tuck dabs
<# begin 2dup 1.000 d> while # # # [char] , hold repeat #s rot sign #>
r> over - spaces type ;
 
: .,r ( n1 n2 -- ) \ write integer commatized.
>r s>d r> d.,r ;
 
 
\ generate and print cuban primes
\
: sq s" dup *" evaluate ; immediate
: next-cuban ( n -- n' p )
begin
1+ dup sq 3 * 1+ dup 3 and 0= \ first check == 0 (mod 4)
if 2 rshift dup prime?
if exit
else drop
then
else drop
then
again ;
 
: task1
1
20 0 do
cr 10 0 do
next-cuban 12 .,r
loop
loop drop ;
 
: task2
cr ." The 100,000th cuban prime is "
1 99999 0 do next-cuban drop loop next-cuban 0 .,r drop ;
 
 
task1 cr
task2 cr
bye
</syntaxhighlight>
{{Out}}
<pre>
7 19 37 61 127 271 331 397 547 631
919 1,657 1,801 1,951 2,269 2,437 2,791 3,169 3,571 4,219
4,447 5,167 5,419 6,211 7,057 7,351 8,269 9,241 10,267 11,719
12,097 13,267 13,669 16,651 19,441 19,927 22,447 23,497 24,571 25,117
26,227 27,361 33,391 35,317 42,841 45,757 47,251 49,537 50,311 55,897
59,221 60,919 65,269 70,687 73,477 74,419 75,367 81,181 82,171 87,211
88,237 89,269 92,401 96,661 102,121 103,231 104,347 110,017 112,327 114,661
115,837 126,691 129,169 131,671 135,469 140,617 144,541 145,861 151,201 155,269
163,567 169,219 170,647 176,419 180,811 189,757 200,467 202,021 213,067 231,019
234,361 241,117 246,247 251,431 260,191 263,737 267,307 276,337 279,991 283,669
285,517 292,969 296,731 298,621 310,087 329,677 333,667 337,681 347,821 351,919
360,187 368,551 372,769 374,887 377,011 383,419 387,721 398,581 407,377 423,001
436,627 452,797 459,817 476,407 478,801 493,291 522,919 527,941 553,411 574,219
584,767 590,077 592,741 595,411 603,457 608,851 611,557 619,711 627,919 650,071
658,477 666,937 689,761 692,641 698,419 707,131 733,591 742,519 760,537 769,627
772,669 784,897 791,047 812,761 825,301 837,937 847,477 863,497 879,667 886,177
895,987 909,151 915,769 925,741 929,077 932,419 939,121 952,597 972,991 976,411
986,707 990,151 997,057 1,021,417 1,024,921 1,035,469 1,074,607 1,085,407 1,110,817 1,114,471
1,125,469 1,155,061 1,177,507 1,181,269 1,215,397 1,253,887 1,281,187 1,285,111 1,324,681 1,328,671
1,372,957 1,409,731 1,422,097 1,426,231 1,442,827 1,451,161 1,480,519 1,484,737 1,527,247 1,570,357
 
The 100,000th cuban prime is 1,792,617,147,127
</pre>
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 399 ⟶ 1,392:
}
fmt.Println("\nThe 100,000th cuban prime is", commatize(cube100k))
}</langsyntaxhighlight>
 
{{out}}
Line 428 ⟶ 1,421:
</pre>
 
=={{header|Groovy}}==
{{trans|Java}}
<syntaxhighlight lang="groovy">class CubanPrimes {
private static int MAX = 1_400_000
private static boolean[] primes = new boolean[MAX]
 
static void main(String[] args) {
preCompute()
cubanPrime(200, true)
for (int i = 1; i <= 5; i++) {
int max = (int) Math.pow(10, i)
printf("%,d-th cuban prime = %,d%n", max, cubanPrime(max, false))
}
}
 
private static long cubanPrime(int n, boolean display) {
int count = 0
long result = 0
for (long i = 0; count < n; i++) {
long test = 1l + 3 * i * (i + 1)
if (isPrime(test)) {
count++
result = test
if (display) {
printf("%10s%s", String.format("%,d", test), count % 10 == 0 ? "\n" : "")
}
}
}
return result
}
 
private static boolean isPrime(long n) {
if (n < MAX) {
return primes[(int) n]
}
int max = (int) Math.sqrt(n)
for (int i = 3; i <= max; i++) {
if (primes[i] && n % i == 0) {
return false
}
}
return true
}
 
private static final void preCompute() {
// primes
for (int i = 2; i < MAX; i++) {
primes[i] = true
}
for (int i = 2; i < MAX; i++) {
if (primes[i]) {
for (int j = 2 * i; j < MAX; j += i) {
primes[j] = false
}
}
}
}
}</syntaxhighlight>
{{out}}
<pre> 7 19 37 61 127 271 331 397 547 631
919 1,657 1,801 1,951 2,269 2,437 2,791 3,169 3,571 4,219
4,447 5,167 5,419 6,211 7,057 7,351 8,269 9,241 10,267 11,719
12,097 13,267 13,669 16,651 19,441 19,927 22,447 23,497 24,571 25,117
26,227 27,361 33,391 35,317 42,841 45,757 47,251 49,537 50,311 55,897
59,221 60,919 65,269 70,687 73,477 74,419 75,367 81,181 82,171 87,211
88,237 89,269 92,401 96,661 102,121 103,231 104,347 110,017 112,327 114,661
115,837 126,691 129,169 131,671 135,469 140,617 144,541 145,861 151,201 155,269
163,567 169,219 170,647 176,419 180,811 189,757 200,467 202,021 213,067 231,019
234,361 241,117 246,247 251,431 260,191 263,737 267,307 276,337 279,991 283,669
285,517 292,969 296,731 298,621 310,087 329,677 333,667 337,681 347,821 351,919
360,187 368,551 372,769 374,887 377,011 383,419 387,721 398,581 407,377 423,001
436,627 452,797 459,817 476,407 478,801 493,291 522,919 527,941 553,411 574,219
584,767 590,077 592,741 595,411 603,457 608,851 611,557 619,711 627,919 650,071
658,477 666,937 689,761 692,641 698,419 707,131 733,591 742,519 760,537 769,627
772,669 784,897 791,047 812,761 825,301 837,937 847,477 863,497 879,667 886,177
895,987 909,151 915,769 925,741 929,077 932,419 939,121 952,597 972,991 976,411
986,707 990,151 997,057 1,021,417 1,024,921 1,035,469 1,074,607 1,085,407 1,110,817 1,114,471
1,125,469 1,155,061 1,177,507 1,181,269 1,215,397 1,253,887 1,281,187 1,285,111 1,324,681 1,328,671
1,372,957 1,409,731 1,422,097 1,426,231 1,442,827 1,451,161 1,480,519 1,484,737 1,527,247 1,570,357
10-th cuban prime = 631
100-th cuban prime = 283,669
1,000-th cuban prime = 65,524,807
10,000-th cuban prime = 11,712,188,419
100,000-th cuban prime = 1,792,617,147,127</pre>
 
=={{header|Haskell}}==
Uses Data.Numbers.Primes library: http://hackage.haskell.org/package/primes-0.2.1.0/docs/Data-Numbers-Primes.html<br>
Uses Data.List.Split library: https://hackage.haskell.org/package/split-0.2.3.4/docs/Data-List-Split.html
<syntaxhighlight lang="haskell">import Data.Numbers.Primes (isPrime)
import Data.List (intercalate)
import Data.List.Split (chunksOf)
import Text.Printf (printf)
 
cubans :: [Int]
cubans = filter isPrime . map (\x -> (succ x ^ 3) - (x ^ 3)) $ [1 ..]
 
main :: IO ()
main = do
mapM_ (\row -> mapM_ (printf "%10s" . thousands) row >> printf "\n") $ rows cubans
printf "\nThe 100,000th cuban prime is: %10s\n" $ thousands $ cubans !! 99999
where
rows = chunksOf 10 . take 200
thousands = reverse . intercalate "," . chunksOf 3 . reverse . show</syntaxhighlight>
 
Where filter over map could also be expressed in terms of concatMap, or a list comprehension:
 
<syntaxhighlight lang="haskell">cubans :: [Int]
cubans =
[ x
| n <- [1 ..]
, let x = (succ n ^ 3) - (n ^ 3)
, isPrime x ]</syntaxhighlight>
 
{{out}}
<pre>
7 19 37 61 127 271 331 397 547 631
919 1,657 1,801 1,951 2,269 2,437 2,791 3,169 3,571 4,219
4,447 5,167 5,419 6,211 7,057 7,351 8,269 9,241 10,267 11,719
12,097 13,267 13,669 16,651 19,441 19,927 22,447 23,497 24,571 25,117
26,227 27,361 33,391 35,317 42,841 45,757 47,251 49,537 50,311 55,897
59,221 60,919 65,269 70,687 73,477 74,419 75,367 81,181 82,171 87,211
88,237 89,269 92,401 96,661 102,121 103,231 104,347 110,017 112,327 114,661
115,837 126,691 129,169 131,671 135,469 140,617 144,541 145,861 151,201 155,269
163,567 169,219 170,647 176,419 180,811 189,757 200,467 202,021 213,067 231,019
234,361 241,117 246,247 251,431 260,191 263,737 267,307 276,337 279,991 283,669
285,517 292,969 296,731 298,621 310,087 329,677 333,667 337,681 347,821 351,919
360,187 368,551 372,769 374,887 377,011 383,419 387,721 398,581 407,377 423,001
436,627 452,797 459,817 476,407 478,801 493,291 522,919 527,941 553,411 574,219
584,767 590,077 592,741 595,411 603,457 608,851 611,557 619,711 627,919 650,071
658,477 666,937 689,761 692,641 698,419 707,131 733,591 742,519 760,537 769,627
772,669 784,897 791,047 812,761 825,301 837,937 847,477 863,497 879,667 886,177
895,987 909,151 915,769 925,741 929,077 932,419 939,121 952,597 972,991 976,411
986,707 990,151 997,057 1,021,417 1,024,921 1,035,469 1,074,607 1,085,407 1,110,817 1,114,471
1,125,469 1,155,061 1,177,507 1,181,269 1,215,397 1,253,887 1,281,187 1,285,111 1,324,681 1,328,671
1,372,957 1,409,731 1,422,097 1,426,231 1,442,827 1,451,161 1,480,519 1,484,737 1,527,247 1,570,357
 
The 100,000th cuban prime is: 1,792,617,147,127
</pre>
 
=={{header|J}}==
I've used assertions to demonstrate and to prove the defined verbs
<syntaxhighlight lang="j">
<lang j>
 
isPrime =: 1&p:
Line 494 ⟶ 1,625:
[: (#~ 1&p:) (-&(^&3)~ >:)
</syntaxhighlight>
</lang>
 
=={{header|Java}}==
<syntaxhighlight lang="java">
public class CubanPrimes {
 
private static int MAX = 1_400_000;
private static boolean[] primes = new boolean[MAX];
public static void main(String[] args) {
preCompute();
cubanPrime(200, true);
for ( int i = 1 ; i <= 5 ; i++ ) {
int max = (int) Math.pow(10, i);
System.out.printf("%,d-th cuban prime = %,d%n", max, cubanPrime(max, false));
}
}
private static long cubanPrime(int n, boolean display) {
int count = 0;
long result = 0;
for ( long i = 0 ; count < n ; i++ ) {
long test = 1l + 3 * i * (i+1);
if ( isPrime(test) ) {
count++;
result = test;
if ( display ) {
System.out.printf("%10s%s", String.format("%,d", test), count % 10 == 0 ? "\n" : "");
}
}
}
return result;
}
private static boolean isPrime(long n) {
if ( n < MAX ) {
return primes[(int)n];
}
int max = (int) Math.sqrt(n);
for ( int i = 3 ; i <= max ; i++ ) {
if ( primes[i] && n % i == 0 ) {
return false;
}
}
return true;
}
 
private static final void preCompute() {
// primes
for ( int i = 2 ; i < MAX ; i++ ) {
primes[i] = true;
}
for ( int i = 2 ; i < MAX ; i++ ) {
if ( primes[i] ) {
for ( int j = 2*i ; j < MAX ; j += i ) {
primes[j] = false;
}
}
}
}
}
</syntaxhighlight>
{{out}}
<pre>
7 19 37 61 127 271 331 397 547 631
919 1,657 1,801 1,951 2,269 2,437 2,791 3,169 3,571 4,219
4,447 5,167 5,419 6,211 7,057 7,351 8,269 9,241 10,267 11,719
12,097 13,267 13,669 16,651 19,441 19,927 22,447 23,497 24,571 25,117
26,227 27,361 33,391 35,317 42,841 45,757 47,251 49,537 50,311 55,897
59,221 60,919 65,269 70,687 73,477 74,419 75,367 81,181 82,171 87,211
88,237 89,269 92,401 96,661 102,121 103,231 104,347 110,017 112,327 114,661
115,837 126,691 129,169 131,671 135,469 140,617 144,541 145,861 151,201 155,269
163,567 169,219 170,647 176,419 180,811 189,757 200,467 202,021 213,067 231,019
234,361 241,117 246,247 251,431 260,191 263,737 267,307 276,337 279,991 283,669
285,517 292,969 296,731 298,621 310,087 329,677 333,667 337,681 347,821 351,919
360,187 368,551 372,769 374,887 377,011 383,419 387,721 398,581 407,377 423,001
436,627 452,797 459,817 476,407 478,801 493,291 522,919 527,941 553,411 574,219
584,767 590,077 592,741 595,411 603,457 608,851 611,557 619,711 627,919 650,071
658,477 666,937 689,761 692,641 698,419 707,131 733,591 742,519 760,537 769,627
772,669 784,897 791,047 812,761 825,301 837,937 847,477 863,497 879,667 886,177
895,987 909,151 915,769 925,741 929,077 932,419 939,121 952,597 972,991 976,411
986,707 990,151 997,057 1,021,417 1,024,921 1,035,469 1,074,607 1,085,407 1,110,817 1,114,471
1,125,469 1,155,061 1,177,507 1,181,269 1,215,397 1,253,887 1,281,187 1,285,111 1,324,681 1,328,671
1,372,957 1,409,731 1,422,097 1,426,231 1,442,827 1,451,161 1,480,519 1,484,737 1,527,247 1,570,357
10-th cuban prime = 631
100-th cuban prime = 283,669
1,000-th cuban prime = 65,524,807
10,000-th cuban prime = 11,712,188,419
100,000-th cuban prime = 1,792,617,147,127
</pre>
 
=={{header|jq}}==
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
 
In this entry, the formula for the differences of cubes (2n(n+1)+1) is used,
in part for efficiency and in part because the C implementation of jq
has an upper bound of 2^53 for accurate integer arithmetic.
 
For a suitable implementation of `is_prime` see e.g. [[Erd%C5%91s-primes#jq]].
 
'''Preliminaries'''
<syntaxhighlight lang="jq"># input should be a non-negative integer
def commatize:
def digits: tostring | explode | reverse;
[foreach digits[] as $d (-1; .+1;
(select(. > 0 and . % 3 == 0)|44), $d)] # "," is 44
| reverse | implode ;
 
def count(stream): reduce stream as $i (0; .+1);
 
def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .;
 
def nwise($n):
def n: if length <= $n then . else .[0:$n] , (.[$n:] | n) end;
n;</syntaxhighlight>
'''The tasks'''
<syntaxhighlight lang="jq">
# Emit an unbounded stream
# The differences between successive cubes: 3n(n+1)+1
def cubanprimes:
foreach range(1;infinite) as $i (null;
(3 * $i * ($i + 1) + 1) as $d
| if $d|is_prime then $d else null end;
select(.) );
 
(200
| "The first \(.) cuban primes are:",
([limit(.; cubanprimes) | commatize | lpad(10)] | nwise(10) | join(" "))),
 
"\nThe 100,000th cuban prime is \(nth(100000 - 1; cubanprimes) | commatize)"</syntaxhighlight>
{{out}}
<pre>
The first 200 cuban primes are:
7 19 37 61 127 271 331 397 547 631
919 1,657 1,801 1,951 2,269 2,437 2,791 3,169 3,571 4,219
4,447 5,167 5,419 6,211 7,057 7,351 8,269 9,241 10,267 11,719
12,097 13,267 13,669 16,651 19,441 19,927 22,447 23,497 24,571 25,117
26,227 27,361 33,391 35,317 42,841 45,757 47,251 49,537 50,311 55,897
59,221 60,919 65,269 70,687 73,477 74,419 75,367 81,181 82,171 87,211
88,237 89,269 92,401 96,661 102,121 103,231 104,347 110,017 112,327 114,661
115,837 126,691 129,169 131,671 135,469 140,617 144,541 145,861 151,201 155,269
163,567 169,219 170,647 176,419 180,811 189,757 200,467 202,021 213,067 231,019
234,361 241,117 246,247 251,431 260,191 263,737 267,307 276,337 279,991 283,669
285,517 292,969 296,731 298,621 310,087 329,677 333,667 337,681 347,821 351,919
360,187 368,551 372,769 374,887 377,011 383,419 387,721 398,581 407,377 423,001
436,627 452,797 459,817 476,407 478,801 493,291 522,919 527,941 553,411 574,219
584,767 590,077 592,741 595,411 603,457 608,851 611,557 619,711 627,919 650,071
658,477 666,937 689,761 692,641 698,419 707,131 733,591 742,519 760,537 769,627
772,669 784,897 791,047 812,761 825,301 837,937 847,477 863,497 879,667 886,177
895,987 909,151 915,769 925,741 929,077 932,419 939,121 952,597 972,991 976,411
986,707 990,151 997,057 1,021,417 1,024,921 1,035,469 1,074,607 1,085,407 1,110,817 1,114,471
1,125,469 1,155,061 1,177,507 1,181,269 1,215,397 1,253,887 1,281,187 1,285,111 1,324,681 1,328,671
1,372,957 1,409,731 1,422,097 1,426,231 1,442,827 1,451,161 1,480,519 1,484,737 1,527,247 1,570,357
 
The 100,000th cuban prime is 1,792,617,147,127
</pre>
 
 
=={{header|Julia}}==
 
{{trans|Go}}
{{works with|Julia|1.2}}
<lang julia>using Formatting, Primes
<syntaxhighlight lang="julia">using Primes
 
function cubanprimes(N)
cubans = zeros(BigIntInt, N)
cube100k, cube1, count = 0, 1, 1
cube1for =i BigIntin Iterators.countfrom(1)
count = 1
for i in 1:100000000
j = BigInt(i + 1)
cube2 = j^3
diff = cube2 - cube1
if isprime(diff)
ifcount ≤ N && (cubans[count] <= Ndiff)
cubans[count] = diff
end
if count == 100000
cube100k = diff
Line 524 ⟶ 1,809:
println("The first $N cuban primes are: ")
foreach(x -> print(lpad(cubans[x] == 0 ? "" : cubans[x], 10), x % 8 == 0 ? "\n" : ""), 1:N)
println("\nThe 100,000th cuban prime is ", format(cube100k, commas=true))
end
 
cubanprimes(200)
</langsyntaxhighlight>{{out}}
<pre>
The first 200 cuban primes are:
Line 559 ⟶ 1,844:
The 100,000th cuban prime is 1,792,617,147,127
</pre>
 
=={{header|Kotlin}}==
{{trans|D}}
<syntaxhighlight lang="scala">import kotlin.math.ceil
import kotlin.math.sqrt
 
fun main() {
val primes = mutableListOf(3L, 5L)
val cutOff = 200
val bigUn = 100_000
val chunks = 50
val little = bigUn / chunks
 
println("The first $cutOff cuban primes:")
var showEach = true
var c = 0
var u = 0L
var v = 1L
var i = 1L
while (i > 0) {
var found = false
u += 6
v += u
val mx = ceil(sqrt(v.toDouble())).toInt()
for (item in primes) {
if (item > mx) break
if (v % item == 0L) {
found = true
break
}
}
if (!found) {
c++
if (showEach) {
var z = primes.last() + 2
while (z <= v - 2) {
var fnd = false
for (item in primes) {
if (item > mx) break
if (z % item == 0L) {
fnd = true
break
}
}
if (!fnd) {
primes.add(z)
}
z += 2
}
primes.add(v)
print("%11d".format(v))
if (c % 10 == 0) println()
if (c == cutOff) {
showEach = false
print("\nProgress to the ${bigUn}th cuban prime: ")
}
}
if (c % little == 0) {
print(".")
if (c == bigUn) break
}
}
i++
}
println("\nThe %dth cuban prime is %17d".format(c, v))
}</syntaxhighlight>
{{out}}
<pre>The first 200 cuban primes:
7 19 37 61 127 271 331 397 547 631
919 1657 1801 1951 2269 2437 2791 3169 3571 4219
4447 5167 5419 6211 7057 7351 8269 9241 10267 11719
12097 13267 13669 16651 19441 19927 22447 23497 24571 25117
26227 27361 33391 35317 42841 45757 47251 49537 50311 55897
59221 60919 65269 70687 73477 74419 75367 81181 82171 87211
88237 89269 92401 96661 102121 103231 104347 110017 112327 114661
115837 126691 129169 131671 135469 140617 144541 145861 151201 155269
163567 169219 170647 176419 180811 189757 200467 202021 213067 231019
234361 241117 246247 251431 260191 263737 267307 276337 279991 283669
285517 292969 296731 298621 310087 329677 333667 337681 347821 351919
360187 368551 372769 374887 377011 383419 387721 398581 407377 423001
436627 452797 459817 476407 478801 493291 522919 527941 553411 574219
584767 590077 592741 595411 603457 608851 611557 619711 627919 650071
658477 666937 689761 692641 698419 707131 733591 742519 760537 769627
772669 784897 791047 812761 825301 837937 847477 863497 879667 886177
895987 909151 915769 925741 929077 932419 939121 952597 972991 976411
986707 990151 997057 1021417 1024921 1035469 1074607 1085407 1110817 1114471
1125469 1155061 1177507 1181269 1215397 1253887 1281187 1285111 1324681 1328671
1372957 1409731 1422097 1426231 1442827 1451161 1480519 1484737 1527247 1570357
 
Progress to the 100000th cuban prime: ..................................................
The 100000th cuban prime is 1792617147127</pre>
 
=={{header|Lua}}==
===Original===
{{trans|D}}
<syntaxhighlight lang="lua">local primes = {3, 5}
local cutOff = 200
local bigUn = 100000
local chunks = 50
local little = math.floor(bigUn / chunks)
local tn = " cuban prime"
print(string.format("The first %d%ss", cutOff, tn))
local showEach = true
local c = 0
local u = 0
local v = 1
for i=1,10000000000000 do
local found = false
u = u + 6
v = v + u
local mx = math.ceil(math.sqrt(v))
--for _,item in pairs(primes) do -- why: latent traversal bugfix (and performance), 6/11/2020 db
for _,item in ipairs(primes) do
if item > mx then
break
end
if v % item == 0 then
--print("[DEBUG] :( i = " .. i .. "; v = " .. v)
found = true
break
end
end
if not found then
--print("[DEBUG] :) i = " .. i .. "; v = " .. v)
c = c + 1
if showEach then
--local z = primes[table.getn(primes)] + 2 -- why: modernize (deprecated), 6/11/2020 db
local z = primes[#primes] + 2
while z <= v - 2 do
local fnd = false
--for _,item in pairs(primes) do -- why: latent traversal bugfix (and performance), 6/11/2020 db
for _,item in ipairs(primes) do
if item > mx then
break
end
if z % item == 0 then
fnd = true
break
end
end
if not fnd then
table.insert(primes, z)
end
z = z + 2
end
table.insert(primes, v)
io.write(string.format("%11d", v))
if c % 10 == 0 then
print()
end
if c == cutOff then
showEach = false
io.write(string.format("\nProgress to the %dth%s: ", bigUn, tn))
end
end
if c % little == 0 then
io.write(".")
if c == bigUn then
break
end
end
end
end
--print(string.format("\nThe %dth%s is %17d", c, tn, v)) -- why: correcting reported inaccuracy in output, 6/11/2020 db
print(string.format("\nThe %dth%s is %.0f", c, tn, v))</syntaxhighlight>
{{out}}
<pre>The first 200 cuban primes
7 19 37 61 127 271 331 397 547 631
919 1657 1801 1951 2269 2437 2791 3169 3571 4219
4447 5167 5419 6211 7057 7351 8269 9241 10267 11719
12097 13267 13669 16651 19441 19927 22447 23497 24571 25117
26227 27361 33391 35317 42841 45757 47251 49537 50311 55897
59221 60919 65269 70687 73477 74419 75367 81181 82171 87211
88237 89269 92401 96661 102121 103231 104347 110017 112327 114661
115837 126691 129169 131671 135469 140617 144541 145861 151201 155269
163567 169219 170647 176419 180811 189757 200467 202021 213067 231019
234361 241117 246247 251431 260191 263737 267307 276337 279991 283669
285517 292969 296731 298621 310087 329677 333667 337681 347821 351919
360187 368551 372769 374887 377011 383419 387721 398581 407377 423001
436627 452797 459817 476407 478801 493291 522919 527941 553411 574219
584767 590077 592741 595411 603457 608851 611557 619711 627919 650071
658477 666937 689761 692641 698419 707131 733591 742519 760537 769627
772669 784897 791047 812761 825301 837937 847477 863497 879667 886177
895987 909151 915769 925741 929077 932419 939121 952597 972991 976411
986707 990151 997057 1021417 1024921 1035469 1074607 1085407 1110817 1114471
1125469 1155061 1177507 1181269 1215397 1253887 1281187 1285111 1324681 1328671
1372957 1409731 1422097 1426231 1442827 1451161 1480519 1484737 1527247 1570357
 
Progress to the 100000th cuban prime: ..................................................
The 100000th cuban prime is 1792617147127</pre>
===Alternate===
Perhaps a more "readable" structure, and with specified formatting..
<syntaxhighlight lang="lua">-- cuban primes in Lua (alternate version 6/12/2020 db)
------------------
-- PRIME SUPPORT:
------------------
local sqrt, sieve, primes, N = math.sqrt, {false}, {}, 1400000
for i = 2,N do sieve[i]=true end
for i = 2,N do if sieve[i] then for j=i*i,N,i do sieve[j]=false end end end
for i = 2,N do if sieve[i] then primes[#primes+1]=i end end; sieve=nil
local function isprime(n)
if (n <= 1) then return false end
local limit = sqrt(n)
for i,p in ipairs(primes) do
if (n % p == 0) then return false end
if (p > limit) then return true end
end
error("insufficient list of primes")
end
 
------------------
-- PRINT SUPPORT:
------------------
local write, format = io.write, string.format
local function commafy(i) return tostring(i):reverse():gsub("(%d%d%d)","%1,"):reverse():gsub("^,","") end
 
----------------
-- ACTUAL TASK:
----------------
local COUNT, DOTAT, DOTPER, count, n = 100000, 200, 2000, 0, 0
while (count < COUNT) do
local h = 3 * n * (n + 1) + 1 -- A003215
if (isprime(h)) then
count = count + 1
if (count <= DOTAT) then
write(format("%11s%s", commafy(h), count%10==0 and "\n" or ""))
elseif (count == COUNT) then
print(format("\n%s", commafy(h)))
elseif (count % DOTPER == 0) then
write(".")
end
end
n = n + 1
end
</syntaxhighlight>
{{out}}
<pre> 7 19 37 61 127 271 331 397 547 631
919 1,657 1,801 1,951 2,269 2,437 2,791 3,169 3,571 4,219
4,447 5,167 5,419 6,211 7,057 7,351 8,269 9,241 10,267 11,719
12,097 13,267 13,669 16,651 19,441 19,927 22,447 23,497 24,571 25,117
26,227 27,361 33,391 35,317 42,841 45,757 47,251 49,537 50,311 55,897
59,221 60,919 65,269 70,687 73,477 74,419 75,367 81,181 82,171 87,211
88,237 89,269 92,401 96,661 102,121 103,231 104,347 110,017 112,327 114,661
115,837 126,691 129,169 131,671 135,469 140,617 144,541 145,861 151,201 155,269
163,567 169,219 170,647 176,419 180,811 189,757 200,467 202,021 213,067 231,019
234,361 241,117 246,247 251,431 260,191 263,737 267,307 276,337 279,991 283,669
285,517 292,969 296,731 298,621 310,087 329,677 333,667 337,681 347,821 351,919
360,187 368,551 372,769 374,887 377,011 383,419 387,721 398,581 407,377 423,001
436,627 452,797 459,817 476,407 478,801 493,291 522,919 527,941 553,411 574,219
584,767 590,077 592,741 595,411 603,457 608,851 611,557 619,711 627,919 650,071
658,477 666,937 689,761 692,641 698,419 707,131 733,591 742,519 760,537 769,627
772,669 784,897 791,047 812,761 825,301 837,937 847,477 863,497 879,667 886,177
895,987 909,151 915,769 925,741 929,077 932,419 939,121 952,597 972,991 976,411
986,707 990,151 997,057 1,021,417 1,024,921 1,035,469 1,074,607 1,085,407 1,110,817 1,114,471
1,125,469 1,155,061 1,177,507 1,181,269 1,215,397 1,253,887 1,281,187 1,285,111 1,324,681 1,328,671
1,372,957 1,409,731 1,422,097 1,426,231 1,442,827 1,451,161 1,480,519 1,484,737 1,527,247 1,570,357
.................................................
1,792,617,147,127</pre>
 
=={{header|Maple}}==
 
{{incorrect|Maple|<br><br> The output is still incorrect. <br><br> It appears that the Maple solution isn't using a correct formula for computing cuban primes. <br><br> See output from other entries for the first 200 cuban primes. <br><br> The first three cuban primes are: &nbsp; &nbsp; &nbsp; 7 &nbsp; &nbsp; 19 &nbsp; &nbsp; 37 &nbsp; &nbsp; ··· <br><br><br> It also appears that most of the program is missing. <br><br>}}
 
<syntaxhighlight lang="maple">CubanPrimes := proc(n) local i, cp;
cp := Array([]);
for i by 2 while numelems(cp) < n do
if isprime(3/4*i^2 + 1/4) then
ArrayTools:-Append(cp, 3/4*i^2 + 1/4);
end if;
end do;
return cp;
end proc;</syntaxhighlight>
{{out}}
<pre> The first 200 cuban primes are
[1407, 3819, 7437, 12261, 25527, 54471, 66531, 79797, 109947, 126831, 184719, 333057, 362001, 392151, 456069, 489837, 560991, 636969, 717771, 848019, 893847, 1038567, 1089219, 1248411, 1418457, 1477551, 1662069, 1857441, 2063667, 2355519, 2431497, 2666667, 2747469, 3346851, 3907641, 4005327, 4511847, 4722897, 4938771, 5048517, 5271627, 5499561, 6711591, 7098717, 8611041, 9197157, 9497451, 9956937, 10112511, 11235297, 11903421, 12244719, 13119069, 14208087, 14768877, 14958219, 15148767, 16317381, 16516371, 17529411, 17735637, 17943069, 18572601, 19428861, 20526321, 20749431, 20973747, 22113417, 22577727, 23046861, 23283237, 25464891, 25962969, 26465871, 27229269, 28264017, 29052741, 29318061, 30391401, 31209069, 32876967, 34013019, 34300047, 35460219, 36343011, 38141157, 40293867, 40606221, 42826467, 46434819, 47106561, 48464517, 49495647, 50537631, 52298391, 53011137, 53728707, 55543737, 56278191, 57017469, 57388917, 58886769, 59642931, 60022821, 62327487, 66265077, 67067067, 67873881, 69912021, 70735719, 72397587, 74078751, 74926569, 75352287, 75779211, 77067219, 77931921, 80114781, 81882777, 85023201, 87762027, 91012197, 92423217, 95757807, 96239001, 99151491, 105106719, 106116141, 111235611, 115418019, 117538167, 118605477, 119140941, 119677611, 121294857, 122379051, 122922957, 124561911, 126211719, 130664271, 132353877, 134054337, 138641961, 139220841, 140382219, 142133331, 147451791, 149246319, 152867937, 154695027, 155306469, 157764297, 159000447, 163364961, 165885501, 168425337, 170342877, 173562897, 176813067, 178121577, 180093387, 182739351, 184069569, 186073941, 186744477, 187416219, 188763321, 191471997, 195571191, 196258611, 198328107, 199020351, 200408457, 205304817, 206009121, 208129269, 215996007, 218166807, 223274217, 224008671, 226219269, 232167261, 236678907, 237435069, 244294797, 252031287, 257518587, 258307311, 266260881, 267062871, 275964357, 283355931, 285841497, 286672431, 290008227, 291683361, 297584319, 298432137, 306976647, 315641757]
The 200th cuban prime is: 1,570,357
The 100000th cuban prime is: 1792617147127</pre>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">cubans[m_Integer] := Block[{n = 1, result = {}, candidate},
While[Length[result] < m,
n++;
candidate = n^3 - (n - 1)^3;
If[PrimeQ[candidate], AppendTo[result, candidate]]];
result]
cubans[200]
NumberForm[Last[cubans[100000]], NumberSeparator -> ",", DigitBlock -> 3]</syntaxhighlight>
 
{{out}}
<pre>{7, 19, 37, 61, 127, 271, 331, 397, 547, 631, 919, 1657, 1801, 1951,
2269, 2437, 2791, 3169, 3571, 4219, 4447, 5167, 5419, 6211, 7057,
7351, 8269, 9241, 10267, 11719, 12097, 13267, 13669, 16651, 19441,
19927, 22447, 23497, 24571, 25117, 26227, 27361, 33391, 35317, 42841,
45757, 47251, 49537, 50311, 55897, 59221, 60919, 65269, 70687, 73477,
74419, 75367, 81181, 82171, 87211, 88237, 89269, 92401, 96661,
102121, 103231, 104347, 110017, 112327, 114661, 115837, 126691,
129169, 131671, 135469, 140617, 144541, 145861, 151201, 155269,
163567, 169219, 170647, 176419, 180811, 189757, 200467, 202021,
213067, 231019, 234361, 241117, 246247, 251431, 260191, 263737,
267307, 276337, 279991, 283669, 285517, 292969, 296731, 298621,
310087, 329677, 333667, 337681, 347821, 351919, 360187, 368551,
372769, 374887, 377011, 383419, 387721, 398581, 407377, 423001,
436627, 452797, 459817, 476407, 478801, 493291, 522919, 527941,
553411, 574219, 584767, 590077, 592741, 595411, 603457, 608851,
611557, 619711, 627919, 650071, 658477, 666937, 689761, 692641,
698419, 707131, 733591, 742519, 760537, 769627, 772669, 784897,
791047, 812761, 825301, 837937, 847477, 863497, 879667, 886177,
895987, 909151, 915769, 925741, 929077, 932419, 939121, 952597,
972991, 976411, 986707, 990151, 997057, 1021417, 1024921, 1035469,
1074607, 1085407, 1110817, 1114471, 1125469, 1155061, 1177507,
1181269, 1215397, 1253887, 1281187, 1285111, 1324681, 1328671,
1372957, 1409731, 1422097, 1426231, 1442827, 1451161, 1480519,
1484737, 1527247, 1570357}
 
3,432,424,324,232</pre>
 
=={{header|Nim}}==
{{trans|C#}}
<syntaxhighlight lang="nim">import strformat
import strutils
import math
 
const cutOff = 200
const bigUn = 100000
const chunks = 50
const little = bigUn div chunks
 
echo fmt"The first {cutOff} cuban primes"
var primes: seq[int] = @[3, 5]
var c, u = 0
var showEach: bool = true
var v = 1
for i in 1..high(BiggestInt):
var found: bool
inc u, 6
inc v, u
var mx = int(ceil(sqrt(float(v))))
for item in primes:
if item > mx:
break
if v mod item == 0:
found = true
break
if not found:
inc c
if showEach:
for z in countup(primes[^1] + 2, v - 2, step=2):
var fnd: bool = false
for item in primes:
if item > mx:
break
if z mod item == 0:
fnd = true
break
if not fnd:
primes.add(z)
primes.add(v)
write(stdout, fmt"{insertSep($v, ','):>11}")
if c mod 10 == 0:
write(stdout, "\n")
if c == cutOff:
showEach = false
write(stdout, fmt"Progress to the {bigUn}th cuban prime: ")
stdout.flushFile
if c mod little == 0:
write(stdout, ".")
stdout.flushFile
if c == bigUn:
break
write(stdout, "\n")
echo fmt"The {c}th cuban prime is {insertSep($v, ',')}"</syntaxhighlight>
{{out}}
<pre>The first 200 cuban primes
7 19 37 61 127 271 331 397 547 631
919 1,657 1,801 1,951 2,269 2,437 2,791 3,169 3,571 4,219
4,447 5,167 5,419 6,211 7,057 7,351 8,269 9,241 10,267 11,719
12,097 13,267 13,669 16,651 19,441 19,927 22,447 23,497 24,571 25,117
26,227 27,361 33,391 35,317 42,841 45,757 47,251 49,537 50,311 55,897
59,221 60,919 65,269 70,687 73,477 74,419 75,367 81,181 82,171 87,211
88,237 89,269 92,401 96,661 102,121 103,231 104,347 110,017 112,327 114,661
115,837 126,691 129,169 131,671 135,469 140,617 144,541 145,861 151,201 155,269
163,567 169,219 170,647 176,419 180,811 189,757 200,467 202,021 213,067 231,019
234,361 241,117 246,247 251,431 260,191 263,737 267,307 276,337 279,991 283,669
285,517 292,969 296,731 298,621 310,087 329,677 333,667 337,681 347,821 351,919
360,187 368,551 372,769 374,887 377,011 383,419 387,721 398,581 407,377 423,001
436,627 452,797 459,817 476,407 478,801 493,291 522,919 527,941 553,411 574,219
584,767 590,077 592,741 595,411 603,457 608,851 611,557 619,711 627,919 650,071
658,477 666,937 689,761 692,641 698,419 707,131 733,591 742,519 760,537 769,627
772,669 784,897 791,047 812,761 825,301 837,937 847,477 863,497 879,667 886,177
895,987 909,151 915,769 925,741 929,077 932,419 939,121 952,597 972,991 976,411
986,707 990,151 997,057 1,021,417 1,024,921 1,035,469 1,074,607 1,085,407 1,110,817 1,114,471
1,125,469 1,155,061 1,177,507 1,181,269 1,215,397 1,253,887 1,281,187 1,285,111 1,324,681 1,328,671
1,372,957 1,409,731 1,422,097 1,426,231 1,442,827 1,451,161 1,480,519 1,484,737 1,527,247 1,570,357
Progress to the 100000th cuban prime: ..................................................
The 100000th cuban prime is 1,792,617,147,127</pre>
 
=={{header|PARI/GP}}==
{{trans|Julia}}
<syntaxhighlight lang="PARI/GP">
cubanprimes(N) =
{
cubans = vector(N);
cube1 = 1; count = 1; cube100k = 0;
for (i=1, +oo,
cube2 = (i + 1)^3;
diff = cube2 - cube1;
if (isprime(diff),
if (count <= N, cubans[count] = diff);
if (count == 100000, cube100k = diff; break);
count++;
);
cube1 = cube2;
);
print("The first " N " Cuban primes are: ");
for (i=1, N,
if (cubans[i] != 0,
print1(cubans[i], " ");
if (i % 8 == 0, print);
);
);
print("\nThe 100,000th Cuban prime is " cube100k);
}
 
cubanprimes(200);
</syntaxhighlight>
{{out}}
<pre>
The first 200 Cuban primes are:
7 19 37 61 127 271 331 397
547 631 919 1657 1801 1951 2269 2437
2791 3169 3571 4219 4447 5167 5419 6211
7057 7351 8269 9241 10267 11719 12097 13267
13669 16651 19441 19927 22447 23497 24571 25117
26227 27361 33391 35317 42841 45757 47251 49537
50311 55897 59221 60919 65269 70687 73477 74419
75367 81181 82171 87211 88237 89269 92401 96661
102121 103231 104347 110017 112327 114661 115837 126691
129169 131671 135469 140617 144541 145861 151201 155269
163567 169219 170647 176419 180811 189757 200467 202021
213067 231019 234361 241117 246247 251431 260191 263737
267307 276337 279991 283669 285517 292969 296731 298621
310087 329677 333667 337681 347821 351919 360187 368551
372769 374887 377011 383419 387721 398581 407377 423001
436627 452797 459817 476407 478801 493291 522919 527941
553411 574219 584767 590077 592741 595411 603457 608851
611557 619711 627919 650071 658477 666937 689761 692641
698419 707131 733591 742519 760537 769627 772669 784897
791047 812761 825301 837937 847477 863497 879667 886177
895987 909151 915769 925741 929077 932419 939121 952597
972991 976411 986707 990151 997057 1021417 1024921 1035469
1074607 1085407 1110817 1114471 1125469 1155061 1177507 1181269
1215397 1253887 1281187 1285111 1324681 1328671 1372957 1409731
1422097 1426231 1442827 1451161 1480519 1484737 1527247 1570357
 
The 100,000th Cuban prime is 1792617147127
 
</pre>
 
 
 
=={{header|Pascal}}==
Line 565 ⟶ 2,309:
100: 283,669; 1000: 65,524,807; 10000: 11,712,188,419; 100000: 1,792,617,147,127
 
<langsyntaxhighlight lang="pascal">program CubanPrimes;
{$IFDEF FPC}
{$MODE DELPHI}
Line 667 ⟶ 2,411:
OutFirstCntCubPrimes(200,10);
OutNthCubPrime(100000);
end.</langsyntaxhighlight>
{{out}}
<pre> 7 19 37 61 127 271 331 397 547 631
Line 694 ⟶ 2,438:
=={{header|Perl}}==
{{libheader|ntheory}}
<langsyntaxhighlight lang="perl">use feature 'say';
use ntheory 'is_prime';
 
Line 725 ⟶ 2,469:
for my $n (1 .. 6) {
say "10^$n-th cuban prime is: ", commify((cuban_primes(10**$n))[-1]);
}</langsyntaxhighlight>
{{out}}
<pre>
Line 757 ⟶ 2,501:
</pre>
 
=={{header|Perl 6Phix}}==
{{libheader|Phix/mpfr}}
{{works with|Rakudo|2018.12}}
<!--<syntaxhighlight lang="phix">(phixonline)-->
 
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
===The task (k == 1)===
<span style="color: #008080;">include</span> <span style="color: #004080;">mpfr</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
Not the most efficient, but concise, and good enough for this task.
<span style="color: #004080;">integer</span> <span style="color: #000000;">np</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span>
<lang perl6>sub comma { $^i.flip.comb(3).join(',').flip }
<span style="color: #000000;">i</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">2</span>
 
<span style="color: #004080;">mpz</span> <span style="color: #000000;">p3</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_init</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">*</span><span style="color: #000000;">1</span><span style="color: #0000FF;">*</span><span style="color: #000000;">1</span><span style="color: #0000FF;">),</span>
my @cubans = lazy (1..Inf).hyper(:8degree).map({ ($_+1)³ - .³ }).grep: *.is-prime;
<span style="color: #000000;">i3</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_init</span><span style="color: #0000FF;">(),</span>
 
<span style="color: #000000;">p</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_init</span><span style="color: #0000FF;">(),</span>
put @cubans[^200]».&comma».fmt("%9s").rotor(10).join: "\n";
<span style="color: #000000;">pn</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_init</span><span style="color: #0000FF;">()</span>
 
put '';
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"The first 200 cuban primes are:\n"</span><span style="color: #0000FF;">)</span>
 
<span style="color: #004080;">sequence</span> <span style="color: #000000;">first200</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
put @cubans[99_999].&comma; # zero indexed</lang>
<span style="color: #004080;">atom</span> <span style="color: #000000;">t0</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">time</span><span style="color: #0000FF;">()</span>
 
<span style="color: #008080;">constant</span> <span style="color: #000000;">lim</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">platform</span><span style="color: #0000FF;">()=</span><span style="color: #004600;">JS</span><span style="color: #0000FF;">?</span><span style="color: #000000;">10000</span><span style="color: #0000FF;">:</span><span style="color: #000000;">100000</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">while</span> <span style="color: #000000;">np</span><span style="color: #0000FF;"><</span><span style="color: #000000;">lim</span> <span style="color: #008080;">do</span>
<span style="color: #7060A8;">mpz_ui_pow_ui</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">mpz_sub</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">,</span><span style="color: #000000;">i3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">p3</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">mpz_prime</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
<span style="color: #7060A8;">mpz_set</span><span style="color: #0000FF;">(</span><span style="color: #000000;">pn</span><span style="color: #0000FF;">,</span><span style="color: #000000;">p</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">np</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">np</span><span style="color: #0000FF;"><=</span><span style="color: #000000;">200</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">first200</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">first200</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"%,9d"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">mpz_get_integer</span><span style="color: #0000FF;">(</span><span style="color: #000000;">pn</span><span style="color: #0000FF;">)))</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">mod</span><span style="color: #0000FF;">(</span><span style="color: #000000;">np</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%s\n"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #000000;">first200</span><span style="color: #0000FF;">[-</span><span style="color: #000000;">10</span><span style="color: #0000FF;">..-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]))</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #7060A8;">mpz_set</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">i3</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">i</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\nThe %,dth cuban prime is %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">np</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">mpz_get_str</span><span style="color: #0000FF;">(</span><span style="color: #000000;">pn</span><span style="color: #0000FF;">,</span><span style="color: #000000;">comma_fill</span><span style="color: #0000FF;">:=</span><span style="color: #004600;">true</span><span style="color: #0000FF;">)})</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">p3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">i3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">p</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_free</span><span style="color: #0000FF;">({</span><span style="color: #000000;">p3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">i3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">p</span><span style="color: #0000FF;">})</span>
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">elapsed</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">time</span><span style="color: #0000FF;">()-</span><span style="color: #000000;">t0</span><span style="color: #0000FF;">)</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
<pre> 7 19 37 61 127 271 331 397 547 631
The first 200 cuban primes are:
7 19 37 61 127 271 331 397 547 631
919 1,657 1,801 1,951 2,269 2,437 2,791 3,169 3,571 4,219
4,447 5,167 5,419 6,211 7,057 7,351 8,269 9,241 10,267 11,719
Line 794 ⟶ 2,561:
1,372,957 1,409,731 1,422,097 1,426,231 1,442,827 1,451,161 1,480,519 1,484,737 1,527,247 1,570,357
 
The 100,000th cuban prime is 1,792,617,147,127</pre>
"5.9s"
</pre>
 
=={{header|Python}}==
===k == 2 through 10===
After reading up a bit, the general equation for cuban primes is prime numbers of the form {{math|((<var>x</var>+<var>k</var>)<sup>3</sup> - <var>x</var><sup>3</sup>)/<var>k</var> }} where k mod 3 is not equal 0.
 
The cubans where k == 1 (the focus of this task) is one of many possible groups. In general, it seems like the cubans where k == 1 and k == 2 are the two primary cases, but it is possible to have cubans with a k of any integer that is not a multiple of 3.
 
Here are the first 20 for each valid k up to 10:
<lang perl6>sub comma { $^i.flip.comb(3).join(',').flip }
 
for 2..10 -> \k {
next if k %% 3;
my @cubans = lazy (1..Inf).map({ (($_+k)³ - .³)/k }).grep: *.is-prime;
put "First 20 cuban primes where k = {k}:";
put @cubans[^20]».&comma».fmt("%7s").rotor(10).join: "\n";
put '';
}
</lang>
{{out}}
<pre>First 20 cuban primes where k = 2:
13 109 193 433 769 1,201 1,453 2,029 3,469 3,889
4,801 10,093 12,289 13,873 18,253 20,173 21,169 22,189 28,813 37,633
 
First 20 cuban primes where k = 4:
31 79 151 367 1,087 1,327 1,879 2,887 3,271 4,111
4,567 6,079 7,207 8,431 15,991 16,879 17,791 19,687 23,767 24,847
 
First 20 cuban primes where k = 5:
43 67 97 223 277 337 727 823 1,033 1,663
2,113 2,617 2,797 3,373 4,003 5,683 6,217 7,963 10,273 10,627
 
First 20 cuban primes where k = 7:
73 103 139 181 229 283 409 643 733 829
1,039 1,153 1,399 1,531 1,669 2,281 2,803 3,181 3,583 3,793
 
First 20 cuban primes where k = 8:
163 379 523 691 883 2,203 2,539 3,691 5,059 5,563
6,091 7,219 8,443 9,091 10,459 11,923 15,139 19,699 24,859 27,091
 
First 20 cuban primes where k = 10:
457 613 997 1,753 2,053 2,377 4,357 6,373 9,433 13,093
16,453 21,193 27,673 28,837 31,237 37,657 46,153 47,653 49,177 62,233</pre>
 
===k == 2^128===
Note that Perl 6 has native support for arbitrarily large integers and does not need to generate primes to test for primality. Using k of 2^128; finishes in ''well'' under a second.
<lang perl6>sub comma { $^i.flip.comb(3).join(',').flip }
 
my \k = 2**128;
put "First 10 cuban primes where k = {k}:";
.&comma.put for (lazy (0..Inf).map({ (($_+k)³ - .³)/k }).grep: *.is-prime)[^10];</lang>
<pre>First 10 cuban primes where k = 340282366920938463463374607431768211456:
115,792,089,237,316,195,423,570,985,008,687,908,160,544,961,995,247,996,546,884,854,518,799,824,856,507
115,792,089,237,316,195,423,570,985,008,687,908,174,836,821,405,927,412,012,346,588,030,934,089,763,531
115,792,089,237,316,195,423,570,985,008,687,908,219,754,093,839,491,289,189,512,036,211,927,493,764,691
115,792,089,237,316,195,423,570,985,008,687,908,383,089,629,961,541,751,651,931,847,779,176,235,685,011
115,792,089,237,316,195,423,570,985,008,687,908,491,299,422,642,400,183,033,284,972,942,478,527,291,811
115,792,089,237,316,195,423,570,985,008,687,908,771,011,528,251,411,600,000,178,900,251,391,998,361,371
115,792,089,237,316,195,423,570,985,008,687,908,875,137,932,529,218,769,819,971,530,125,513,071,648,307
115,792,089,237,316,195,423,570,985,008,687,908,956,805,700,590,244,001,051,181,435,909,137,442,897,427
115,792,089,237,316,195,423,570,985,008,687,909,028,264,997,643,641,078,378,490,103,469,808,767,771,907
115,792,089,237,316,195,423,570,985,008,687,909,158,933,426,541,281,448,348,425,952,723,607,761,904,131</pre>
 
=={{header|Python}}==
{{trans|C#}}
<syntaxhighlight lang="python">
<lang Python>
import datetime
import math
Line 879 ⟶ 2,589:
st = datetime.datetime.now()
 
for i in range(01, int(math.pow(2,20))):
found = False
u += 6
v += u
mx = int(math.sqrt(uv))
for item in primes:
Line 926 ⟶ 2,636:
print ("The {:,}th{} is {:,}".format(c, tn, v))
print("Computation time was {} seconds".format((datetime.datetime.now() - st).seconds))
</syntaxhighlight>
</lang>
 
{{out}}
<pre>The first 200 cuban primes:
<pre>
7 19 37 61 127 271 331 397 547 631
The first 200 cuban primes:
919 7 1,657 19 1,801 1,951 37 2,269 61 2,437 91 127 2,791 3,169 217 271 3,571 3314,219
4,447 397 5,167 547 5,419 6316,211 7,057 817 919 17,027351 18,261269 19,387241 110,657 267 111,801719
112,951 097 213,269267 213,437669 216,791651 319,169441 319,571927 322,781447 423,219497 424,447 571 425,681117
526,167 227 527,419361 633,211391 735,057317 742,351841 745,957757 847,269251 849,587537 950,241 311 955,577897
1059,267221 1060,981919 1165,719269 1270,097687 1273,871477 1374,267419 1375,669367 1481,491181 1682,651171 1787,557211
1888,019237 1889,961269 1992,441401 1996,927661 21102,421121 22103,447231 22104,969347 23110,497017 24112,571 327 25114,117661
26115,227 837 26126,791691 27129,361169 30131,301671 30135,907469 31140,519617 32144,761541 33145,391861 34151,669 201 35155,317269
37163,969 567 39169,331219 40170,021 647 42176,127419 42180,841811 44189,287757 45200,757467 47202,251021 48213,007 067 49231,537019
50234,311 361 52241,669 117 53246,467247 55251,897431 58260,381191 59263,221737 60267,919 307 61276,777337 62279,641991 64283,387669
65285,269 517 68292,857969 70296,687731 73298,477621 74310,419087 75329,367677 77333,281667 81337,181681 82347,171 821 84351,169919
87360,211187 88368,237551 89372,269769 92374,401887 95377,587011 96383,661419 98387,827 721 99398,919581 102407,121377 103423,231001
104436,347627 106452,597797 110459,017817 112476,327407 114478,661801 115493,837291 118522,207919 119527,401941 124553,237411 126574,691219
129584,169767 131590,671077 132592,931741 135595,469411 138603,031457 140608,617851 141611,919557 144619,541711 145627,861919 147650,187071
149658,857477 151666,201937 155689,269761 160692,777641 163698,567419 164707,971131 166733,381591 169742,219519 170760,647537 173769,521627
176772,419669 180784,811897 183791,769047 185812,257761 189825,757301 200837,467937 202847,021477 205863,147497 213879,067667 217886,891177
222895,769987 224909,407151 227915,701769 229925,357741 231929,019077 234932,361419 241939,117121 242952,821597 246972,247991 251976,431411
253986,171707 260990,191151 263997,737057 1,021,417 2651,519024,921 2671,035,307469 2701,901074,607 2761,085,337407 2791,110,991817 2831,669 285114,517471
1,125,469 2891,231155,061 2911,177,097507 2921,181,969269 2961,215,731397 2981,253,621887 3101,281,087187 3121,019 285,111 3151,901324,681 3191,807 323328,737671
1,372,957 3291,677 409,731 3331,667422,097 3371,426,681231 3431,442,747827 3471,451,821161 3511,480,919519 3601,187 484,737 3621,269527,247 3681,551 372570,769357
374,887 377,011 383,419 387,721 392,047 398,581 405,169 407,377 420,751 423,001
Progress to the 100,000th cuban prime:..................................................
The 100,000th cuban prime is 3911,406792,072617,461147,127
Computation time was 7856 seconds</pre>
 
</pre>
=={{header|Racket}}==
<syntaxhighlight lang="racket">#lang racket
 
(require math/number-theory
racket/generator)
 
(define (make-cuban-prime-generator)
(generator ()
(let loop ((y 1) (y3 1))
(let* ((x (+ y 1))
(x3 (expt x 3))
(p (quotient (- x3 y3) (- x y))))
(when (prime? p) (yield p))
(loop x x3)))))
 
(define (tabulate l (line-width 80))
(let* ((w (add1 (string-length (argmax string-length (map ~a l)))))
(cols (quotient line-width w)))
(for ((n (in-range 1 (add1 (length l))))
(i l))
(display (~a i #:width w #:align 'right))
(when (zero? (modulo n cols)) (newline)))))
 
(define (progress-report x)
(when (zero? (modulo x 1000))
(eprintf (if (zero? (modulo x 10000)) "|" "."))
(flush-output (current-error-port))))
 
(let ((200-cuban-primes (for/list ((_ 200)
(p (in-producer (make-cuban-prime-generator))))
p)))
(tabulate 200-cuban-primes))
 
(begin0
(for/last ((x 100000)
(p (in-producer (make-cuban-prime-generator))))
(progress-report x)
p)
(newline))</syntaxhighlight>
 
{{out}}
<pre> 7 19 37 61 127 271 331 397 547 631
919 1657 1801 1951 2269 2437 2791 3169 3571 4219
4447 5167 5419 6211 7057 7351 8269 9241 10267 11719
12097 13267 13669 16651 19441 19927 22447 23497 24571 25117
26227 27361 33391 35317 42841 45757 47251 49537 50311 55897
59221 60919 65269 70687 73477 74419 75367 81181 82171 87211
88237 89269 92401 96661 102121 103231 104347 110017 112327 114661
115837 126691 129169 131671 135469 140617 144541 145861 151201 155269
163567 169219 170647 176419 180811 189757 200467 202021 213067 231019
234361 241117 246247 251431 260191 263737 267307 276337 279991 283669
285517 292969 296731 298621 310087 329677 333667 337681 347821 351919
360187 368551 372769 374887 377011 383419 387721 398581 407377 423001
436627 452797 459817 476407 478801 493291 522919 527941 553411 574219
584767 590077 592741 595411 603457 608851 611557 619711 627919 650071
658477 666937 689761 692641 698419 707131 733591 742519 760537 769627
772669 784897 791047 812761 825301 837937 847477 863497 879667 886177
895987 909151 915769 925741 929077 932419 939121 952597 972991 976411
986707 990151 997057 1021417 1024921 1035469 1074607 1085407 1110817 1114471
1125469 1155061 1177507 1181269 1215397 1253887 1281187 1285111 1324681 1328671
1372957 1409731 1422097 1426231 1442827 1451161 1480519 1484737 1527247 1570357
|.........|.........|.........|.........|.........|.........|.........|.........|.........|.........
1792617147127</pre>
 
=={{header|Raku}}==
(formerly Perl 6)
{{libheader|ntheory}}
===The task (k == 1)===
Not the most efficient, but concise, and good enough for this task. Use the ntheory library for prime testing; gets it down to around 20 seconds.
<syntaxhighlight lang="raku" line>use Lingua::EN::Numbers;
use ntheory:from<Perl5> <:all>;
 
my @cubans = lazy (1..Inf).map({ ($_+1)³ - .³ }).grep: *.&is_prime;
 
put @cubans[^200]».&comma».fmt("%9s").rotor(10).join: "\n";
 
put '';
 
put @cubans[99_999].&comma; # zero indexed</syntaxhighlight>
 
{{out}}
<pre> 7 19 37 61 127 271 331 397 547 631
919 1,657 1,801 1,951 2,269 2,437 2,791 3,169 3,571 4,219
4,447 5,167 5,419 6,211 7,057 7,351 8,269 9,241 10,267 11,719
12,097 13,267 13,669 16,651 19,441 19,927 22,447 23,497 24,571 25,117
26,227 27,361 33,391 35,317 42,841 45,757 47,251 49,537 50,311 55,897
59,221 60,919 65,269 70,687 73,477 74,419 75,367 81,181 82,171 87,211
88,237 89,269 92,401 96,661 102,121 103,231 104,347 110,017 112,327 114,661
115,837 126,691 129,169 131,671 135,469 140,617 144,541 145,861 151,201 155,269
163,567 169,219 170,647 176,419 180,811 189,757 200,467 202,021 213,067 231,019
234,361 241,117 246,247 251,431 260,191 263,737 267,307 276,337 279,991 283,669
285,517 292,969 296,731 298,621 310,087 329,677 333,667 337,681 347,821 351,919
360,187 368,551 372,769 374,887 377,011 383,419 387,721 398,581 407,377 423,001
436,627 452,797 459,817 476,407 478,801 493,291 522,919 527,941 553,411 574,219
584,767 590,077 592,741 595,411 603,457 608,851 611,557 619,711 627,919 650,071
658,477 666,937 689,761 692,641 698,419 707,131 733,591 742,519 760,537 769,627
772,669 784,897 791,047 812,761 825,301 837,937 847,477 863,497 879,667 886,177
895,987 909,151 915,769 925,741 929,077 932,419 939,121 952,597 972,991 976,411
986,707 990,151 997,057 1,021,417 1,024,921 1,035,469 1,074,607 1,085,407 1,110,817 1,114,471
1,125,469 1,155,061 1,177,507 1,181,269 1,215,397 1,253,887 1,281,187 1,285,111 1,324,681 1,328,671
1,372,957 1,409,731 1,422,097 1,426,231 1,442,827 1,451,161 1,480,519 1,484,737 1,527,247 1,570,357
 
1,792,617,147,127</pre>
 
===k == 2 through 10===
After reading up a bit, the general equation for cuban primes is prime numbers of the form {{math|((<var>x</var>+<var>k</var>)<sup>3</sup> - <var>x</var><sup>3</sup>)/<var>k</var> }} where k mod 3 is not equal 0.
 
The cubans where k == 1 (the focus of this task) is one of many possible groups. In general, it seems like the cubans where k == 1 and k == 2 are the two primary cases, but it is possible to have cubans with a k of any integer that is not a multiple of 3.
 
Here are the first 20 for each valid k up to 10:
<syntaxhighlight lang="raku" line>sub comma { $^i.flip.comb(3).join(',').flip }
 
for 2..10 -> \k {
next if k %% 3;
my @cubans = lazy (1..Inf).map({ (($_+k)³ - .³)/k }).grep: *.is-prime;
put "First 20 cuban primes where k = {k}:";
put @cubans[^20]».&comma».fmt("%7s").rotor(10).join: "\n";
put '';
}
</syntaxhighlight>
{{out}}
<pre>First 20 cuban primes where k = 2:
13 109 193 433 769 1,201 1,453 2,029 3,469 3,889
4,801 10,093 12,289 13,873 18,253 20,173 21,169 22,189 28,813 37,633
 
First 20 cuban primes where k = 4:
31 79 151 367 1,087 1,327 1,879 2,887 3,271 4,111
4,567 6,079 7,207 8,431 15,991 16,879 17,791 19,687 23,767 24,847
 
First 20 cuban primes where k = 5:
43 67 97 223 277 337 727 823 1,033 1,663
2,113 2,617 2,797 3,373 4,003 5,683 6,217 7,963 10,273 10,627
 
First 20 cuban primes where k = 7:
73 103 139 181 229 283 409 643 733 829
1,039 1,153 1,399 1,531 1,669 2,281 2,803 3,181 3,583 3,793
 
First 20 cuban primes where k = 8:
163 379 523 691 883 2,203 2,539 3,691 5,059 5,563
6,091 7,219 8,443 9,091 10,459 11,923 15,139 19,699 24,859 27,091
 
First 20 cuban primes where k = 10:
457 613 997 1,753 2,053 2,377 4,357 6,373 9,433 13,093
16,453 21,193 27,673 28,837 31,237 37,657 46,153 47,653 49,177 62,233</pre>
 
===k == 2^128===
Note that Raku has native support for arbitrarily large integers and does not need to generate primes to test for primality. Using k of 2^128; finishes in ''well'' under a second.
<syntaxhighlight lang="raku" line>sub comma { $^i.flip.comb(3).join(',').flip }
 
my \k = 2**128;
put "First 10 cuban primes where k = {k}:";
.&comma.put for (lazy (0..Inf).map({ (($_+k)³ - .³)/k }).grep: *.is-prime)[^10];</syntaxhighlight>
<pre>First 10 cuban primes where k = 340282366920938463463374607431768211456:
115,792,089,237,316,195,423,570,985,008,687,908,160,544,961,995,247,996,546,884,854,518,799,824,856,507
115,792,089,237,316,195,423,570,985,008,687,908,174,836,821,405,927,412,012,346,588,030,934,089,763,531
115,792,089,237,316,195,423,570,985,008,687,908,219,754,093,839,491,289,189,512,036,211,927,493,764,691
115,792,089,237,316,195,423,570,985,008,687,908,383,089,629,961,541,751,651,931,847,779,176,235,685,011
115,792,089,237,316,195,423,570,985,008,687,908,491,299,422,642,400,183,033,284,972,942,478,527,291,811
115,792,089,237,316,195,423,570,985,008,687,908,771,011,528,251,411,600,000,178,900,251,391,998,361,371
115,792,089,237,316,195,423,570,985,008,687,908,875,137,932,529,218,769,819,971,530,125,513,071,648,307
115,792,089,237,316,195,423,570,985,008,687,908,956,805,700,590,244,001,051,181,435,909,137,442,897,427
115,792,089,237,316,195,423,570,985,008,687,909,028,264,997,643,641,078,378,490,103,469,808,767,771,907
115,792,089,237,316,195,423,570,985,008,687,909,158,933,426,541,281,448,348,425,952,723,607,761,904,131</pre>
 
=={{header|REXX}}==
Cuban primes can't end in an even (decimal) digit, &nbsp; or the digit &nbsp; '''5'''.
<lang rexx>/*REXX program finds and displays a number of cuban primes or the Nth cuban prime. */
 
numeric digits 20 /*ensure enought decimal digits for #s.*/
Also, by their construction, cuban primes can't have a
factor of &nbsp; '''6*k + 1''', &nbsp; where &nbsp; '''k''' &nbsp; is any positive integer.
<syntaxhighlight lang="rexx">/*REXX program finds and displays a number of cuban primes or the Nth cuban prime. */
numeric digits 20 /*ensure enough decimal digits for #s. */
parse arg N . /*obtain optional argument from the CL.*/
if N=='' | N=="," then N= 200 /*Not specified? Then use the default.*/
Nth= N<0; N= abs(N) /*used for finding the Nth cuban prime.*/
@.=0; @.0=1; @.2=1; @.3=1; @.4=1; @.5=1; @.6=1; @.8=1 /*ending digs that aren't cubans.*/
#= 0 /*number of cuban primes found so far. */
!.sw=0; linesize() - !.0=1; !.2=1;if !.3=sw<1; then !.4sw=1; 79 !.5=1; /*obtain !.6=1;width of the terminal !screen.8=1; s=41; $=*/
sww=12; linesize() - 1; if sw< #= 1; then sw$=79 right(7, w) /*obtainstart widthwith offirst thecuban terminalprime; screen. count.*/
if (Nth & N==1) | N>0 thendo $j=1 7 until #=>N; x= (j+1)**3 - j**3 /*compute a possible cuban prime. /*handle case of the 1st cuban prime.*/
parse var x '' -1 _; if @._ then iterate /*check last digit for non─cuban prime.*/
if (Nth & N==2) | N>1 then $= $ 19 /* " " " " 2nd " " */
if (Nth & N==3) | N>2 then $= $ 37 do k=1 until km*km>x; km= k*6 + 1 /*cuban primes " " " " 3rd can't be " ÷ "by 6k+1 */
#= 3 if x//km==0 then iterate j /*Divisible? Then not a /*above are needed forcuban prime. generation*/
N= abs(N) end /*use absolute value of N from now onk*/
if #<N= #+1 then /*bump [↑] the endingnumber digsof thatcuban aren'tprimes cubans.found*/
doif j=4Nth untilthen #=>Ndo; if #==N then do; say commas(x= (j+1)**3; - leave j**3 ; end /*computedisplay a possible1 cubannum.*/
parse var x '' -1 _; if !._ then iterate else iterate /*checkj*/ for the last digit /*keep searching*/
if x// 3==0 then iterate; if x// 7==0 then iterateend /* " " ÷ 3; for ÷ 7 /* [↑] try to fit as many #s per line.*/
ifcx= commas(x//11==0 then iterate); if x//13L==0 length(cx) then iterate /* " " ÷11; "/*insert commas──►X; ÷obtain 13the length.*/
if x//17=cx=0 right(cx, thenmax(w, iterateL) ); if x//19=new=0 $ cx then iterate /*right justify " " ÷17CX; concat to " ÷ 19new list*/
if x//23==0length(new)>sw then iteratedo; say if$; x//29= $=0 then iterate cx /*line is " " ÷23; too "long, display ÷#'s 29so far.*/
if x//31==0 then iterate; if x//37==0 then iterate /* " " ÷31; "end /* [↑] initialize the ÷(new) 37next line.*/
else $= new /*start with cuban # that wouldn't fit.*/
 
do k=s by 6 until k*k>x /*skip multiples of 3 (when using BY 6)*/
if x// k ==0 then iterate j /*test for a "lower" possible prime. */
if x//(k+2) ==0 then iterate j /* " " " "higher" " " */
end /*k*/
#= # + 1 /*bump the number of cuban primes found*/
if Nth then do; if #==N then do; say commas(x); leave j; end /*display 1 num*/
else iterate j /*keep searching. */
end
new=$ commas(x) /*append a new cuban. */
if length(new)>sw then do; say $ /*line too long, show #s*/
$= commas(x) /*initialize next line. */
end
else $= new /*use this longer output*/
end /*j*/
if S\=='' then say $ if \Nth & $\=='' then say $ /*check for any residual cuban primes in $.*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
commas: parse arg _; do jc=length(_)-3 to 1 by -3; _=insert(',', _, jc); end; return _</langsyntaxhighlight>
This REXX program makes use of &nbsp; '''LINESIZE''' &nbsp; REXX program &nbsp; (or
BIF) &nbsp; which is used to determine the screen width &nbsp;
<br>(or linesize) &nbsp; of the terminal (console). &nbsp; Some REXXes don't have this BIF.
 
The &nbsp; '''LINESIZE.REX''' &nbsp; REXX program is included
here &nbsp; ───► &nbsp; [[LINESIZE.REX]].
 
{{out|output|text=&nbsp; when using the default input of: &nbsp; &nbsp; <tt> 200 </tt>}}
 
<pre>
(Shown at three-quarter size.)
7 19 37 61 127 271 331 397 547 631 919 1,657 1,801 1,951 2,269 2,437 2,791 3,169 3,571 4,219 4,447 5,167 5,419 6,211 7,057 7,351
 
8,269 9,241 10,267 11,719 12,097 13,267 13,669 16,651 19,441 19,927 22,447 23,497 24,571 25,117 26,227 27,361 33,391 35,317
<pre style="font-size:75%">
42,841 45,757 47,251 49,537 50,311 55,897 59,221 60,919 65,269 70,687 73,477 74,419 75,367 81,181 82,171 87,211 88,237 89,269
7 19 37 61 127 271 331 397 547 631 919
92,401 96,661 102,121 103,231 104,347 110,017 112,327 114,661 115,837 126,691 129,169 131,671 135,469 140,617 144,541 145,861
1,657 1,801 1,951 2,269 2,437 2,791 3,169 3,571 4,219 4,447 5,167
151,201 155,269 163,567 169,219 170,647 176,419 180,811 189,757 200,467 202,021 213,067 231,019 234,361 241,117 246,247 251,431
5,419 6,211 7,057 7,351 8,269 9,241 10,267 11,719 12,097 13,267 13,669
260,191 263,737 267,307 276,337 279,991 283,669 285,517 292,969 296,731 298,621 310,087 329,677 333,667 337,681 347,821 351,919
16,651 19,441 19,927 22,447 23,497 24,571 25,117 26,227 27,361 33,391 35,317
360,187 368,551 372,769 374,887 377,011 383,419 387,721 398,581 407,377 423,001 436,627 452,797 459,817 476,407 478,801 493,291
42,841 45,757 47,251 49,537 50,311 55,897 59,221 60,919 65,269 70,687 73,477
522,919 527,941 553,411 574,219 584,767 590,077 592,741 595,411 603,457 608,851 611,557 619,711 627,919 650,071 658,477 666,937
74,419 75,367 81,181 82,171 87,211 88,237 89,269 92,401 96,661 102,121 103,231
689,761 692,641 698,419 707,131 733,591 742,519 760,537 769,627 772,669 784,897 791,047 812,761 825,301 837,937 847,477 863,497
104,347 110,017 112,327 114,661 115,837 126,691 129,169 131,671 135,469 140,617 144,541
879,667 886,177 895,987 909,151 915,769 925,741 929,077 932,419 939,121 952,597 972,991 976,411 986,707 990,151 997,057 1,021,417
145,861 151,201 155,269 163,567 169,219 170,647 176,419 180,811 189,757 200,467 202,021
1,024,921 1,035,469 1,074,607 1,085,407 1,110,817 1,114,471 1,125,469 1,155,061 1,177,507 1,181,269 1,215,397 1,253,887 1,281,187
213,067 231,019 234,361 241,117 246,247 251,431 260,191 263,737 267,307 276,337 279,991
1,285,111 1,324,681 1,328,671 1,372,957 1,409,731 1,422,097 1,426,231 1,442,827 1,451,161 1,480,519 1,484,737 1,527,247 1,570,357
283,669 285,517 292,969 296,731 298,621 310,087 329,677 333,667 337,681 347,821 351,919
360,187 368,551 372,769 374,887 377,011 383,419 387,721 398,581 407,377 423,001 436,627
452,797 459,817 476,407 478,801 493,291 522,919 527,941 553,411 574,219 584,767 590,077
592,741 595,411 603,457 608,851 611,557 619,711 627,919 650,071 658,477 666,937 689,761
692,641 698,419 707,131 733,591 742,519 760,537 769,627 772,669 784,897 791,047 812,761
825,301 837,937 847,477 863,497 879,667 886,177 895,987 909,151 915,769 925,741 929,077
932,419 939,121 952,597 972,991 976,411 986,707 990,151 997,057 1,021,417 1,024,921 1,035,469
1,074,607 1,085,407 1,110,817 1,114,471 1,125,469 1,155,061 1,177,507 1,181,269 1,215,397 1,253,887 1,281,187
1,285,111 1,324,681 1,328,671 1,372,957 1,409,731 1,422,097 1,426,231 1,442,827 1,451,161 1,480,519 1,484,737
1,527,247 1,570,357
</pre>
{{out|output|text=&nbsp; when using the input of: &nbsp; &nbsp; <tt> -100000 </tt>}}
Line 1,016 ⟶ 2,896:
1,792,617,147,127
</pre>
 
=={{header|Ring}}==
<syntaxhighlight lang="ring">
load "stdlib.ring"
 
sum = 0
limit = 1000
 
see "First 200 cuban primes:" + nl
 
for n = 1 to limit
pr = pow(n+1,3) - pow(n,3)
if isprime(pr)
sum = sum + 1
if sum < 201
see "" + pr + " "
else
exit
ok
ok
next
 
see "done..." + nl
</syntaxhighlight>
Output:
<pre>
First 200 cuban primes:
7 19 37 61 127 271 331 397 547 631 919 1657 1801 1951 2269 2437 2791 3169 3571 4219 4447 5167 5419 6211 7057 7351 8269 9241 10267 11719 12097 13267 13669 16651 19441 19927 22447 23497 24571 25117 26227 27361 33391 35317 42841 45757 47251 49537 50311 55897 59221 60919 65269 70687 73477 74419 75367 81181 82171 87211 88237 89269 92401 96661 102121 103231 104347 110017 112327 114661 115837 126691 129169 131671 135469 140617 144541 145861 151201 155269 163567 169219 170647 176419 180811 189757 200467 202021 213067 231019 234361 241117 246247 251431 260191 263737 267307 276337 279991 283669 285517 292969 296731 298621 310087 329677 333667 337681 347821 351919 360187 368551 372769 374887 377011 383419 387721 398581 407377 423001 436627 452797 459817 476407 478801 493291 522919 527941 553411 574219 584767 590077 592741 595411 603457 608851 611557 619711 627919 650071 658477 666937 689761 692641 698419 707131 733591 742519 760537 769627 772669 784897 791047 812761 825301 837937 847477 863497 879667 886177 895987 909151 915769 925741 929077 932419 939121 952597 972991 976411 986707 990151 997057 1021417 1024921 1035469 1074607 1085407 1110817 1114471 1125469 1155061 1177507 1181269 1215397 1253887 1281187 1285111 1324681 1328671 1372957 1409731 1422097 1426231 1442827 1451161 1480519 1484737 1527247 1570357
</pre>
 
=={{header|RPL}}==
{{works with|RPL|HP49-C}}
« { } 0
'''WHILE''' OVER SIZE 200 < '''REPEAT'''
1 + DUPDUP SQ + 3 * 1 +
'''IF''' DUP ISPRIME? '''THEN''' ROT + SWAP '''ELSE''' DROP '''END'''
'''END'''
DROP REVLIST
» '<span style="color:blue">TASK</span>' STO
{{out}}
<pre>
1: { 7 19 37 61 127 271 331 397 547 631 919 1657 1801 1951 2269 2437 2791 3169 3571 4219 4447 5167 5419 6211 7057 7351 8269 9241 10267 11719 12097 13267 13669 16651 19441 19927 22447 23497 24571 25117 26227 27361 33391 35317 42841 45757 47251 49537 50311 55897 59221 60919 65269 70687 73477 74419 75367 81181 82171 87211 88237 89269 92401 96661 102121 103231 104347 110017 112327 114661 115837 126691 129169 131671 135469 140617 144541 145861 151201 155269 163567 169219 170647 176419 180811 189757 200467 202021 213067 231019 234361 241117 246247 251431 260191 263737 267307 276337 279991 283669 285517 292969 296731 298621 310087 329677 333667 337681 347821 351919 360187 368551 372769 374887 377011 383419 387721 398581 407377 423001 436627 452797 459817 476407 478801 493291 522919 527941 553411 574219 584767 590077 592741 595411 603457 608851 611557 619711 627919 650071 658477 666937 689761 692641 698419 707131 733591 742519 760537 769627 772669 784897 791047 812761 825301 837937 847477 863497 879667 886177 895987 909151 915769 925741 929077 932419 939121 952597 972991 976411 986707 990151 997057 1021417 1024921 1035469 1074607 1085407 1110817 1114471 1125469 1155061 1177507 1181269 1215397 1253887 1281187 1285111 1324681 1328671 1372957 1409731 1422097 1426231 1442827 1451161 1480519 1484737 1527247 1570357 }
</pre>
Task needs 61 seconds to run on a regular HP-50g. Looking for the 100,000th cuban prime would take a very long time for an interpreted language.
 
=={{header|Ruby}}==
<syntaxhighlight lang="ruby">require "openssl"
RE = /(\d)(?=(\d\d\d)+(?!\d))/ # Activesupport uses this for commatizing
cuban_primes = Enumerator.new do |y|
(1..).each do |n|
cand = 3*n*(n+1) + 1
y << cand if OpenSSL::BN.new(cand).prime?
end
end
def commatize(num)
num.to_s.gsub(RE, "\\1,")
end
cbs = cuban_primes.take(200)
formatted = cbs.map{|cb| commatize(cb).rjust(10) }
puts formatted.each_slice(10).map(&:join)
t0 = Time.now
puts "
100_000th cuban prime is #{commatize( cuban_primes.take(100_000).last)}
which took #{(Time.now-t0).round} seconds to calculate."</syntaxhighlight>
{{out}}
<pre> 7 19 37 61 127 271 331 397 547 631
919 1,657 1,801 1,951 2,269 2,437 2,791 3,169 3,571 4,219
4,447 5,167 5,419 6,211 7,057 7,351 8,269 9,241 10,267 11,719
12,097 13,267 13,669 16,651 19,441 19,927 22,447 23,497 24,571 25,117
26,227 27,361 33,391 35,317 42,841 45,757 47,251 49,537 50,311 55,897
59,221 60,919 65,269 70,687 73,477 74,419 75,367 81,181 82,171 87,211
88,237 89,269 92,401 96,661 102,121 103,231 104,347 110,017 112,327 114,661
115,837 126,691 129,169 131,671 135,469 140,617 144,541 145,861 151,201 155,269
163,567 169,219 170,647 176,419 180,811 189,757 200,467 202,021 213,067 231,019
234,361 241,117 246,247 251,431 260,191 263,737 267,307 276,337 279,991 283,669
285,517 292,969 296,731 298,621 310,087 329,677 333,667 337,681 347,821 351,919
360,187 368,551 372,769 374,887 377,011 383,419 387,721 398,581 407,377 423,001
436,627 452,797 459,817 476,407 478,801 493,291 522,919 527,941 553,411 574,219
584,767 590,077 592,741 595,411 603,457 608,851 611,557 619,711 627,919 650,071
658,477 666,937 689,761 692,641 698,419 707,131 733,591 742,519 760,537 769,627
772,669 784,897 791,047 812,761 825,301 837,937 847,477 863,497 879,667 886,177
895,987 909,151 915,769 925,741 929,077 932,419 939,121 952,597 972,991 976,411
986,707 990,151 997,057 1,021,417 1,024,921 1,035,469 1,074,607 1,085,407 1,110,817 1,114,471
1,125,469 1,155,061 1,177,507 1,181,269 1,215,397 1,253,887 1,281,187 1,285,111 1,324,681 1,328,671
1,372,957 1,409,731 1,422,097 1,426,231 1,442,827 1,451,161 1,480,519 1,484,737 1,527,247 1,570,357
 
100_000th cuban prime is 1,792,617,147,127
which took 31 seconds to calculate.
</pre>
 
=={{header|Rust}}==
Uses the libraries [https://crates.io/crates/primal primal] and [https://crates.io/crates/separator separator]
<syntaxhighlight lang="rust">use std::time::Instant;
use separator::Separatable;
 
const NUMBER_OF_CUBAN_PRIMES: usize = 200;
const COLUMNS: usize = 10;
const LAST_CUBAN_PRIME: usize = 100_000;
 
fn main() {
println!("Calculating the first {} cuban primes and the {}th cuban prime...", NUMBER_OF_CUBAN_PRIMES, LAST_CUBAN_PRIME);
let start = Instant::now();
 
let mut i: u64 = 0;
let mut j: u64 = 1;
let mut index: usize = 0;
let mut cuban_primes = Vec::new();
let mut cuban: u64 = 0;
while index < 100_000 {
cuban = {j += 1; j}.pow(3) - {i += 1; i}.pow(3);
if primal::is_prime(cuban) {
if index < NUMBER_OF_CUBAN_PRIMES {
cuban_primes.push(cuban);
}
index += 1;
}
}
 
let elapsed = start.elapsed();
println!("THE {} FIRST CUBAN PRIMES:", NUMBER_OF_CUBAN_PRIMES);
cuban_primes
.chunks(COLUMNS)
.map(|chunk| {
chunk.iter()
.map(|item| {
print!("{}\t", item)
})
.for_each(drop);
println!("");
})
.for_each(drop);
println!("The {}th cuban prime number is {}", LAST_CUBAN_PRIME, cuban.separated_string());
println!("Elapsed time: {:?}", elapsed);
}</syntaxhighlight>
{{out}}
<pre>Calculating the first 200 cuban primes and the 100000th cuban prime...
THE 200 FIRST CUBAN PRIMES:
7 19 37 61 127 271 331 397 547 631
919 1657 1801 1951 2269 2437 2791 3169 3571 4219
4447 5167 5419 6211 7057 7351 8269 9241 10267 11719
12097 13267 13669 16651 19441 19927 22447 23497 24571 25117
26227 27361 33391 35317 42841 45757 47251 49537 50311 55897
59221 60919 65269 70687 73477 74419 75367 81181 82171 87211
88237 89269 92401 96661 102121 103231 104347 110017 112327 114661
115837 126691 129169 131671 135469 140617 144541 145861 151201 155269
163567 169219 170647 176419 180811 189757 200467 202021 213067 231019
234361 241117 246247 251431 260191 263737 267307 276337 279991 283669
285517 292969 296731 298621 310087 329677 333667 337681 347821 351919
360187 368551 372769 374887 377011 383419 387721 398581 407377 423001
436627 452797 459817 476407 478801 493291 522919 527941 553411 574219
584767 590077 592741 595411 603457 608851 611557 619711 627919 650071
658477 666937 689761 692641 698419 707131 733591 742519 760537 769627
772669 784897 791047 812761 825301 837937 847477 863497 879667 886177
895987 909151 915769 925741 929077 932419 939121 952597 972991 976411
986707 990151 997057 1021417 1024921 1035469 1074607 1085407 1110817 1114471
1125469 1155061 1177507 1181269 1215397 1253887 1281187 1285111 1324681 1328671
1372957 1409731 1422097 1426231 1442827 1451161 1480519 1484737 1527247 1570357
The 100000th cuban prime number is 1,792,617,147,127
Elapsed time: 11.005581564s</pre>
 
=={{header|Scala}}==
In this example, we start by building an infinite lazy list of cubans and filter out non-primes. This gives us a lazily evaluated list of all cuban primes, and finding the first 200 simply involves taking 200 elements off the list.
 
To find the 100,000th cuban prime, performance becomes an issue. To remedy this, we write a function that breaks off a chunk from the front of the list of cubans and filters it using a parallel vector, repeating this process until it's found enough cuban primes. This allows us to benefit from the memory efficiency of lazy lists and the number-crunching speed of parallel vectors at the same time.
 
Spire's SafeLong is used instead of Java's BigInt for performance.
<syntaxhighlight lang="scala">import spire.math.SafeLong
import spire.implicits._
 
import scala.annotation.tailrec
import scala.collection.parallel.immutable.ParVector
 
object CubanPrimes {
def main(args: Array[String]): Unit = {
println(formatTable(cubanPrimes.take(200).toVector, 10))
println(f"The 100,000th cuban prime is: ${getNthCubanPrime(100000).toBigInt}%,d")
}
def cubanPrimes: LazyList[SafeLong] = cubans.filter(isPrime)
def cubans: LazyList[SafeLong] = LazyList.iterate(SafeLong(0))(_ + 1).map(n => (n + 1).pow(3) - n.pow(3))
def isPrime(num: SafeLong): Boolean = (num > 1) && !(SafeLong(2) #:: LazyList.iterate(SafeLong(3)){n => n + 2}).takeWhile(n => n*n <= num).exists(num%_ == 0)
def getNthCubanPrime(num: Int): SafeLong = {
@tailrec
def nHelper(rem: Int, src: LazyList[SafeLong]): SafeLong = {
val cprimes = src.take(100000).to(ParVector).filter(isPrime)
if(cprimes.size < rem) nHelper(rem - cprimes.size, src.drop(100000))
else cprimes.toVector.sortWith(_<_)(rem - 1)
}
nHelper(num, cubans)
}
def formatTable(lst: Vector[SafeLong], rlen: Int): String = {
@tailrec
def fHelper(ac: Vector[String], src: Vector[String]): String = {
if(src.nonEmpty) fHelper(ac :+ src.take(rlen).mkString, src.drop(rlen))
else ac.mkString("\n")
}
val maxLen = lst.map(n => f"${n.toBigInt}%,d".length).max
val formatted = lst.map(n => s"%,${maxLen + 2}d".format(n.toInt))
fHelper(Vector[String](), formatted)
}
}</syntaxhighlight>
 
{{out}}
<pre> 7 19 37 61 127 271 331 397 547 631
919 1,657 1,801 1,951 2,269 2,437 2,791 3,169 3,571 4,219
4,447 5,167 5,419 6,211 7,057 7,351 8,269 9,241 10,267 11,719
12,097 13,267 13,669 16,651 19,441 19,927 22,447 23,497 24,571 25,117
26,227 27,361 33,391 35,317 42,841 45,757 47,251 49,537 50,311 55,897
59,221 60,919 65,269 70,687 73,477 74,419 75,367 81,181 82,171 87,211
88,237 89,269 92,401 96,661 102,121 103,231 104,347 110,017 112,327 114,661
115,837 126,691 129,169 131,671 135,469 140,617 144,541 145,861 151,201 155,269
163,567 169,219 170,647 176,419 180,811 189,757 200,467 202,021 213,067 231,019
234,361 241,117 246,247 251,431 260,191 263,737 267,307 276,337 279,991 283,669
285,517 292,969 296,731 298,621 310,087 329,677 333,667 337,681 347,821 351,919
360,187 368,551 372,769 374,887 377,011 383,419 387,721 398,581 407,377 423,001
436,627 452,797 459,817 476,407 478,801 493,291 522,919 527,941 553,411 574,219
584,767 590,077 592,741 595,411 603,457 608,851 611,557 619,711 627,919 650,071
658,477 666,937 689,761 692,641 698,419 707,131 733,591 742,519 760,537 769,627
772,669 784,897 791,047 812,761 825,301 837,937 847,477 863,497 879,667 886,177
895,987 909,151 915,769 925,741 929,077 932,419 939,121 952,597 972,991 976,411
986,707 990,151 997,057 1,021,417 1,024,921 1,035,469 1,074,607 1,085,407 1,110,817 1,114,471
1,125,469 1,155,061 1,177,507 1,181,269 1,215,397 1,253,887 1,281,187 1,285,111 1,324,681 1,328,671
1,372,957 1,409,731 1,422,097 1,426,231 1,442,827 1,451,161 1,480,519 1,484,737 1,527,247 1,570,357
The 100,000th cuban prime is: 1,792,617,147,127</pre>
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">func cuban_primes(n) {
1..Inf -> lazy.map {|k| 3*k*(k+1) + 1 }\
.grep{ .is_prime }\
Line 1,028 ⟶ 3,141:
}
 
say ("\n100,000th cuban prime is: ", cuban_primes(1e5).last.commify)</langsyntaxhighlight>
{{out}}
<pre>
Line 1,054 ⟶ 3,167:
100,000th cuban prime is: 1,792,617,147,127
</pre>
=={{header|Transd}}==
{{trans|Python}}
<syntaxhighlight lang="scheme">
#lang transd
 
MainModule: {
=={{header|Visual Basic .NET}}==
primes: Vector<ULong>([3, 5]),
===Corner Cutting Version===
lim: 200,
This language doesn't have a built-in for a ''IsPrime()'' function, so I was surprised to find that this runs so quickly. It builds a list of primes while it is creating the output table. Since the last item on the table is larger than the square root of the 100,000<sup>th</sup> cuban prime, there is no need to continue adding to the prime list while checking up to the 100,000<sup>th</sup> cuban prime. I found a bit of a shortcut, if you skip the iterator by just the right amount, only one value is tested for the final result. It's hard-coded in the program, so if another final cuban prime were to be selected for output, the program would need a re-write. If not skipping ahead to the answer, it takes a few seconds over a minute to eventually get to it (see Snail Version below).
bigUn: 100000,
<lang vbnet>Module Module1
chunks: 50,
Dim primes As List(Of Long) = {3L, 5L}.ToList()
little: 0,
c: 0,
showEach: true,
u: ULong(0),
v: ULong(1),
 
_start: (λ found Bool() fnd Bool() mx Int() z ULong()
Sub Main(args As String())
Const(= cutOfflittle As Integer = 200,(/ bigUn As Integer = 100000,chunks))
(for i in Range(1 (pow 2 tn20)) As String = " cuban prime"do
(= found false)
Console.WriteLine("The first {0:n0}{1}s:", cutOff, tn)
Dim c As Integer (+= 0,u showEach6) As(+= Booleanv u) (= True,mx skip(to-Int As(sqrt Boolean =v) True,1))
v(for Asitem Longin =primes 0, st As DateTime = DateTime.Nowdo
For i As Long = 1 To Long.MaxValue (if (> item mx) break)
v = 3 * i(if :(not v =(mod v *item)) i(= + vfound +true) 1
Dim found As Boolean = False, mx As Integer = Math.Ceiling(Math.Sqrt(vbreak))
For(if Each(not item In primesfound)
If(+= itemc > mx Then Exit For1)
If(if v Mod item = 0 Then found = True : Exit ForshowEach
Next : If Not found Then (= z (get primes -1))
c += 1 : If(while showEach(< Thenz (- v 2))
For z = primes.Last (+= z 2) To(= vfnd - 2 Step 2false)
Dim(for fnditem Asin Booleanprimes = Falsedo
For Each (if (> item Inmx) primesbreak)
If(if item(not >(mod mxz item)) Then(= Exitfnd Fortrue)
If z Mod item = 0 Then fnd = True : Exit Forbreak))
Next(if : If Not(not fnd) Then(append primes.Add( z)))
Next :(append primes.Add(v) : Console.Write("{0,11:n0}", v)
If(textout c:width Mod11 10:group = 0 Then Console.WriteLine(v)
If(if c(not =(mod cutOff Thenc showEach10)) =(textout False:nl))
Else (if (== c lim) (= showEach false)
If skip Then skip =(textout False"Progress :to ithe += 772279" : c =group bigUn - 1
End If "'th cuban prime:" ))
If c = bigUn Then Exit For)
End If (if (not (mod c little)) (textout "."))
Next (if (== c bigUn) break)
)
Console.WriteLine("{1}The {2:n0}th{3} is {0,17:n0}", v, vbLf, c, tn)
)
Console.WriteLine("Computation time was {0} seconds", (DateTime.Now - st).TotalSeconds)
(lout "The " :group c "'th cuban prime is " v )
If System.Diagnostics.Debugger.IsAttached Then Console.ReadKey()
End Sub)
}
End Module</lang>
</syntaxhighlight>{{out}}
<pre>
<pre>The first 200 cuban primes:
7 19 37 61 127 271 331 397 547 631
919 1,657 1,801 1,951 2,269 2,437 2,791 3,169 3,571 4,219
Line 1,117 ⟶ 3,239:
1,125,469 1,155,061 1,177,507 1,181,269 1,215,397 1,253,887 1,281,187 1,285,111 1,324,681 1,328,671
1,372,957 1,409,731 1,422,097 1,426,231 1,442,827 1,451,161 1,480,519 1,484,737 1,527,247 1,570,357
Progress to the 100,000'th cuban prime:..................................................
 
The 100,000th cuban prime is 1,792,617,147,127
</pre>
Computation time was 0.2989494 seconds</pre>
 
===Snail Version===
=={{header|Wren}}==
This one doesn't take any shortcuts. It could be sped up (Execution time about 15 seconds) by threading chunks of the search for the 100,000<sup>th</sup> cuban prime, but you would have to take a guess about how far to go, which would be hard-coded, so one might as well use the short-cut version if you are willing to overlook that difficulty.
{{trans|Python}}
<lang vbnet>Module Program
{{libheader|Wren-fmt}}
Dim primes As List(Of Long) = {3L, 5L}.ToList()
<syntaxhighlight lang="wren">import "./fmt" for Fmt
 
var start = System.clock
var primes = [3, 5]
var cutOff = 200
var bigOne = 100000
var cubans = []
var bigCuban = ""
var c = 0
var showEach = true
var u = 0
var v = 1
 
for (i in 1...(1<<20)) {
var found = false
u = u + 6
v = v + u
var mx = v.sqrt.floor
for (item in primes) {
if (item > mx) break
if (v%item == 0) {
found = true
break
}
}
if (!found) {
c = c + 1
if (showEach) {
var z = primes[-1]
while (z <= v -2) {
z = z + 2
var fnd = false
for (item in primes) {
if (item > mx) break
if (z%item == 0) {
fnd = true
break
}
}
if (!fnd) {
primes.add(z)
}
}
primes.add(v)
cubans.add(Fmt.commatize(v))
if (c == cutOff) showEach = false
}
if (c == bigOne) {
bigCuban = Fmt.commatize(v)
break
}
}
}
 
System.print("The first 200 cuban primes are:-")
for (i in 0...20) {
var j = i * 10
for (k in j...j+10) System.write(Fmt.s(10, cubans[k])) // 10 per line say
System.print()
}
 
System.print("\nThe 100,000th cuban prime is %(bigCuban)")
System.print("\nTook %(System.clock - start) secs")</syntaxhighlight>
 
Sub Main(args As String())
Dim taskList As New List(Of Task(Of Integer))
Const cutOff As Integer = 200, bigUn As Integer = 100000,
chunks As Integer = 50, little As Integer = bigUn / chunks,
tn As String = " cuban prime"
Console.WriteLine("The first {0:n0}{1}s:", cutOff, tn)
Dim c As Integer = 0, showEach As Boolean = True,
u As Long = 0, v As Long = 1,
st As DateTime = DateTime.Now
For i As Long = 1 To Long.MaxValue
u += 6 : v += u
Dim found As Boolean = False, mx As Integer = Math.Ceiling(Math.Sqrt(v))
For Each item In primes
If item > mx Then Exit For
If v Mod item = 0 Then found = True : Exit For
Next : If Not found Then
c += 1 : If showEach Then
For z = primes.Last + 2 To v - 2 Step 2
Dim fnd As Boolean = False
For Each item In primes
If item > mx Then Exit For
If z Mod item = 0 Then fnd = True : Exit For
Next : If Not fnd Then primes.Add(z)
Next : primes.Add(v) : Console.Write("{0,11:n0}", v)
If c Mod 10 = 0 Then Console.WriteLine()
If c = cutOff Then showEach = False : _
Console.Write("{0}Progress to the {1:n0}th{2}: ", vbLf, bigUn, tn)
End If
If c Mod little = 0 Then Console.Write(".") : If c = bigUn Then Exit For
End If
Next
Console.WriteLine("{1}The {2:n0}th{3} is {0,17:n0}", v, vbLf, c, tn)
Console.WriteLine("Computation time was {0} seconds", (DateTime.Now - st).TotalSeconds)
If System.Diagnostics.Debugger.IsAttached Then Console.ReadKey()
End Sub
End Module</lang>
{{out}}
<pre>
<pre>The first 200 cuban primes:
The first 200 cuban primes are:-
7 19 37 61 127 271 331 397 547 631
919 7 1,657 19 1,801 37 1,951 2,269 61 2,437 127 2,791 271 3,169 331 3,571 397 4,219 547 631
4,447 919 51,167657 51,419801 61,211951 72,057269 72,351437 82,269791 93,241169 103,267571 114,719219
124,097447 135,267167 135,669419 166,651211 197,441057 197,927351 228,447269 239,497241 2410,571 267 2511,117719
2612,227 097 2713,361267 3313,391669 3516,317651 4219,841441 4519,757927 4722,251447 4923,537497 5024,311 571 5525,897117
5926,221 227 6027,919361 6533,269391 7035,687317 7342,477841 7445,419757 7547,367251 8149,181537 8250,171 311 8755,211897
8859,237221 8960,269919 9265,401 269 9670,661687 10273,121477 10374,231419 10475,347367 11081,017181 11282,327171 11487,661211
11588,837237 12689,691269 12992,169401 13196,671661 135102,469121 140103,617231 144104,541 347 145110,861017 151112,201327 155114,269661
163115,567837 169126,219691 170129,647169 176131,419671 180135,811 469 189140,757617 200144,467541 202145,021861 213151,067 201 231155,019269
234163,361 567 241169,117219 246170,247647 251176,431419 260180,191811 263189,737757 267200,307467 276202,337021 279213,991 067 283231,669019
285234,517 361 292241,969117 296246,731247 298251,621431 310260,087191 329263,677737 333267,667 307 276,337,681 347279,821 991 351283,919669
360285,187 517 368292,551969 372296,769731 374298,887621 377310,011087 383329,419677 387333,721667 398337,581681 407347,377 821 423351,001919
436360,627 187 452368,797551 459372,817769 476374,407 887 478377,801011 493383,291419 522387,919721 527398,941581 553407,411 377 574423,219001
584436,767627 590452,077797 592459,741 817 595476,411 407 603478,457801 608493,851291 611522,557919 619527,711941 627553,919 411 650574,071219
658584,477 767 666590,937077 689592,761741 692595,641411 698603,419457 707608,131851 733611,591 557 742619,519711 760627,537919 769650,627071
772658,669477 784666,897937 791689,047761 812692,761641 825698,301419 837707,937131 847733,477 591 863742,497519 879760,667 537 886769,177627
895772,987 669 909784,151897 915791,769047 925812,741761 929825,077301 932837,419937 939847,121477 952863,597497 972879,991 667 976886,411177
986895,707 987 990909,151 997915,057769 1 925,021,417741 1,024 929,921077 1,035 932,469419 1,074 939,607121 1 952,085,407597 1972,110,817991 1,114 976,471411
1 986,125,469707 1990,155,061151 1997,177,507 057 1,181021,269 417 1,215024,397 921 1,253035,887 469 1,281074,187 607 1,285085,111 407 1,324110,681 817 1,328114,671471
1,372125,957 469 1,409155,731 061 1,422177,097 507 1,426181,231 269 1,442215,827 397 1,451253,161 887 1,480281,519 187 1,484285,737 111 1,527324,247 681 1,570328,357671
1,372,957 1,409,731 1,422,097 1,426,231 1,442,827 1,451,161 1,480,519 1,484,737 1,527,247 1,570,357
 
Progress to the 100,000th cuban prime: ..................................................
The 100,000th cuban prime is 1,792,617,147,127
 
Computation time was 49.5868152 seconds</pre>
Took 623.849545 secs
===k > 1 Version===
</pre>
A VB.NET version of the [http://www.rosettacode.org/wiki/Cuban_primes#Perl_6 Perl 6] version where k > 1, linked at [https://tio.run/##fVVtb9owEP7OrzjlU9KmGbSaJiExqSuthFboNND2cTKJAxaJzWIHqFB/O71zQqghW6SQ3HHP43t5Djbzm1gV/HAYq6TMOFSPXgfwGooc1oXIuYZ7DRO@hWehjf@SwkgavuBF0LFx03KOHmF@2Fg/sM4jQZYbQtcIGMDt524IacYW2sfvAvrym1IZZ7LBPakC5BkKZspy9WGUwkSZmkIGMFtyCX8a8JFg5RJIuMK7JpkavkarX5OsAgyYFSVHz4TvTMNFBvqITrTn4xzrZCbqzKoORixJyAUOtzUeZUIt7EDTzDET0mfFwvZ9agohF35wauuDktpAXJqXND3LCltr5AmFLg8D56xOw3NGE7tgxGZ3wDSIyhXC2jGdUjcfsCFoO@MhM3yGpyDX8TWaqG2Da9VIy6huwSjodatRryBXCdxRflU/sXojJM4KkU5K1BbUUfS7EIY/C8l970kUmNq@@7bvvWnYLnnBkXAA@9u3vhfWLaSWhbAKHLLYHtg6@h6NvjajMdv9YlnJHTBdGaUsUHTV3admkuMaE7DG1cB@0kcf2zkAH99uEBfAJ1hd8NHAUlWiVk4Lg6AnlmkeQr5zcxwzs4weuMhIOtaY/i2MvwmCC2Kq8JHFS8Cu5UhRC/Yirha4jfpKB9ppPO6EuZjEh/AN/aJUoGaCVRnNxv2Tot6@41ZZFBG0nhXDNY0G4x0d@N6@G37pSysBHPkmRD4/tlmhxKzqN/NnFIHntTSnroLUUO/bUYNnUgv@Vwht@CjtnNfWaTVaZPyg8nVpmBFKgqEF2@JaoqpB81jJRGNd/seNQxlpE0QzZVg2rUJOpWE501eNA4mGgi2k0kbEOhryebkgQY/0vTEoB564pf7kLPnOX@vNPf5m0bP6yzgc3gE Try It Online!]
 
=={{header|zkl}}==
Line 1,195 ⟶ 3,344:
 
[[Extensible prime generator#zkl]] could be used instead.
<langsyntaxhighlight lang="zkl">var [const] BI=Import("zklBigNum"); // libGMP
cubans:=(1).walker(*).tweak('wrap(n){ // lazy iterator
p:=3*n*(n + 1) + 1;
Line 1,204 ⟶ 3,353:
 
cubans.drop(100_000 - cubans.n).value :
println("\nThe 100,000th cuban prime is: %,d".fmt(_));</langsyntaxhighlight>
{{out}}
<pre style="font-size:83%">
Line 1,232 ⟶ 3,381:
</pre>
Now lets get big.
<langsyntaxhighlight lang="zkl">k,z := BI(2).pow(128), 10;
println("First %d cuban primes where k = %,d:".fmt(z,k));
foreach n in ([BI(1)..]){
Line 1,238 ⟶ 3,387:
if(p.probablyPrime()){ println("%,d".fmt(p)); z-=1; }
if(z<=0) break;
}</langsyntaxhighlight>
{{out}}
<pre>
2,069

edits