Cuban primes: Difference between revisions

m
 
(22 intermediate revisions by 19 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 33:
 
=={{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 53 ⟶ 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 78 ⟶ 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 86 ⟶ 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 107 ⟶ 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 ⟶ 114:
The 100,000 cuban prime is: 1,792,617,147,127
</pre>
 
=={{header|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!
 
<syntaxhighlight lang="applescript">on isPrime(n)
-- 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)
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)
set groups to {}
repeat while (int > 999)
set groups's beginning to ((1000 + (int mod 1000 as integer)) as text)'s text 2 thru 4
set int to int div 1000
end repeat
set groups's beginning to int
return join(groups, separator)
end intToText
 
on task()
set output to {"The first 200 cuban primes are:"}
set inc to 0
set candidate to 1
set counter to 0
set row to {}
repeat until (counter = 200)
set inc to inc + 6
set candidate to candidate + inc
if (isPrime(candidate)) then
set counter to counter + 1
set end of row to (" " & intToText(candidate, ","))'s text -11 thru -1
if ((counter) mod 8 = 0) then
set end of output to join(row, "")
set row to {}
end if
end if
end repeat
repeat until (counter = 100000)
set inc to inc + 6
set candidate to candidate + inc
if (isPrime(candidate)) then set counter to counter + 1
end repeat
set end of output to linefeed & "The 100,000th is " & intToText(candidate, ",")
return join(output, linefeed)
end task
 
task()</syntaxhighlight>
 
{{output}}
<syntaxhighlight lang="applescript">"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 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:
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 49.5868152 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}}
<langsyntaxhighlight Bracmatlang="bracmat">( ( cubanprimes
=
. !arg:(?N.?bigN)
Line 208 ⟶ 572:
)
& cubanprimes$(200.100000)
)</langsyntaxhighlight>
{{out}}
<pre>The first 200 cuban primes are:
Line 236 ⟶ 600:
=={{header|C}}==
{{trans|C++}}
<langsyntaxhighlight Clang="c">#include <limits.h>
#include <math.h>
#include <stdbool.h>
Line 347 ⟶ 711:
deallocate(&primes);
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>The first 200 cuban primes:
Line 376 ⟶ 740:
===using GMP===
So that we can go arbitrarily large, and it's faster to boot.
<langsyntaxhighlight Clang="c">#include <gmp.h>
#include <stdio.h>
 
Line 411 ⟶ 775:
return 0;
}
</syntaxhighlight>
</lang>
 
=={{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 474 ⟶ 838:
if (System.Diagnostics.Debugger.IsAttached) Console.ReadKey();
}
}</langsyntaxhighlight>
{{out}}
<pre>The first 200 cuban primes:
Line 504 ⟶ 868:
=={{header|C++}}==
{{trans|C#}}
<langsyntaxhighlight Cpplang="cpp">#include <iostream>
#include <vector>
#include <chrono>
Line 564 ⟶ 928:
cout << "\nComputation time was " << elapsed_seconds.count() << " seconds" << endl;
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>The first 200 cuban primes:
Line 602 ⟶ 966:
with multiple columns, commas and all.
 
<langsyntaxhighlight lang="lisp">;;; Show the first 200 and the 100,000th cuban prime.
;;; Cuban primes are the difference of 2 consecutive cubes.
 
Line 638 ⟶ 1,002:
(car (last (cuban 100000))))
 
(princ #\newline)</langsyntaxhighlight>
{{out}}
<pre>
Line 668 ⟶ 1,032:
=={{header|D}}==
{{trans|C#}}
<langsyntaxhighlight lang="d">import std.math;
import std.stdio;
 
Line 729 ⟶ 1,093:
}
writefln("\nThe %sth%s is %17s", c, tn, v);
}</langsyntaxhighlight>
{{out}}
<pre>The first 200 cuban primes:
Line 755 ⟶ 1,119:
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#)]
<langsyntaxhighlight 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>
</lang>
===The Task===
<langsyntaxhighlight 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>
</lang>
{{out}}
<pre>
Line 796 ⟶ 1,224:
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>
<langsyntaxhighlight lang="fsharp">
printfn "\n\n%s" (cL(Seq.item 99999 cubans))
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 806 ⟶ 1,234:
=={{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 818 ⟶ 1,246:
 
1e5 cuban-primes last commas "100,000th cuban prime is: %s\n"
printf</langsyntaxhighlight>
{{out}}
<pre>
Line 843 ⟶ 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 891 ⟶ 1,392:
}
fmt.Println("\nThe 100,000th cuban prime is", commatize(cube100k))
}</langsyntaxhighlight>
 
{{out}}
Line 919 ⟶ 1,420:
The 100,000th cuban prime is 1,792,617,147,127
</pre>
 
=={{header|Groovy}}==
{{trans|Java}}
<langsyntaxhighlight lang="groovy">class CubanPrimes {
private static int MAX = 1_400_000
private static boolean[] primes = new boolean[MAX]
Line 976 ⟶ 1,478:
}
}
}</langsyntaxhighlight>
{{out}}
<pre> 7 19 37 61 127 271 331 397 547 631
Line 1,007 ⟶ 1,509:
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
<langsyntaxhighlight lang="haskell">import Data.Numbers.Primes (isPrime)
import Data.List (intercalate)
import Data.List.Split (chunksOf)
Line 1,021 ⟶ 1,523:
where
rows = chunksOf 10 . take 200
thousands = reverse . intercalate "," . chunksOf 3 . reverse . show</langsyntaxhighlight>
 
Where filter over map could also be expressed in terms of concatMap, or a list comprehension:
 
<langsyntaxhighlight lang="haskell">cubans :: [Int]
cubans =
[ x
| n <- [1 ..]
, let x = (succ n ^ 3) - (n ^ 3)
, isPrime x ]</langsyntaxhighlight>
 
{{out}}
Line 1,060 ⟶ 1,562:
=={{header|J}}==
I've used assertions to demonstrate and to prove the defined verbs
<syntaxhighlight lang="j">
<lang j>
 
isPrime =: 1&p:
Line 1,123 ⟶ 1,625:
[: (#~ 1&p:) (-&(^&3)~ >:)
</syntaxhighlight>
</lang>
 
=={{header|Java}}==
<syntaxhighlight lang="java">
<lang Java>
public class CubanPrimes {
 
Line 1,184 ⟶ 1,686:
}
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,213 ⟶ 1,715:
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}}==
Line 1,218 ⟶ 1,788:
{{trans|Go}}
{{works with|Julia|1.2}}
<langsyntaxhighlight lang="julia">using Primes
 
function cubanprimes(N)
Line 1,243 ⟶ 1,813:
 
cubanprimes(200)
</langsyntaxhighlight>{{out}}
<pre>
The first 200 cuban primes are:
Line 1,277 ⟶ 1,847:
=={{header|Kotlin}}==
{{trans|D}}
<langsyntaxhighlight lang="scala">import kotlin.math.ceil
import kotlin.math.sqrt
 
Line 1,339 ⟶ 1,909:
}
println("\nThe %dth cuban prime is %17d".format(c, v))
}</langsyntaxhighlight>
{{out}}
<pre>The first 200 cuban primes:
Line 1,369 ⟶ 1,939:
===Original===
{{trans|D}}
<langsyntaxhighlight lang="lua">local primes = {3, 5}
local cutOff = 200
local bigUn = 100000
Line 1,438 ⟶ 2,008:
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))</langsyntaxhighlight>
{{out}}
<pre>The first 200 cuban primes
Line 1,466 ⟶ 2,036:
===Alternate===
Perhaps a more "readable" structure, and with specified formatting..
<langsyntaxhighlight lang="lua">-- cuban primes in Lua (alternate version 6/12/2020 db)
------------------
-- PRIME SUPPORT:
Line 1,508 ⟶ 2,078:
n = n + 1
end
</syntaxhighlight>
</lang>
{{out}}
<pre> 7 19 37 61 127 271 331 397 547 631
Line 1,537 ⟶ 2,107:
{{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>}}
 
<langsyntaxhighlight lang="maple">CubanPrimes := proc(n) local i, cp;
cp := Array([]);
for i by 2 while numelems(cp) < n do
Line 1,545 ⟶ 2,115:
end do;
return cp;
end proc;</langsyntaxhighlight>
{{out}}
<pre> The first 200 cuban primes are
Line 1,552 ⟶ 2,122:
The 100000th cuban prime is: 1792617147127</pre>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">cubans[m_Integer] := Block[{n = 1, result = {}, candidate},
While[Length[result] < m,
n++;
Line 1,560 ⟶ 2,130:
result]
cubans[200]
NumberForm[Last[cubans[100000]], NumberSeparator -> ",", DigitBlock -> 3]</langsyntaxhighlight>
 
{{out}}
Line 1,592 ⟶ 2,162:
=={{header|Nim}}==
{{trans|C#}}
<langsyntaxhighlight lang="nim">import strformat
import strutils
import math
Line 1,644 ⟶ 2,214:
break
write(stdout, "\n")
echo fmt"The {c}th cuban prime is {insertSep($v, ',')}"</langsyntaxhighlight>
{{out}}
<pre>The first 200 cuban primes
Line 1,669 ⟶ 2,239:
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 1,675 ⟶ 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 1,777 ⟶ 2,411:
OutFirstCntCubPrimes(200,10);
OutNthCubPrime(100000);
end.</langsyntaxhighlight>
{{out}}
<pre> 7 19 37 61 127 271 331 397 547 631
Line 1,804 ⟶ 2,438:
=={{header|Perl}}==
{{libheader|ntheory}}
<langsyntaxhighlight lang="perl">use feature 'say';
use ntheory 'is_prime';
 
Line 1,835 ⟶ 2,469:
for my $n (1 .. 6) {
say "10^$n-th cuban prime is: ", commify((cuban_primes(10**$n))[-1]);
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,869 ⟶ 2,503:
=={{header|Phix}}==
{{libheader|Phix/mpfr}}
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>include mpfr.e
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
integer np = 0,
<span style="color: #008080;">include</span> <span style="color: #004080;">mpfr</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
i = 2
<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>
mpz p3 = mpz_init(1*1*1),
<span style="color: #000000;">i</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">2</span>
i3 = mpz_init(),
<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>
p = mpz_init(),
<span style="color: #000000;">i3</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_init</span><span style="color: #0000FF;">(),</span>
pn = mpz_init()
<span style="color: #000000;">p</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_init</span><span style="color: #0000FF;">(),</span>
atom randstate = gmp_randinit_mt()
<span style="color: #000000;">pn</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_init</span><span style="color: #0000FF;">()</span>
 
printf(1,"The first 200 cuban primes are:\n")
<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>
sequence first200 = {}
<span style="color: #004080;">sequence</span> <span style="color: #000000;">first200</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
atom t0 = time()
<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>
while np<100000 do
<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>
mpz_ui_pow_ui(i3,i,3)
<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>
mpz_sub(p,i3,p3)
<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>
if mpz_probable_prime_p(p,randstate) then
<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>
mpz_set(pn,p)
<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>
np += 1
<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>
if np<=200 then
<span style="color: #000000;">np</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
first200 = append(first200,sprintf("%,9d",mpz_get_integer(pn)))
<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>
if mod(np,10)=0 then
<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>
printf(1,"%s\n",join(first200[-10..-1]))
<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>
end if
<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>
end if
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end if
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
mpz_set(p3,i3)
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
i += 1
<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>
end while
<span style="color: #000000;">i</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
printf(1,"\nThe %,dth cuban prime is %s\n",{np,mpz_get_str(pn,comma_fill:=true)})
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
randstate = gmp_randclear(randstate)
<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>
{p3,i3,p} = mpz_free({p3,i3,p})
<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>
?elapsed(time()-t0)</lang>
<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>
Line 1,932 ⟶ 2,568:
 
{{trans|C#}}
<syntaxhighlight lang="python">
<lang Python>
import datetime
import math
Line 2,000 ⟶ 2,636:
print ("The {:,}th{} is {:,}".format(c, tn, v))
print("Computation time was {} seconds".format((datetime.datetime.now() - st).seconds))
</syntaxhighlight>
</lang>
 
{{out}}
Line 2,029 ⟶ 2,665:
 
=={{header|Racket}}==
<langsyntaxhighlight lang="racket">#lang racket
 
(require math/number-theory
Line 2,066 ⟶ 2,702:
(progress-report x)
p)
(newline))</langsyntaxhighlight>
 
{{out}}
Line 2,094 ⟶ 2,730:
=={{header|Raku}}==
(formerly Perl 6)
{{libheader|ntheory}}
{{works with|Rakudo|2018.12}}
 
===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" perl6line>use Lingua::EN::Numbers;
use ntheory:from<Perl5> <:all>;
 
Line 2,107 ⟶ 2,742:
put '';
 
put @cubans[99_999].&comma; # zero indexed</langsyntaxhighlight>
 
{{out}}
Line 2,139 ⟶ 2,774:
 
Here are the first 20 for each valid k up to 10:
<syntaxhighlight lang="raku" perl6line>sub comma { $^i.flip.comb(3).join(',').flip }
 
for 2..10 -> \k {
Line 2,148 ⟶ 2,783:
put '';
}
</syntaxhighlight>
</lang>
{{out}}
<pre>First 20 cuban primes where k = 2:
Line 2,176 ⟶ 2,811:
===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" perl6line>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];</langsyntaxhighlight>
<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
Line 2,198 ⟶ 2,833:
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.
<langsyntaxhighlight 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.*/
Line 2,224 ⟶ 2,859:
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;
Line 2,263 ⟶ 2,898:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
load "stdlib.ring"
 
Line 2,284 ⟶ 2,919:
 
see "done..." + nl
</syntaxhighlight>
</lang>
Output:
<pre>
Line 2,290 ⟶ 2,925:
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}}==
<langsyntaxhighlight lang="ruby">require "openssl"
RE = /(\d)(?=(\d\d\d)+(?!\d))/ # Activesupport uses this for commatizing
Line 2,313 ⟶ 2,963:
puts "
100_000th cuban prime is #{commatize( cuban_primes.take(100_000).last)}
which took #{(Time.now-t0).round} seconds to calculate."</langsyntaxhighlight>
{{out}}
<pre> 7 19 37 61 127 271 331 397 547 631
Line 2,342 ⟶ 2,992:
=={{header|Rust}}==
Uses the libraries [https://crates.io/crates/primal primal] and [https://crates.io/crates/separator separator]
<langsyntaxhighlight lang="rust">use std::time::Instant;
use separator::Separatable;
 
Line 2,383 ⟶ 3,033:
println!("The {}th cuban prime number is {}", LAST_CUBAN_PRIME, cuban.separated_string());
println!("Elapsed time: {:?}", elapsed);
}</langsyntaxhighlight>
{{out}}
<pre>Calculating the first 200 cuban primes and the 100000th cuban prime...
Line 2,416 ⟶ 3,066:
 
Spire's SafeLong is used instead of Java's BigInt for performance.
<langsyntaxhighlight lang="scala">import spire.math.SafeLong
import spire.implicits._
 
Line 2,455 ⟶ 3,105:
fHelper(Vector[String](), formatted)
}
}</langsyntaxhighlight>
 
{{out}}
Line 2,481 ⟶ 3,131:
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">func cuban_primes(n) {
1..Inf -> lazy.map {|k| 3*k*(k+1) + 1 }\
.grep{ .is_prime }\
Line 2,491 ⟶ 3,141:
}
 
say ("\n100,000th cuban prime is: ", cuban_primes(1e5).last.commify)</langsyntaxhighlight>
{{out}}
<pre>
Line 2,517 ⟶ 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 2,580 ⟶ 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===
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.
<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</lang>
{{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 49.5868152 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|Wren}}==
{{trans|Python}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight ecmascriptlang="wren">import "./fmt" for Fmt
 
var start = System.clock
Line 2,718 ⟶ 3,308:
 
System.print("\nThe 100,000th cuban prime is %(bigCuban)")
System.print("\nTook %(System.clock - start) secs")</langsyntaxhighlight>
 
{{out}}
Line 2,754 ⟶ 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 2,763 ⟶ 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 2,791 ⟶ 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 2,797 ⟶ 3,387:
if(p.probablyPrime()){ println("%,d".fmt(p)); z-=1; }
if(z<=0) break;
}</langsyntaxhighlight>
{{out}}
<pre>
1,983

edits