Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
3
Task/Cuban-primes/00-META.yaml
Normal file
3
Task/Cuban-primes/00-META.yaml
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
---
|
||||
from: http://rosettacode.org/wiki/Cuban_primes
|
||||
note: Prime Numbers
|
||||
31
Task/Cuban-primes/00-TASK.txt
Normal file
31
Task/Cuban-primes/00-TASK.txt
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
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 (3<sup>rd</sup> powers) play a role in its definition.
|
||||
|
||||
|
||||
;Some definitions of cuban primes:
|
||||
::* primes which are the difference of two consecutive cubes.
|
||||
::* primes of the form: (n+1)<sup>3</sup> - n<sup>3</sup>.
|
||||
::* primes of the form: n<sup>3</sup> - (n-1)<sup>3</sup>.
|
||||
::* primes ''p'' such that n<sup>2</sup>(''p''+n) is a cube for some n>0.
|
||||
::* primes ''p'' such that 4''p'' = 1 + 3n<sup>2</sup>.
|
||||
|
||||
|
||||
Cuban primes were named in 1923 by Allan Joseph Champneys Cunningham.
|
||||
|
||||
|
||||
;Task requirements:
|
||||
::* show the first 200 cuban primes (in a multi─line horizontal format).
|
||||
::* show the 100,000<sup>th</sup> cuban prime.
|
||||
::* show all cuban primes with commas (if appropriate).
|
||||
::* show all output here.
|
||||
|
||||
|
||||
Note that '''cuban prime''' isn't capitalized (as it doesn't refer to the nation of Cuba).
|
||||
|
||||
|
||||
;Also see:
|
||||
:* Wikipedia entry: [[wp:Cuban_prime|cuban prime]].
|
||||
:* MathWorld entry: [http://mathworld.wolfram.com/CubanPrime.html cuban prime].
|
||||
:* The OEIS entry: [[oeis:A002407|A002407]]. The 100,000<sup>th</sup> cuban prime can be verified in the 2<sup>nd</sup> ''example'' on this OEIS web page.
|
||||
<br><br>
|
||||
|
||||
75
Task/Cuban-primes/ALGOL-68/cuban-primes.alg
Normal file
75
Task/Cuban-primes/ALGOL-68/cuban-primes.alg
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
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) #
|
||||
|
||||
# returns a string representation of n with commas #
|
||||
PROC commatise = ( LONG INT n )STRING:
|
||||
BEGIN
|
||||
STRING result := "";
|
||||
STRING unformatted = whole( n, 0 );
|
||||
INT ch count := 0;
|
||||
FOR c FROM UPB unformatted BY -1 TO LWB unformatted DO
|
||||
IF ch count <= 2 THEN ch count +:= 1
|
||||
ELSE ch count := 1; "," +=: result
|
||||
FI;
|
||||
unformatted[ c ] +=: result
|
||||
OD;
|
||||
result
|
||||
END # commatise # ;
|
||||
|
||||
# sieve the primes #
|
||||
INT sieve max = 2 000 000;
|
||||
[ sieve max ]BOOL sieve; FOR i TO UPB sieve DO sieve[ i ] := TRUE OD;
|
||||
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 #
|
||||
INT cuban count := 0;
|
||||
LONG INT final cuban := 0;
|
||||
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
|
||||
LONG INT this cube = ( LENG n * n ) * n;
|
||||
LONG INT p = this cube - prev cube;
|
||||
prev cube := this cube;
|
||||
IF ODD p THEN
|
||||
# 2 is not a cuban prime so we only test odd numbers #
|
||||
BOOL is prime := TRUE;
|
||||
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 #
|
||||
cuban count +:= 1;
|
||||
IF cuban count <= print limit THEN
|
||||
# must show this cuban #
|
||||
STRING p formatted = commatise( p );
|
||||
print( ( " "[ UPB p formatted : ], p formatted ) );
|
||||
IF cuban count MOD 10 = 0 THEN print( ( newline ) ) FI
|
||||
FI;
|
||||
final cuban := p
|
||||
FI
|
||||
FI;
|
||||
cuban count < max cuban
|
||||
DO SKIP OD;
|
||||
IF cuban count MOD 10 /= 0 THEN print( ( newline ) ) FI;
|
||||
print( ( "The ", commatise( max cuban ), " cuban prime is: ", commatise( final cuban ), newline ) )
|
||||
END
|
||||
59
Task/Cuban-primes/AppleScript/cuban-primes-1.applescript
Normal file
59
Task/Cuban-primes/AppleScript/cuban-primes-1.applescript
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
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()
|
||||
28
Task/Cuban-primes/AppleScript/cuban-primes-2.applescript
Normal file
28
Task/Cuban-primes/AppleScript/cuban-primes-2.applescript
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
"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"
|
||||
18
Task/Cuban-primes/Arturo/cuban-primes.arturo
Normal file
18
Task/Cuban-primes/Arturo/cuban-primes.arturo
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
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]
|
||||
41
Task/Cuban-primes/BASIC256/cuban-primes.basic
Normal file
41
Task/Cuban-primes/BASIC256/cuban-primes.basic
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
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
|
||||
73
Task/Cuban-primes/Bracmat/cuban-primes.bracmat
Normal file
73
Task/Cuban-primes/Bracmat/cuban-primes.bracmat
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
( ( 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)
|
||||
)
|
||||
61
Task/Cuban-primes/C++/cuban-primes.cpp
Normal file
61
Task/Cuban-primes/C++/cuban-primes.cpp
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
#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;
|
||||
}
|
||||
59
Task/Cuban-primes/C-sharp/cuban-primes.cs
Normal file
59
Task/Cuban-primes/C-sharp/cuban-primes.cs
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
static class Program
|
||||
{
|
||||
static List<long> primes = new List<long>() { 3, 5 };
|
||||
|
||||
static void Main(string[] args)
|
||||
{
|
||||
const int cutOff = 200;
|
||||
const int bigUn = 100000;
|
||||
const int chunks = 50;
|
||||
const int little = bigUn / chunks;
|
||||
const string tn = " cuban prime";
|
||||
Console.WriteLine("The first {0:n0}{1}s:", cutOff, tn);
|
||||
int c = 0;
|
||||
bool showEach = true;
|
||||
long u = 0, v = 1;
|
||||
DateTime st = DateTime.Now;
|
||||
for (long i = 1; i <= long.MaxValue; i++)
|
||||
{
|
||||
bool found = false;
|
||||
int mx = System.Convert.ToInt32(Math.Ceiling(Math.Sqrt(v += (u += 6))));
|
||||
foreach (long item in primes)
|
||||
{
|
||||
if (item > mx) break;
|
||||
if (v % item == 0) { found = true; break; }
|
||||
}
|
||||
if (!found)
|
||||
{
|
||||
c += 1; if (showEach)
|
||||
{
|
||||
for (var z = primes.Last() + 2; z <= v - 2; z += 2)
|
||||
{
|
||||
bool fnd = false;
|
||||
foreach (long item in primes)
|
||||
{
|
||||
if (item > mx) break;
|
||||
if (z % item == 0) { fnd = true; break; }
|
||||
}
|
||||
if (!fnd) primes.Add(z);
|
||||
}
|
||||
primes.Add(v); Console.Write("{0,11:n0}", v);
|
||||
if (c % 10 == 0) Console.WriteLine();
|
||||
if (c == cutOff)
|
||||
{
|
||||
showEach = false;
|
||||
Console.Write("\nProgress to the {0:n0}th{1}: ", bigUn, tn);
|
||||
}
|
||||
}
|
||||
if (c % little == 0) { Console.Write("."); if (c == bigUn) break; }
|
||||
}
|
||||
}
|
||||
Console.WriteLine("\nThe {1:n0}th{2} is {0,17:n0}", v, c, tn);
|
||||
Console.WriteLine("Computation time was {0} seconds", (DateTime.Now - st).TotalSeconds);
|
||||
if (System.Diagnostics.Debugger.IsAttached) Console.ReadKey();
|
||||
}
|
||||
}
|
||||
112
Task/Cuban-primes/C/cuban-primes-1.c
Normal file
112
Task/Cuban-primes/C/cuban-primes-1.c
Normal file
|
|
@ -0,0 +1,112 @@
|
|||
#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;
|
||||
}
|
||||
35
Task/Cuban-primes/C/cuban-primes-2.c
Normal file
35
Task/Cuban-primes/C/cuban-primes-2.c
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
#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;
|
||||
}
|
||||
37
Task/Cuban-primes/Common-Lisp/cuban-primes.lisp
Normal file
37
Task/Cuban-primes/Common-Lisp/cuban-primes.lisp
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
;;; 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)
|
||||
62
Task/Cuban-primes/D/cuban-primes.d
Normal file
62
Task/Cuban-primes/D/cuban-primes.d
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
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);
|
||||
}
|
||||
3
Task/Cuban-primes/F-Sharp/cuban-primes-1.fs
Normal file
3
Task/Cuban-primes/F-Sharp/cuban-primes-1.fs
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
// 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))
|
||||
1
Task/Cuban-primes/F-Sharp/cuban-primes-2.fs
Normal file
1
Task/Cuban-primes/F-Sharp/cuban-primes-2.fs
Normal file
|
|
@ -0,0 +1 @@
|
|||
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)))
|
||||
1
Task/Cuban-primes/F-Sharp/cuban-primes-3.fs
Normal file
1
Task/Cuban-primes/F-Sharp/cuban-primes-3.fs
Normal file
|
|
@ -0,0 +1 @@
|
|||
printfn "\n\n%s" (cL(Seq.item 99999 cubans))
|
||||
13
Task/Cuban-primes/Factor/cuban-primes.factor
Normal file
13
Task/Cuban-primes/Factor/cuban-primes.factor
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
USING: formatting grouping io kernel lists lists.lazy math
|
||||
math.primes sequences tools.memory.private ;
|
||||
IN: rosetta-code.cuban-primes
|
||||
|
||||
: cuban-primes ( n -- seq )
|
||||
1 lfrom [ [ 3 * ] [ 1 + * ] bi 1 + ] <lazy-map>
|
||||
[ prime? ] <lazy-filter> ltake list>array ;
|
||||
|
||||
200 cuban-primes 10 <groups>
|
||||
[ [ commas ] map [ "%10s" printf ] each nl ] each nl
|
||||
|
||||
1e5 cuban-primes last commas "100,000th cuban prime is: %s\n"
|
||||
printf
|
||||
43
Task/Cuban-primes/Forth/cuban-primes.fth
Normal file
43
Task/Cuban-primes/Forth/cuban-primes.fth
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
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
|
||||
37
Task/Cuban-primes/FreeBASIC/cuban-primes.basic
Normal file
37
Task/Cuban-primes/FreeBASIC/cuban-primes.basic
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
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
|
||||
46
Task/Cuban-primes/Go/cuban-primes.go
Normal file
46
Task/Cuban-primes/Go/cuban-primes.go
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/big"
|
||||
)
|
||||
|
||||
func commatize(n uint64) string {
|
||||
s := fmt.Sprintf("%d", n)
|
||||
le := len(s)
|
||||
for i := le - 3; i >= 1; i -= 3 {
|
||||
s = s[0:i] + "," + s[i:]
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
func main() {
|
||||
var z big.Int
|
||||
var cube1, cube2, cube100k, diff uint64
|
||||
cubans := make([]string, 200)
|
||||
cube1 = 1
|
||||
count := 0
|
||||
for i := 1; ; i++ {
|
||||
j := i + 1
|
||||
cube2 = uint64(j * j * j)
|
||||
diff = cube2 - cube1
|
||||
z.SetUint64(diff)
|
||||
if z.ProbablyPrime(0) { // 100% accurate for z < 2 ^ 64
|
||||
if count < 200 {
|
||||
cubans[count] = commatize(diff)
|
||||
}
|
||||
count++
|
||||
if count == 100000 {
|
||||
cube100k = diff
|
||||
break
|
||||
}
|
||||
}
|
||||
cube1 = cube2
|
||||
}
|
||||
fmt.Println("The first 200 cuban primes are:-")
|
||||
for i := 0; i < 20; i++ {
|
||||
j := i * 10
|
||||
fmt.Printf("%9s\n", cubans[j : j+10]) // 10 per line say
|
||||
}
|
||||
fmt.Println("\nThe 100,000th cuban prime is", commatize(cube100k))
|
||||
}
|
||||
56
Task/Cuban-primes/Groovy/cuban-primes.groovy
Normal file
56
Task/Cuban-primes/Groovy/cuban-primes.groovy
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
15
Task/Cuban-primes/Haskell/cuban-primes-1.hs
Normal file
15
Task/Cuban-primes/Haskell/cuban-primes-1.hs
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
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
|
||||
6
Task/Cuban-primes/Haskell/cuban-primes-2.hs
Normal file
6
Task/Cuban-primes/Haskell/cuban-primes-2.hs
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
cubans :: [Int]
|
||||
cubans =
|
||||
[ x
|
||||
| n <- [1 ..]
|
||||
, let x = (succ n ^ 3) - (n ^ 3)
|
||||
, isPrime x ]
|
||||
60
Task/Cuban-primes/J/cuban-primes.j
Normal file
60
Task/Cuban-primes/J/cuban-primes.j
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
isPrime =: 1&p:
|
||||
assert 1 0 -: isPrime 3 9
|
||||
|
||||
|
||||
NB. difference, but first cube, of incremented y with y
|
||||
dcc =: -&(^&3)~ >:
|
||||
assert ((8 9 13^3)-7 8 12^3) -: dcc 7 8 12
|
||||
|
||||
Filter =: (#~`)(`:6)
|
||||
assert 2 3 5 7 11 13 -: isPrime Filter i. 16
|
||||
|
||||
cubanPrime =: [: isPrime Filter dcc
|
||||
assert 7 19 37 61 127 271 331 397 547 631 919 -: cubanPrime i. 20
|
||||
|
||||
NB. comatose copies with comma fill
|
||||
comatose =: (#!.','~ (1 1 1j1 1 1 1j1 1 1 1j1 1 1 1j1 1 1 1j1 1 1 1j1 1 1 1 {.~ -@:#))@:":&>
|
||||
assert (comatose 1000 1238 12 989832) -: [;._2 ] 0 :0
|
||||
1,000
|
||||
1,238
|
||||
12
|
||||
989,832
|
||||
)
|
||||
|
||||
CP =: cubanPrime i. 800000x
|
||||
# CP NB. tally, I've stored more than 100000 cuban primes
|
||||
103278
|
||||
NB. granted, I used wolframalpha Solve[(n+1)^3-n^3==1792617147127,n]
|
||||
|
||||
9!:17]2 2 NB. specify bottom right position in box
|
||||
|
||||
comatose&.> 10 20 $ CP
|
||||
┌─────────┬─────────┬─────────┬─────────┬─────────┬─────────┬─────────┬─────────┬─────────┬─────────┬─────────┬─────────┬─────────┬─────────┬─────────┬─────────┬─────────┬─────────┬─────────┬─────────┐
|
||||
│ 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│
|
||||
└─────────┴─────────┴─────────┴─────────┴─────────┴─────────┴─────────┴─────────┴─────────┴─────────┴─────────┴─────────┴─────────┴─────────┴─────────┴─────────┴─────────┴─────────┴─────────┴─────────┘
|
||||
|
||||
NB. the one hundred thousandth cuban prime
|
||||
comatose (<: 100000) { CP
|
||||
1,792,617,147,127
|
||||
|
||||
|
||||
cubanPrime f. NB. cubanPrime with fixed adverbs
|
||||
[: (#~ 1&p:) (-&(^&3)~ >:)
|
||||
57
Task/Cuban-primes/Java/cuban-primes.java
Normal file
57
Task/Cuban-primes/Java/cuban-primes.java
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
14
Task/Cuban-primes/Jq/cuban-primes-1.jq
Normal file
14
Task/Cuban-primes/Jq/cuban-primes-1.jq
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
# 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;
|
||||
13
Task/Cuban-primes/Jq/cuban-primes-2.jq
Normal file
13
Task/Cuban-primes/Jq/cuban-primes-2.jq
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
# 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)"
|
||||
25
Task/Cuban-primes/Julia/cuban-primes.julia
Normal file
25
Task/Cuban-primes/Julia/cuban-primes.julia
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
using Primes
|
||||
|
||||
function cubanprimes(N)
|
||||
cubans = zeros(Int, N)
|
||||
cube100k, cube1, count = 0, 1, 1
|
||||
for i in Iterators.countfrom(1)
|
||||
j = BigInt(i + 1)
|
||||
cube2 = j^3
|
||||
diff = cube2 - cube1
|
||||
if isprime(diff)
|
||||
count ≤ N && (cubans[count] = diff)
|
||||
if count == 100000
|
||||
cube100k = diff
|
||||
break
|
||||
end
|
||||
count += 1
|
||||
end
|
||||
cube1 = cube2
|
||||
end
|
||||
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 ", cube100k)
|
||||
end
|
||||
|
||||
cubanprimes(200)
|
||||
63
Task/Cuban-primes/Kotlin/cuban-primes.kotlin
Normal file
63
Task/Cuban-primes/Kotlin/cuban-primes.kotlin
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
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))
|
||||
}
|
||||
70
Task/Cuban-primes/Lua/cuban-primes-1.lua
Normal file
70
Task/Cuban-primes/Lua/cuban-primes-1.lua
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
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))
|
||||
42
Task/Cuban-primes/Lua/cuban-primes-2.lua
Normal file
42
Task/Cuban-primes/Lua/cuban-primes-2.lua
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
-- 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
|
||||
9
Task/Cuban-primes/Maple/cuban-primes.maple
Normal file
9
Task/Cuban-primes/Maple/cuban-primes.maple
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
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;
|
||||
8
Task/Cuban-primes/Mathematica/cuban-primes.math
Normal file
8
Task/Cuban-primes/Mathematica/cuban-primes.math
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
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]
|
||||
53
Task/Cuban-primes/Nim/cuban-primes.nim
Normal file
53
Task/Cuban-primes/Nim/cuban-primes.nim
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
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, ',')}"
|
||||
103
Task/Cuban-primes/Pascal/cuban-primes.pas
Normal file
103
Task/Cuban-primes/Pascal/cuban-primes.pas
Normal file
|
|
@ -0,0 +1,103 @@
|
|||
program CubanPrimes;
|
||||
{$IFDEF FPC}
|
||||
{$MODE DELPHI}
|
||||
{$OPTIMIZATION ON,Regvar,PEEPHOLE,CSE,ASMCSE}
|
||||
{$CODEALIGN proc=32}
|
||||
{$ENDIF}
|
||||
uses
|
||||
primTrial;
|
||||
const
|
||||
COLUMNCOUNT = 10*10;
|
||||
|
||||
procedure FormOut(Cuban:Uint64;ColSize:Uint32);
|
||||
var
|
||||
s : String;
|
||||
pI,pJ :pChar;
|
||||
i,j : NativeInt;
|
||||
Begin
|
||||
str(Cuban,s);
|
||||
i := length(s);
|
||||
If i>3 then
|
||||
Begin
|
||||
//extend s by the count of comma to be inserted
|
||||
j := i+ (i-1) div 3;
|
||||
setlength(s,j);
|
||||
pI := @s[i];
|
||||
pJ := @s[j];
|
||||
while i > 3 do
|
||||
Begin
|
||||
// copy 3 digits
|
||||
pJ^ := pI^;dec(pJ);dec(pI);
|
||||
pJ^ := pI^;dec(pJ);dec(pI);
|
||||
pJ^ := pI^;dec(pJ);dec(pI);
|
||||
// insert comma
|
||||
pJ^ := ',';dec(pJ);
|
||||
dec(i,3);
|
||||
end;
|
||||
//the digits in front are in the right place
|
||||
end;
|
||||
write(s:ColSize);
|
||||
end;
|
||||
|
||||
procedure OutFirstCntCubPrimes(Cnt : Int32;ColCnt : Int32);
|
||||
var
|
||||
cbDelta1,
|
||||
cbDelta2 : Uint64;
|
||||
ClCnt,ColSize : NativeInt;
|
||||
Begin
|
||||
If Cnt <= 0 then
|
||||
EXIT;
|
||||
IF ColCnt <= 0 then
|
||||
ColCnt := 1;
|
||||
ColSize := COLUMNCOUNT DIV ColCnt;
|
||||
dec(ColCnt);
|
||||
|
||||
ClCnt := ColCnt;
|
||||
cbDelta1 := 0;
|
||||
cbDelta2 := 1;
|
||||
|
||||
repeat
|
||||
if isPrime(cbDelta2) then
|
||||
Begin
|
||||
FormOut(cbDelta2,ColSize);
|
||||
dec(Cnt);
|
||||
|
||||
dec(ClCnt);
|
||||
If ClCnt < 0 then
|
||||
Begin
|
||||
Writeln;
|
||||
ClCnt := ColCnt;
|
||||
end;
|
||||
end;
|
||||
inc(cbDelta1,6);// 0,6,12,18...
|
||||
inc(cbDelta2,cbDelta1);//1,7,19,35...
|
||||
until Cnt<= 0;
|
||||
|
||||
writeln;
|
||||
end;
|
||||
|
||||
procedure OutNthCubPrime(n : Int32);
|
||||
var
|
||||
cbDelta1,
|
||||
cbDelta2 : Uint64;
|
||||
Begin
|
||||
If n <= 0 then
|
||||
EXIT;
|
||||
cbDelta1 := 0;
|
||||
cbDelta2 := 1;
|
||||
|
||||
repeat
|
||||
inc(cbDelta1,6);
|
||||
inc(cbDelta2,cbDelta1);
|
||||
if isPrime(cbDelta2) then
|
||||
dec(n);
|
||||
until n<=0;
|
||||
|
||||
FormOut(cbDelta2,20);
|
||||
writeln;
|
||||
end;
|
||||
|
||||
Begin
|
||||
OutFirstCntCubPrimes(200,10);
|
||||
OutNthCubPrime(100000);
|
||||
end.
|
||||
32
Task/Cuban-primes/Perl/cuban-primes.pl
Normal file
32
Task/Cuban-primes/Perl/cuban-primes.pl
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
use feature 'say';
|
||||
use ntheory 'is_prime';
|
||||
|
||||
sub cuban_primes {
|
||||
my ($n) = @_;
|
||||
|
||||
my @primes;
|
||||
for (my $k = 1 ; ; ++$k) {
|
||||
my $p = 3 * $k * ($k + 1) + 1;
|
||||
if (is_prime($p)) {
|
||||
push @primes, $p;
|
||||
last if @primes >= $n;
|
||||
}
|
||||
}
|
||||
|
||||
return @primes;
|
||||
}
|
||||
|
||||
sub commify {
|
||||
scalar reverse join ',', unpack '(A3)*', reverse shift;
|
||||
}
|
||||
|
||||
my @c = cuban_primes(200);
|
||||
|
||||
while (@c) {
|
||||
say join ' ', map { sprintf "%9s", commify $_ } splice(@c, 0, 10);
|
||||
}
|
||||
|
||||
say '';
|
||||
for my $n (1 .. 6) {
|
||||
say "10^$n-th cuban prime is: ", commify((cuban_primes(10**$n))[-1]);
|
||||
}
|
||||
34
Task/Cuban-primes/Phix/cuban-primes.phix
Normal file
34
Task/Cuban-primes/Phix/cuban-primes.phix
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
(phixonline)-->
|
||||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
|
||||
<span style="color: #008080;">include</span> <span style="color: #004080;">mpfr</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
|
||||
<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>
|
||||
<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>
|
||||
<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>
|
||||
<span style="color: #000000;">pn</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_init</span><span style="color: #0000FF;">()</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;">"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>
|
||||
<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>
|
||||
<!--
|
||||
67
Task/Cuban-primes/Python/cuban-primes.py
Normal file
67
Task/Cuban-primes/Python/cuban-primes.py
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
import datetime
|
||||
import math
|
||||
|
||||
primes = [ 3, 5 ]
|
||||
|
||||
cutOff = 200
|
||||
|
||||
bigUn = 100_000
|
||||
chunks = 50
|
||||
little = bigUn / chunks
|
||||
|
||||
tn = " cuban prime"
|
||||
print ("The first {:,}{}s:".format(cutOff, tn))
|
||||
|
||||
c = 0
|
||||
showEach = True
|
||||
u = 0
|
||||
v = 1
|
||||
st = datetime.datetime.now()
|
||||
|
||||
for i in range(1, int(math.pow(2,20))):
|
||||
found = False
|
||||
u += 6
|
||||
v += u
|
||||
mx = int(math.sqrt(v))
|
||||
|
||||
for item in primes:
|
||||
if (item > mx):
|
||||
break
|
||||
if (v % item == 0):
|
||||
found = True
|
||||
break
|
||||
|
||||
if (found == 0):
|
||||
c += 1
|
||||
if (showEach):
|
||||
z = primes[-1]
|
||||
while (z <= v - 2):
|
||||
z += 2
|
||||
|
||||
fnd = False
|
||||
for item in primes:
|
||||
if (item > mx):
|
||||
break
|
||||
if (z % item == 0):
|
||||
fnd = True
|
||||
break
|
||||
|
||||
if (not fnd):
|
||||
primes.append(z)
|
||||
|
||||
primes.append(v)
|
||||
print("{:>11,}".format(v), end='')
|
||||
|
||||
if (c % 10 == 0):
|
||||
print("");
|
||||
if (c == cutOff):
|
||||
showEach = False
|
||||
print ("Progress to the {:,}th {}:".format(bigUn, tn), end='')
|
||||
if (c % little == 0):
|
||||
print('.', end='')
|
||||
if (c == bigUn):
|
||||
break
|
||||
|
||||
print("");
|
||||
print ("The {:,}th{} is {:,}".format(c, tn, v))
|
||||
print("Computation time was {} seconds".format((datetime.datetime.now() - st).seconds))
|
||||
27
Task/Cuban-primes/REXX/cuban-primes.rexx
Normal file
27
Task/Cuban-primes/REXX/cuban-primes.rexx
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
/*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.*/
|
||||
sw= linesize() - 1; if sw<1 then sw= 79 /*obtain width of the terminal screen. */
|
||||
w=12; #= 1; $= right(7, w) /*start with first cuban prime; count.*/
|
||||
do j=1 until #=>N; x= (j+1)**3 - j**3 /*compute a possible cuban prime. */
|
||||
parse var x '' -1 _; if @._ then iterate /*check last digit for non─cuban prime.*/
|
||||
do k=1 until km*km>x; km= k*6 + 1 /*cuban primes can't be ÷ by 6k+1 */
|
||||
if x//km==0 then iterate j /*Divisible? Then not a cuban prime. */
|
||||
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 /* [↑] try to fit as many #s per line.*/
|
||||
cx= commas(x); L= length(cx) /*insert commas──►X; obtain the length.*/
|
||||
cx= right(cx, max(w, L) ); new= $ cx /*right justify CX; concat to new list*/
|
||||
if length(new)>sw then do; say $; $= cx /*line is too long, display #'s so far.*/
|
||||
end /* [↑] initialize the (new) next line.*/
|
||||
else $= new /*start with cuban # that wouldn't fit.*/
|
||||
end /*j*/
|
||||
if \Nth & $\=='' then say $ /*check for 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 _
|
||||
38
Task/Cuban-primes/Racket/cuban-primes.rkt
Normal file
38
Task/Cuban-primes/Racket/cuban-primes.rkt
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
#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))
|
||||
10
Task/Cuban-primes/Raku/cuban-primes-1.raku
Normal file
10
Task/Cuban-primes/Raku/cuban-primes-1.raku
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
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]., # zero indexed
|
||||
9
Task/Cuban-primes/Raku/cuban-primes-2.raku
Normal file
9
Task/Cuban-primes/Raku/cuban-primes-2.raku
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
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 '';
|
||||
}
|
||||
5
Task/Cuban-primes/Raku/cuban-primes-3.raku
Normal file
5
Task/Cuban-primes/Raku/cuban-primes-3.raku
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
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];
|
||||
20
Task/Cuban-primes/Ring/cuban-primes.ring
Normal file
20
Task/Cuban-primes/Ring/cuban-primes.ring
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
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
|
||||
22
Task/Cuban-primes/Ruby/cuban-primes.rb
Normal file
22
Task/Cuban-primes/Ruby/cuban-primes.rb
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
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."
|
||||
42
Task/Cuban-primes/Rust/cuban-primes.rust
Normal file
42
Task/Cuban-primes/Rust/cuban-primes.rust
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
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);
|
||||
}
|
||||
40
Task/Cuban-primes/Scala/cuban-primes.scala
Normal file
40
Task/Cuban-primes/Scala/cuban-primes.scala
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
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)
|
||||
}
|
||||
}
|
||||
11
Task/Cuban-primes/Sidef/cuban-primes.sidef
Normal file
11
Task/Cuban-primes/Sidef/cuban-primes.sidef
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
func cuban_primes(n) {
|
||||
1..Inf -> lazy.map {|k| 3*k*(k+1) + 1 }\
|
||||
.grep{ .is_prime }\
|
||||
.first(n)
|
||||
}
|
||||
|
||||
cuban_primes(200).slices(10).each {
|
||||
say .map { "%9s" % .commify }.join(' ')
|
||||
}
|
||||
|
||||
say ("\n100,000th cuban prime is: ", cuban_primes(1e5).last.commify)
|
||||
47
Task/Cuban-primes/Transd/cuban-primes.transd
Normal file
47
Task/Cuban-primes/Transd/cuban-primes.transd
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
#lang transd
|
||||
|
||||
MainModule: {
|
||||
primes: Vector<ULong>([3, 5]),
|
||||
lim: 200,
|
||||
bigUn: 100000,
|
||||
chunks: 50,
|
||||
little: 0,
|
||||
c: 0,
|
||||
showEach: true,
|
||||
u: ULong(0),
|
||||
v: ULong(1),
|
||||
|
||||
_start: (λ found Bool() fnd Bool() mx Int() z ULong()
|
||||
(= little (/ bigUn chunks))
|
||||
(for i in Range(1 (pow 2 20)) do
|
||||
(= found false)
|
||||
(+= u 6) (+= v u) (= mx (to-Int (sqrt v) 1))
|
||||
(for item in primes do
|
||||
(if (> item mx) break)
|
||||
(if (not (mod v item)) (= found true)
|
||||
break))
|
||||
(if (not found)
|
||||
(+= c 1)
|
||||
(if showEach
|
||||
(= z (get primes -1))
|
||||
(while (< z (- v 2))
|
||||
(+= z 2) (= fnd false)
|
||||
(for item in primes do
|
||||
(if (> item mx) break)
|
||||
(if (not (mod z item)) (= fnd true)
|
||||
break))
|
||||
(if (not fnd) (append primes z)))
|
||||
(append primes v)
|
||||
(textout :width 11 :group v)
|
||||
(if (not (mod c 10)) (textout :nl))
|
||||
(if (== c lim) (= showEach false)
|
||||
(textout "Progress to the " :group bigUn
|
||||
"'th cuban prime:" ))
|
||||
)
|
||||
(if (not (mod c little)) (textout "."))
|
||||
(if (== c bigUn) break)
|
||||
)
|
||||
)
|
||||
(lout "The " :group c "'th cuban prime is " v )
|
||||
)
|
||||
}
|
||||
37
Task/Cuban-primes/Visual-Basic-.NET/cuban-primes-1.vb
Normal file
37
Task/Cuban-primes/Visual-Basic-.NET/cuban-primes-1.vb
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
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
|
||||
39
Task/Cuban-primes/Visual-Basic-.NET/cuban-primes-2.vb
Normal file
39
Task/Cuban-primes/Visual-Basic-.NET/cuban-primes-2.vb
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
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
|
||||
63
Task/Cuban-primes/Wren/cuban-primes.wren
Normal file
63
Task/Cuban-primes/Wren/cuban-primes.wren
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
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")
|
||||
10
Task/Cuban-primes/Zkl/cuban-primes-1.zkl
Normal file
10
Task/Cuban-primes/Zkl/cuban-primes-1.zkl
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
var [const] BI=Import("zklBigNum"); // libGMP
|
||||
cubans:=(1).walker(*).tweak('wrap(n){ // lazy iterator
|
||||
p:=3*n*(n + 1) + 1;
|
||||
BI(p).probablyPrime() and p or Void.Skip
|
||||
});
|
||||
println("First 200 cuban primes:");
|
||||
do(20){ (10).pump(String, cubans.next, "%10,d".fmt).println() }
|
||||
|
||||
cubans.drop(100_000 - cubans.n).value :
|
||||
println("\nThe 100,000th cuban prime is: %,d".fmt(_));
|
||||
7
Task/Cuban-primes/Zkl/cuban-primes-2.zkl
Normal file
7
Task/Cuban-primes/Zkl/cuban-primes-2.zkl
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
k,z := BI(2).pow(128), 10;
|
||||
println("First %d cuban primes where k = %,d:".fmt(z,k));
|
||||
foreach n in ([BI(1)..]){
|
||||
p:=( (k + n).pow(3) - n.pow(3) )/k;
|
||||
if(p.probablyPrime()){ println("%,d".fmt(p)); z-=1; }
|
||||
if(z<=0) break;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue