Cuban primes: Difference between revisions

m
 
(5 intermediate revisions by 4 users not shown)
Line 33:
 
=={{header|ALGOL 68}}==
{{libheader|ALGOL 68-primes}}
<syntaxhighlight lang="algol68">
BEGIN
Line 38 ⟶ 39:
# 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 54 ⟶ 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 - 1;
FOR s FROM LWB sieve TO UPB sieve DO
IF sieve[ s ] THEN primes[ prime pos +:= 1 ] := s FI
OD;
 
# find the cuban primes #
Line 79 ⟶ 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 87 ⟶ 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
IF p <= UPB sieve THEN
is prime := sieve[ SHORTEN p ]
ELSE
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
FI;
IF is prime THEN
# have a cuban prime #
IF ( cuban count +:= 1 ) <= print limit THEN
Line 115 ⟶ 91:
{{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 1,145 ⟶ 1,121:
=={{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#}}==
Line 2,201 ⟶ 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 2,823 ⟶ 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}}==
1,995

edits