Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
3
Task/Pierpont-primes/00-META.yaml
Normal file
3
Task/Pierpont-primes/00-META.yaml
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
---
|
||||
from: http://rosettacode.org/wiki/Pierpont_primes
|
||||
note: Prime Numbers
|
||||
28
Task/Pierpont-primes/00-TASK.txt
Normal file
28
Task/Pierpont-primes/00-TASK.txt
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
A Pierpont prime is a prime number of the form: <big>'''2<sup>''u''</sup>3<sup>''v''</sup> + 1'''</big> for some non-negative integers <big>''' ''u'' '''</big> and <big>''' ''v'' '''</big>.
|
||||
|
||||
|
||||
A Pierpont prime of the second kind is a prime number of the form: <big>'''2<sup>''u''</sup>3<sup>''v''</sup> - 1'''</big> for some non-negative integers <big>''' ''u'' '''</big> and <big>''' ''v'' '''</big>.
|
||||
|
||||
|
||||
''The term "Pierpont primes" is generally understood to mean the first definition, but will be called "Pierpont primes of the first kind" on this page to distinguish them.
|
||||
|
||||
|
||||
;Task:
|
||||
|
||||
:* Write a routine (function, procedure, whatever) to find Pierpont primes of the first & second kinds.
|
||||
|
||||
:* Use the routine to find and display here, on this page, the first '''50 Pierpont primes of the first kind'''.
|
||||
|
||||
:* Use the routine to find and display here, on this page, the first '''50 Pierpont primes of the second kind'''
|
||||
|
||||
:* If your language supports large integers, find and display here, on this page, the '''250<sup>th</sup> Pierpont prime of the first kind''' and the '''250<sup>th</sup> Pierpont prime of the second kind'''.
|
||||
|
||||
|
||||
;See also:
|
||||
|
||||
:* '''[[wp:Pierpont_prime|Wikipedia - Pierpont primes]]'''
|
||||
:* '''[[oeis:A005109|OEIS:A005109 - Class 1 -, or Pierpont primes]]'''
|
||||
:* '''[[oeis:A005105|OEIS:A005105 - Class 1 +, or Pierpont primes of the second kind]]'''
|
||||
|
||||
<br>
|
||||
|
||||
96
Task/Pierpont-primes/ALGOL-68/pierpont-primes.alg
Normal file
96
Task/Pierpont-primes/ALGOL-68/pierpont-primes.alg
Normal file
|
|
@ -0,0 +1,96 @@
|
|||
BEGIN # find some pierpoint primes of the first kind (2^u3^v + 1) #
|
||||
# and second kind (2^u3^v - 1) #
|
||||
# construct a sieve of primes up to 10 000 #
|
||||
PR read "primes.incl.a68" PR
|
||||
[]BOOL prime = PRIMESIEVE 10 000;
|
||||
# returns the minimum of a and b #
|
||||
PROC min = ( INT a, b )INT: IF a < b THEN a ELSE b FI;
|
||||
# returns TRUE if p is prime, FALSE otherwise #
|
||||
PROC is prime = ( INT p )BOOL:
|
||||
IF NOT ODD p
|
||||
THEN p = 2
|
||||
ELIF p <= UPB prime
|
||||
THEN prime[ p ] # small enough to use the sieve #
|
||||
ELSE # use trial division by the sieved primes #
|
||||
BOOL probably prime := TRUE;
|
||||
FOR d FROM 3 BY 2 WHILE d * d <= p AND probably prime DO
|
||||
IF prime[ d ] THEN probably prime := p MOD d /= 0 FI
|
||||
OD;
|
||||
probably prime
|
||||
FI; # is prime #
|
||||
# sets the elements of pp1 to the first UPB pp1 Pierpoint primes of the #
|
||||
# first kind and pp2 to the first UPB pp2 Pierpoint primes of the #
|
||||
# second kind #
|
||||
PROC find pierpoint = ( REF[]INT pp1, pp2 )VOID:
|
||||
BEGIN
|
||||
# saved 3-smooth values #
|
||||
# - only the currently active part of the seuence is kept #
|
||||
INT three smooth limit = 33;
|
||||
[ 1 : three smooth limit * 3 ]INT s3s; FOR i TO UPB s3s DO s3s[ i ] := 0 OD;
|
||||
INT pp1 pos := LWB pp1 - 1, pp2 pos := LWB pp2 - 1;
|
||||
INT pp1 max = UPB pp1;
|
||||
INT pp2 max = UPB pp2;
|
||||
INT p2, p3, last2, last3;
|
||||
INT s pos := s3s[ 1 ] := last2 := last3 := p2 := p3 := 1;
|
||||
FOR n FROM 2 WHILE pp1 pos < pp1 max OR pp2 pos < pp2 max DO
|
||||
# the next 3-smooth number is the lowest of the next #
|
||||
# multiples of 2 and 3 #
|
||||
INT m = min( p2, p3 );
|
||||
IF pp1 pos < pp1 max THEN
|
||||
IF INT first = m + 1;
|
||||
is prime( first )
|
||||
THEN # have a Pierpoint prime of the first kind #
|
||||
pp1[ pp1 pos +:= 1 ] := first
|
||||
FI
|
||||
FI;
|
||||
IF pp2 pos < pp2 max THEN
|
||||
IF INT second = m - 1;
|
||||
is prime( second )
|
||||
THEN # have a Pierpoint prime of the second kind #
|
||||
pp2[ pp2 pos +:= 1 ] := second
|
||||
FI
|
||||
FI;
|
||||
s3s[ s pos +:= 1 ] := m;
|
||||
IF m = p2 THEN p2 := 2 * s3s[ last2 +:= 1 ] FI;
|
||||
IF m = p3 THEN p3 := 3 * s3s[ last3 +:= 1 ] FI;
|
||||
INT last min = IF last2 > last3 THEN last3 ELSE last2 FI;
|
||||
IF last min > three smooth limit THEN
|
||||
# shuffle the sequence down, over the now unused bits #
|
||||
INT new pos := 0;
|
||||
FOR pos FROM last min TO s pos DO
|
||||
s3s[ new pos +:= 1 ] := s3s[ pos ]
|
||||
OD;
|
||||
INT diff := last min - 1;
|
||||
last2 -:= diff;
|
||||
last3 -:= diff;
|
||||
s pos -:= diff
|
||||
FI
|
||||
OD
|
||||
END; # find pierpoint #
|
||||
# prints a table of Pierpoint primes of the specified kind #
|
||||
PROC print pierpoint = ( []INT primes, STRING kind )VOID:
|
||||
BEGIN
|
||||
print( ( "The first "
|
||||
, whole( ( UPB primes + 1 ) - LWB primes, 0 )
|
||||
, " Pierpoint primes of the "
|
||||
, kind
|
||||
, " kind:"
|
||||
, newline
|
||||
)
|
||||
);
|
||||
INT p count := 0;
|
||||
FOR i FROM LWB primes TO UPB primes DO
|
||||
print( ( " ", whole( primes[ i ], -8 ) ) );
|
||||
IF ( p count +:= 1 ) MOD 10 = 0 THEN print( ( newline ) ) FI
|
||||
OD;
|
||||
print( ( newline ) )
|
||||
END; # print pierpoint #
|
||||
# find the first 50 Pierpoint primes of the first and second kinds #
|
||||
INT max pierpoint = 50;
|
||||
[ 1 : max pierpoint ]INT pfirst;
|
||||
[ 1 : max pierpoint ]INT psecond;
|
||||
find pierpoint( pfirst, psecond );
|
||||
# show the Pierpoint primes #
|
||||
print pierpoint( pfirst, "First" );
|
||||
print pierpoint( psecond, "Second" )
|
||||
END
|
||||
93
Task/Pierpont-primes/C++/pierpont-primes.cpp
Normal file
93
Task/Pierpont-primes/C++/pierpont-primes.cpp
Normal file
|
|
@ -0,0 +1,93 @@
|
|||
#include <cassert>
|
||||
#include <algorithm>
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <gmpxx.h>
|
||||
|
||||
using big_int = mpz_class;
|
||||
|
||||
bool is_prime(const big_int& n) {
|
||||
return mpz_probab_prime_p(n.get_mpz_t(), 25);
|
||||
}
|
||||
|
||||
template <typename integer>
|
||||
class n_smooth_generator {
|
||||
public:
|
||||
explicit n_smooth_generator(size_t n);
|
||||
integer next();
|
||||
size_t size() const {
|
||||
return results_.size();
|
||||
}
|
||||
private:
|
||||
std::vector<size_t> primes_;
|
||||
std::vector<size_t> index_;
|
||||
std::vector<integer> results_;
|
||||
std::vector<integer> queue_;
|
||||
};
|
||||
|
||||
template <typename integer>
|
||||
n_smooth_generator<integer>::n_smooth_generator(size_t n) {
|
||||
for (size_t p = 2; p <= n; ++p) {
|
||||
if (is_prime(p)) {
|
||||
primes_.push_back(p);
|
||||
queue_.push_back(p);
|
||||
}
|
||||
}
|
||||
index_.assign(primes_.size(), 0);
|
||||
results_.push_back(1);
|
||||
}
|
||||
|
||||
template <typename integer>
|
||||
integer n_smooth_generator<integer>::next() {
|
||||
integer last = results_.back();
|
||||
for (size_t i = 0, n = primes_.size(); i < n; ++i) {
|
||||
if (queue_[i] == last)
|
||||
queue_[i] = results_[++index_[i]] * primes_[i];
|
||||
}
|
||||
results_.push_back(*min_element(queue_.begin(), queue_.end()));
|
||||
return last;
|
||||
}
|
||||
|
||||
void print_vector(const std::vector<big_int>& numbers) {
|
||||
for (size_t i = 0, n = numbers.size(); i < n; ++i) {
|
||||
std::cout << std::setw(9) << numbers[i];
|
||||
if ((i + 1) % 10 == 0)
|
||||
std::cout << '\n';
|
||||
}
|
||||
std::cout << '\n';
|
||||
}
|
||||
|
||||
int main() {
|
||||
const int max = 250;
|
||||
std::vector<big_int> first, second;
|
||||
int count1 = 0;
|
||||
int count2 = 0;
|
||||
n_smooth_generator<big_int> smooth_gen(3);
|
||||
big_int p1, p2;
|
||||
|
||||
while (count1 < max || count2 < max) {
|
||||
big_int n = smooth_gen.next();
|
||||
if (count1 < max && is_prime(n + 1)) {
|
||||
p1 = n + 1;
|
||||
if (first.size() < 50)
|
||||
first.push_back(p1);
|
||||
++count1;
|
||||
}
|
||||
if (count2 < max && is_prime(n - 1)) {
|
||||
p2 = n - 1;
|
||||
if (second.size() < 50)
|
||||
second.push_back(p2);
|
||||
++count2;
|
||||
}
|
||||
}
|
||||
|
||||
std::cout << "First 50 Pierpont primes of the first kind:\n";
|
||||
print_vector(first);
|
||||
std::cout << "First 50 Pierpont primes of the second kind:\n";
|
||||
print_vector(second);
|
||||
|
||||
std::cout << "250th Pierpont prime of the first kind: " << p1 << '\n';
|
||||
std::cout << "250th Pierpont prime of the second kind: " << p2 << '\n';
|
||||
return 0;
|
||||
}
|
||||
173
Task/Pierpont-primes/C-sharp/pierpont-primes.cs
Normal file
173
Task/Pierpont-primes/C-sharp/pierpont-primes.cs
Normal file
|
|
@ -0,0 +1,173 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Numerics;
|
||||
|
||||
namespace PierpontPrimes {
|
||||
public static class Helper {
|
||||
private static readonly Random rand = new Random();
|
||||
private static readonly List<int> primeList = new List<int>() {
|
||||
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47,
|
||||
53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113,
|
||||
127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197,
|
||||
199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281,
|
||||
283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379,
|
||||
383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463,
|
||||
467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571,
|
||||
577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659,
|
||||
661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761,
|
||||
769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863,
|
||||
877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977,
|
||||
};
|
||||
|
||||
public static BigInteger GetRandom(BigInteger min, BigInteger max) {
|
||||
var bytes = max.ToByteArray();
|
||||
BigInteger r;
|
||||
|
||||
do {
|
||||
rand.NextBytes(bytes);
|
||||
bytes[bytes.Length - 1] &= (byte)0x7F; // force sign bit to positive
|
||||
r = new BigInteger(bytes);
|
||||
} while (r < min || r >= max);
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
//Modified from https://rosettacode.org/wiki/Miller-Rabin_primality_test#Python
|
||||
public static bool IsProbablePrime(this BigInteger n) {
|
||||
if (n == 0 || n == 1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Check(BigInteger num) {
|
||||
foreach (var prime in primeList) {
|
||||
if (num == prime) {
|
||||
return true;
|
||||
}
|
||||
if (num % prime == 0) {
|
||||
return false;
|
||||
}
|
||||
if (prime * prime > num) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
if (Check(n)) {
|
||||
var large = primeList[primeList.Count - 1];
|
||||
if (n <= large) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
var s = 0;
|
||||
var d = n - 1;
|
||||
while (d.IsEven) {
|
||||
d >>= 1;
|
||||
s++;
|
||||
}
|
||||
|
||||
bool TrialComposite(BigInteger a) {
|
||||
if (BigInteger.ModPow(a, d, n) == 1) {
|
||||
return false;
|
||||
}
|
||||
for (int i = 0; i < s; i++) {
|
||||
var t = BigInteger.Pow(2, i);
|
||||
if (BigInteger.ModPow(a, t * d, n) == n - 1) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
for (int i = 0; i < 8; i++) {
|
||||
var a = GetRandom(2, n);
|
||||
if (TrialComposite(a)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
class Program {
|
||||
static List<List<BigInteger>> Pierpont(int n) {
|
||||
var p = new List<List<BigInteger>> {
|
||||
new List<BigInteger>(),
|
||||
new List<BigInteger>()
|
||||
};
|
||||
for (int i = 0; i < n; i++) {
|
||||
p[0].Add(0);
|
||||
p[1].Add(0);
|
||||
}
|
||||
p[0][0] = 2;
|
||||
|
||||
var count = 0;
|
||||
var count1 = 1;
|
||||
var count2 = 0;
|
||||
List<BigInteger> s = new List<BigInteger> { 1 };
|
||||
var i2 = 0;
|
||||
var i3 = 0;
|
||||
var k = 1;
|
||||
BigInteger n2;
|
||||
BigInteger n3;
|
||||
BigInteger t;
|
||||
|
||||
while (count < n) {
|
||||
n2 = s[i2] * 2;
|
||||
n3 = s[i3] * 3;
|
||||
if (n2 < n3) {
|
||||
t = n2;
|
||||
i2++;
|
||||
} else {
|
||||
t = n3;
|
||||
i3++;
|
||||
}
|
||||
if (t > s[k - 1]) {
|
||||
s.Add(t);
|
||||
k++;
|
||||
t += 1;
|
||||
if (count1 < n && t.IsProbablePrime()) {
|
||||
p[0][count1] = t;
|
||||
count1++;
|
||||
}
|
||||
t -= 2;
|
||||
if (count2 < n && t.IsProbablePrime()) {
|
||||
p[1][count2] = t;
|
||||
count2++;
|
||||
}
|
||||
count = Math.Min(count1, count2);
|
||||
}
|
||||
}
|
||||
|
||||
return p;
|
||||
}
|
||||
|
||||
static void Main() {
|
||||
var p = Pierpont(250);
|
||||
|
||||
Console.WriteLine("First 50 Pierpont primes of the first kind:");
|
||||
for (int i = 0; i < 50; i++) {
|
||||
Console.Write("{0,8} ", p[0][i]);
|
||||
if ((i - 9) % 10 == 0) {
|
||||
Console.WriteLine();
|
||||
}
|
||||
}
|
||||
Console.WriteLine();
|
||||
|
||||
Console.WriteLine("First 50 Pierpont primes of the second kind:");
|
||||
for (int i = 0; i < 50; i++) {
|
||||
Console.Write("{0,8} ", p[1][i]);
|
||||
if ((i - 9) % 10 == 0) {
|
||||
Console.WriteLine();
|
||||
}
|
||||
}
|
||||
Console.WriteLine();
|
||||
|
||||
Console.WriteLine("250th Pierpont prime of the first kind: {0}", p[0][249]);
|
||||
Console.WriteLine("250th Pierpont prime of the second kind: {0}", p[1][249]);
|
||||
Console.WriteLine();
|
||||
}
|
||||
}
|
||||
}
|
||||
124
Task/Pierpont-primes/C/pierpont-primes.c
Normal file
124
Task/Pierpont-primes/C/pierpont-primes.c
Normal file
|
|
@ -0,0 +1,124 @@
|
|||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
const int PRIMES[] = {
|
||||
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47,
|
||||
53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113,
|
||||
127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197,
|
||||
199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281,
|
||||
283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379,
|
||||
383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463,
|
||||
467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571,
|
||||
577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659,
|
||||
661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761,
|
||||
769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863,
|
||||
877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977,
|
||||
};
|
||||
#define PRIME_SIZE (sizeof(PRIMES) / sizeof(int))
|
||||
|
||||
bool isPrime(const int n) {
|
||||
int i;
|
||||
|
||||
if (n < 2) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (i = 0; i < PRIME_SIZE; i++) {
|
||||
if (n == PRIMES[i]) {
|
||||
return true;
|
||||
}
|
||||
if (n % PRIMES[i] == 0) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (n < PRIMES[PRIME_SIZE - 1] * PRIMES[PRIME_SIZE - 1]) {
|
||||
return true;
|
||||
}
|
||||
|
||||
i = PRIMES[PRIME_SIZE - 1]+2;
|
||||
while (i * i < n) {
|
||||
if (n % i == 0) {
|
||||
return false;
|
||||
}
|
||||
i += 2;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
#define N 50
|
||||
int p[2][50];
|
||||
void pierpont() {
|
||||
int64_t s[8 * N];
|
||||
int count = 0;
|
||||
int count1 = 1;
|
||||
int count2 = 0;
|
||||
int i2 = 0;
|
||||
int i3 = 0;
|
||||
int k = 1;
|
||||
int64_t n2, n3, t;
|
||||
int64_t *sp = &s[1];
|
||||
|
||||
memset(p[0], 0, N * sizeof(int));
|
||||
memset(p[1], 0, N * sizeof(int));
|
||||
p[0][0] = 2;
|
||||
s[0] = 1;
|
||||
|
||||
while (count < N) {
|
||||
n2 = s[i2] * 2;
|
||||
n3 = s[i3] * 3;
|
||||
if (n2 < n3) {
|
||||
t = n2;
|
||||
i2++;
|
||||
} else {
|
||||
t = n3;
|
||||
i3++;
|
||||
}
|
||||
if (t > s[k - 1]) {
|
||||
*sp++ = t;
|
||||
k++;
|
||||
|
||||
t++;
|
||||
if (count1 < N && isPrime(t)) {
|
||||
p[0][count1] = t;
|
||||
count1++;
|
||||
}
|
||||
|
||||
t -= 2;
|
||||
if (count2 < N && isPrime(t)) {
|
||||
p[1][count2] = t;
|
||||
count2++;
|
||||
}
|
||||
|
||||
count = min(count1, count2);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
int i;
|
||||
|
||||
pierpont();
|
||||
|
||||
printf("First 50 Pierpont primes of the first kind:\n");
|
||||
for (i = 0; i < N; i++) {
|
||||
printf("%8d ", p[0][i]);
|
||||
if ((i - 9) % 10 == 0) {
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
printf("First 50 Pierpont primes of the second kind:\n");
|
||||
for (i = 0; i < N; i++) {
|
||||
printf("%8d ", p[1][i]);
|
||||
if ((i - 9) % 10 == 0) {
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
165
Task/Pierpont-primes/D/pierpont-primes.d
Normal file
165
Task/Pierpont-primes/D/pierpont-primes.d
Normal file
|
|
@ -0,0 +1,165 @@
|
|||
import std.algorithm;
|
||||
import std.bigint;
|
||||
import std.random;
|
||||
import std.stdio;
|
||||
|
||||
immutable PRIMES = [
|
||||
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47,
|
||||
53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113,
|
||||
127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197,
|
||||
199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281,
|
||||
283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379,
|
||||
383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463,
|
||||
467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571,
|
||||
577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659,
|
||||
661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761,
|
||||
769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863,
|
||||
877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977,
|
||||
];
|
||||
|
||||
BigInt getRandom(BigInt min, BigInt max) {
|
||||
auto r = max - min;
|
||||
auto hs = r.toHex;
|
||||
|
||||
BigInt result;
|
||||
do {
|
||||
string t = "0x";
|
||||
for (int i = 0; i < hs.length; i++) {
|
||||
t ~= "0123456789abcdef"[uniform(0, 16)];
|
||||
}
|
||||
result = BigInt(t) + min;
|
||||
} while (result < min || max <= result);
|
||||
return result;
|
||||
}
|
||||
|
||||
//Modified from https://rosettacode.org/wiki/Miller-Rabin_primality_test#Python
|
||||
bool isProbablePrime(BigInt n) {
|
||||
if (n == 0 || n == 1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool check(BigInt num) {
|
||||
foreach (prime; PRIMES) {
|
||||
if (num == prime) {
|
||||
return true;
|
||||
}
|
||||
if (num % prime == 0) {
|
||||
return false;
|
||||
}
|
||||
if (prime * prime > num) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
if (check(n)) {
|
||||
auto large = PRIMES[$ - 1];
|
||||
if (n <= large) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
int s = 0;
|
||||
auto d = n - 1;
|
||||
while ((d & 1) == 0) {
|
||||
d >>= 1;
|
||||
s++;
|
||||
}
|
||||
|
||||
bool trialComposite(BigInt a) {
|
||||
if (powmod(a, d, n) == 1) {
|
||||
return false;
|
||||
}
|
||||
for (int i = 0; i < s; i++) {
|
||||
auto t = BigInt(2) ^^ i;
|
||||
if (powmod(a, t * d, n) == n - 1) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
for (int i = 0; i < 8; i++) {
|
||||
auto a = getRandom(BigInt(2), n);
|
||||
if (trialComposite(a)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
BigInt[][] pierpont(int n) {
|
||||
BigInt[][] p = [[], []];
|
||||
for (int i = 0; i < n; i++) {
|
||||
p[0] ~= BigInt(0);
|
||||
p[1] ~= BigInt(0);
|
||||
}
|
||||
p[0][0] = 2;
|
||||
|
||||
int count = 0;
|
||||
int count1 = 1;
|
||||
int count2 = 0;
|
||||
BigInt[] s = [BigInt(1)];
|
||||
int i2 = 0;
|
||||
int i3 = 0;
|
||||
int k = 1;
|
||||
BigInt n2, n3, t;
|
||||
|
||||
while (count < n) {
|
||||
n2 = s[i2] * 2;
|
||||
n3 = s[i3] * 3;
|
||||
if (n2 < n3) {
|
||||
t = n2;
|
||||
i2++;
|
||||
} else {
|
||||
t = n3;
|
||||
i3++;
|
||||
}
|
||||
if (t > s[k - 1]) {
|
||||
s ~= t;
|
||||
k++;
|
||||
|
||||
t++;
|
||||
if (count1 < n && t.isProbablePrime()) {
|
||||
p[0][count1] = t;
|
||||
count1++;
|
||||
}
|
||||
|
||||
t -= 2;
|
||||
if (count2 < n && t.isProbablePrime()) {
|
||||
p[1][count2] = t;
|
||||
count2++;
|
||||
}
|
||||
|
||||
count = min(count1, count2);
|
||||
}
|
||||
}
|
||||
|
||||
return p;
|
||||
}
|
||||
|
||||
void main() {
|
||||
auto p = pierpont(250);
|
||||
|
||||
writeln("First 50 Pierpont primes of the first kind:");
|
||||
for (int i = 0; i < 50; i++) {
|
||||
writef("%8d ", p[0][i]);
|
||||
if ((i - 9) % 10 == 0) {
|
||||
writeln;
|
||||
}
|
||||
}
|
||||
writeln;
|
||||
|
||||
writeln("First 50 Pierpont primes of the first kind:");
|
||||
for (int i = 0; i < 50; i++) {
|
||||
writef("%8d ", p[1][i]);
|
||||
if ((i - 9) % 10 == 0) {
|
||||
writeln;
|
||||
}
|
||||
}
|
||||
writeln;
|
||||
|
||||
writefln("%dth Pierpont prime of the first kind: %d", p[0].length, p[0][$ - 1]);
|
||||
writefln("%dth Pierpont prime of the second kind: %d", p[1].length, p[1][$ - 1]);
|
||||
}
|
||||
71
Task/Pierpont-primes/Delphi/pierpont-primes.delphi
Normal file
71
Task/Pierpont-primes/Delphi/pierpont-primes.delphi
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
program Pierpont_primes;
|
||||
|
||||
{$APPTYPE CONSOLE}
|
||||
|
||||
uses
|
||||
System.SysUtils,
|
||||
System.Math,
|
||||
System.StrUtils,
|
||||
System.Generics.Collections,
|
||||
System.Generics.Defaults,
|
||||
Velthuis.BigIntegers,
|
||||
Velthuis.BigIntegers.Primes;
|
||||
|
||||
function Pierpont(ulim, vlim: Integer; first: boolean): TArray<BigInteger>;
|
||||
begin
|
||||
var p: BigInteger := 0;
|
||||
var p2: BigInteger := 1;
|
||||
var p3: BigInteger := 1;
|
||||
|
||||
for var v := 0 to vlim - 1 do
|
||||
begin
|
||||
for var u := 0 to ulim - 1 do
|
||||
begin
|
||||
p := p2 * p3;
|
||||
if first then
|
||||
p := p + 1
|
||||
else
|
||||
p := p - 1;
|
||||
if IsProbablePrime(p, 10) then
|
||||
begin
|
||||
SetLength(result, Length(result) + 1);
|
||||
result[High(result)] := BigInteger(p);
|
||||
end;
|
||||
p2 := p2 * 2;
|
||||
end;
|
||||
p3 := p3 * 3;
|
||||
p2 := 1;
|
||||
end;
|
||||
|
||||
TArray.sort<BigInteger>(Result, TComparer<BigInteger>.Construct(
|
||||
function(const Left, Right: BigInteger): Integer
|
||||
begin
|
||||
Result := BigInteger.Compare(Left, Right);
|
||||
end));
|
||||
end;
|
||||
|
||||
begin
|
||||
|
||||
writeln('First 50 Pierpont primes of the first kind:');
|
||||
var pp := Pierpont(120, 80, True);
|
||||
for var i := 0 to 49 do
|
||||
begin
|
||||
write(pp[i].ToString: 8, ' ');
|
||||
if ((i - 9) mod 10) = 0 then
|
||||
writeln;
|
||||
end;
|
||||
|
||||
writeln('First 50 Pierpont primes of the second kind:');
|
||||
var pp2 := Pierpont(120, 80, False);
|
||||
for var i := 0 to 49 do
|
||||
begin
|
||||
write(pp2[i].ToString: 8, ' ');
|
||||
if ((i - 9) mod 10) = 0 then
|
||||
writeln;
|
||||
end;
|
||||
|
||||
Writeln('250th Pierpont prime of the first kind:', pp[249].ToString);
|
||||
Writeln('250th Pierpont prime of the second kind:', pp2[249].ToString);
|
||||
|
||||
readln;
|
||||
end.
|
||||
9
Task/Pierpont-primes/F-Sharp/pierpont-primes.fs
Normal file
9
Task/Pierpont-primes/F-Sharp/pierpont-primes.fs
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
// Pierpont primes . Nigel Galloway: March 19th., 2021
|
||||
let fN g=let mutable g=g in ((fun()->g),fun()->g<-g+g;())
|
||||
let fG y=let rec fG n g=seq{match g|>List.minBy(fun(n,_)->n()) with (f,s) when f()=n->yield f()+y; s(); yield! fG(n*3)(fN(n*3)::g)
|
||||
|(f,s) ->yield f()+y; s(); yield! fG n g}
|
||||
seq{yield! fG 1 [fN 1]}|>Seq.filter isPrime
|
||||
let pierpontT1,pierpontT2=fG 1,fG -1
|
||||
|
||||
pierpontT1|>Seq.take 50|>Seq.iter(printf "%d "); printfn ""
|
||||
pierpontT2|>Seq.take 50|>Seq.iter(printf "%d "); printfn ""
|
||||
29
Task/Pierpont-primes/Factor/pierpont-primes.factor
Normal file
29
Task/Pierpont-primes/Factor/pierpont-primes.factor
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
USING: fry grouping io kernel locals make math math.functions
|
||||
math.primes prettyprint sequences sorting ;
|
||||
|
||||
: pierpont ( ulim vlim quot -- seq )
|
||||
'[
|
||||
_ <iota> _ <iota> [
|
||||
[ 2 ] [ 3 ] bi* [ swap ^ ] 2bi@ * 1 @
|
||||
dup prime? [ , ] [ drop ] if
|
||||
] cartesian-each
|
||||
] { } make natural-sort ; inline
|
||||
|
||||
: .fifty ( seq -- ) 50 head 10 group simple-table. nl ;
|
||||
|
||||
[let
|
||||
[ + ] [ - ] [ [ 120 80 ] dip pierpont ] bi@
|
||||
:> ( first second )
|
||||
|
||||
"First 50 Pierpont primes of the first kind:" print
|
||||
first .fifty
|
||||
|
||||
"First 50 Pierpont primes of the second kind:" print
|
||||
second .fifty
|
||||
|
||||
"250th Pierpont prime of the first kind: " write
|
||||
249 first nth . nl
|
||||
|
||||
"250th Pierpont prime of the second kind: " write
|
||||
249 second nth .
|
||||
]
|
||||
58
Task/Pierpont-primes/FreeBASIC/pierpont-primes.basic
Normal file
58
Task/Pierpont-primes/FreeBASIC/pierpont-primes.basic
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
#define NPP 50
|
||||
|
||||
Function isPrime(Byval n As Ulongint) As Boolean
|
||||
If n < 2 Then Return false
|
||||
If n = 2 Then Return true
|
||||
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 is_23(Byval n As Uinteger) As Boolean
|
||||
While n Mod 2 = 0
|
||||
n /= 2
|
||||
Wend
|
||||
While n Mod 3 = 0
|
||||
n /= 3
|
||||
Wend
|
||||
Return Iif(n=1, true, false)
|
||||
End Function
|
||||
|
||||
Function isPierpont(n As Uinteger) As Uinteger
|
||||
If Not isPrime(n) Then Return 0 'not prime
|
||||
Dim As Uinteger p1 = is_23(n+1), p2 = is_23(n-1)
|
||||
If p1 And p2 Then Return 3 'pierpont prime of both kinds
|
||||
If p1 Then Return 1 'pierpont prime of the 1st kind
|
||||
If p2 Then Return 2 'pierpont prime of the 2nd kind
|
||||
Return 0 'prime, but not pierpont
|
||||
End Function
|
||||
|
||||
Dim As Uinteger pier(1 To 2, 1 To NPP), np(1 To 2) = {0, 0}
|
||||
Dim As Uinteger x = 1, j
|
||||
While np(1) <= NPP Or np(2) <= NPP
|
||||
x += 1
|
||||
j = isPierpont(x)
|
||||
If j > 0 Then
|
||||
If j Mod 2 = 1 Then
|
||||
np(1) += 1
|
||||
If np(1) <= NPP Then pier(1, np(1)) = x
|
||||
End If
|
||||
If j > 1 Then
|
||||
np(2) += 1
|
||||
If np(2) <= NPP Then pier(2, np(2)) = x
|
||||
End If
|
||||
End If
|
||||
Wend
|
||||
|
||||
Print "First 50 Pierpoint primes of the first kind:"
|
||||
For j = 1 To NPP
|
||||
Print Using " ########"; pier(2, j);
|
||||
If j Mod 10 = 0 Then Print
|
||||
Next j
|
||||
Print !"\nFirst 50 Pierpoint primes of the secod kind:"
|
||||
For j = 1 To NPP
|
||||
Print Using " ########"; pier(1, j);
|
||||
If j Mod 10 = 0 Then Print
|
||||
Next j
|
||||
62
Task/Pierpont-primes/Go/pierpont-primes-1.go
Normal file
62
Task/Pierpont-primes/Go/pierpont-primes-1.go
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
big "github.com/ncw/gmp"
|
||||
"sort"
|
||||
)
|
||||
|
||||
var (
|
||||
one = new(big.Int).SetUint64(1)
|
||||
two = new(big.Int).SetUint64(2)
|
||||
three = new(big.Int).SetUint64(3)
|
||||
)
|
||||
|
||||
func pierpont(ulim, vlim int, first bool) []*big.Int {
|
||||
p := new(big.Int)
|
||||
p2 := new(big.Int).Set(one)
|
||||
p3 := new(big.Int).Set(one)
|
||||
var pp []*big.Int
|
||||
for v := 0; v < vlim; v++ {
|
||||
for u := 0; u < ulim; u++ {
|
||||
p.Mul(p2, p3)
|
||||
if first {
|
||||
p.Add(p, one)
|
||||
} else {
|
||||
p.Sub(p, one)
|
||||
}
|
||||
if p.ProbablyPrime(10) {
|
||||
q := new(big.Int)
|
||||
q.Set(p)
|
||||
pp = append(pp, q)
|
||||
}
|
||||
p2.Mul(p2, two)
|
||||
}
|
||||
p3.Mul(p3, three)
|
||||
p2.Set(one)
|
||||
}
|
||||
sort.Slice(pp, func(i, j int) bool {
|
||||
return pp[i].Cmp(pp[j]) < 0
|
||||
})
|
||||
return pp
|
||||
}
|
||||
func main() {
|
||||
fmt.Println("First 50 Pierpont primes of the first kind:")
|
||||
pp := pierpont(120, 80, true)
|
||||
for i := 0; i < 50; i++ {
|
||||
fmt.Printf("%8d ", pp[i])
|
||||
if (i-9)%10 == 0 {
|
||||
fmt.Println()
|
||||
}
|
||||
}
|
||||
fmt.Println("\nFirst 50 Pierpont primes of the second kind:")
|
||||
pp2 := pierpont(120, 80, false)
|
||||
for i := 0; i < 50; i++ {
|
||||
fmt.Printf("%8d ", pp2[i])
|
||||
if (i-9)%10 == 0 {
|
||||
fmt.Println()
|
||||
}
|
||||
}
|
||||
fmt.Println("\n250th Pierpont prime of the first kind:", pp[249])
|
||||
fmt.Println("\n250th Pierpont prime of the second kind:", pp2[249])
|
||||
}
|
||||
93
Task/Pierpont-primes/Go/pierpont-primes-2.go
Normal file
93
Task/Pierpont-primes/Go/pierpont-primes-2.go
Normal file
|
|
@ -0,0 +1,93 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
big "github.com/ncw/gmp"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
one = new(big.Int).SetUint64(1)
|
||||
two = new(big.Int).SetUint64(2)
|
||||
three = new(big.Int).SetUint64(3)
|
||||
)
|
||||
|
||||
func min(i, j int) int {
|
||||
if i < j {
|
||||
return i
|
||||
}
|
||||
return j
|
||||
}
|
||||
|
||||
func pierpont(n int, first bool) (p [2][]*big.Int) {
|
||||
p[0] = make([]*big.Int, n)
|
||||
p[1] = make([]*big.Int, n)
|
||||
for i := 0; i < n; i++ {
|
||||
p[0][i] = new(big.Int)
|
||||
p[1][i] = new(big.Int)
|
||||
}
|
||||
p[0][0].Set(two)
|
||||
count, count1, count2 := 0, 1, 0
|
||||
var s []*big.Int
|
||||
s = append(s, new(big.Int).Set(one))
|
||||
i2, i3, k := 0, 0, 1
|
||||
n2, n3, t := new(big.Int), new(big.Int), new(big.Int)
|
||||
for count < n {
|
||||
n2.Mul(s[i2], two)
|
||||
n3.Mul(s[i3], three)
|
||||
if n2.Cmp(n3) < 0 {
|
||||
t.Set(n2)
|
||||
i2++
|
||||
} else {
|
||||
t.Set(n3)
|
||||
i3++
|
||||
}
|
||||
if t.Cmp(s[k-1]) > 0 {
|
||||
s = append(s, new(big.Int).Set(t))
|
||||
k++
|
||||
t.Add(t, one)
|
||||
if count1 < n && t.ProbablyPrime(10) {
|
||||
p[0][count1].Set(t)
|
||||
count1++
|
||||
}
|
||||
t.Sub(t, two)
|
||||
if count2 < n && t.ProbablyPrime(10) {
|
||||
p[1][count2].Set(t)
|
||||
count2++
|
||||
}
|
||||
count = min(count1, count2)
|
||||
}
|
||||
}
|
||||
return p
|
||||
}
|
||||
|
||||
func main() {
|
||||
start := time.Now()
|
||||
p := pierpont(2000, true)
|
||||
fmt.Println("First 50 Pierpont primes of the first kind:")
|
||||
for i := 0; i < 50; i++ {
|
||||
fmt.Printf("%8d ", p[0][i])
|
||||
if (i-9)%10 == 0 {
|
||||
fmt.Println()
|
||||
}
|
||||
}
|
||||
fmt.Println("\nFirst 50 Pierpont primes of the second kind:")
|
||||
for i := 0; i < 50; i++ {
|
||||
fmt.Printf("%8d ", p[1][i])
|
||||
if (i-9)%10 == 0 {
|
||||
fmt.Println()
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Println("\n250th Pierpont prime of the first kind:", p[0][249])
|
||||
fmt.Println("\n250th Pierpont prime of the second kind:", p[1][249])
|
||||
|
||||
fmt.Println("\n1000th Pierpont prime of the first kind:", p[0][999])
|
||||
fmt.Println("\n1000th Pierpont prime of the second kind:", p[1][999])
|
||||
|
||||
fmt.Println("\n2000th Pierpont prime of the first kind:", p[0][1999])
|
||||
fmt.Println("\n2000th Pierpont prime of the second kind:", p[1][1999])
|
||||
|
||||
elapsed := time.Now().Sub(start)
|
||||
fmt.Printf("\nTook %s\n", elapsed)
|
||||
}
|
||||
41
Task/Pierpont-primes/Haskell/pierpont-primes.hs
Normal file
41
Task/Pierpont-primes/Haskell/pierpont-primes.hs
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
import Control.Monad (guard)
|
||||
import Data.List (intercalate)
|
||||
import Data.List.Split (chunksOf)
|
||||
import Math.NumberTheory.Primes (Prime, unPrime, nextPrime)
|
||||
import Math.NumberTheory.Primes.Testing (isPrime)
|
||||
import Text.Printf (printf)
|
||||
|
||||
data PierPointKind = First | Second
|
||||
|
||||
merge :: Ord a => [a] -> [a] -> [a]
|
||||
merge [] b = b
|
||||
merge a@(x:xs) b@(y:ys) | x < y = x : merge xs b
|
||||
| otherwise = y : merge a ys
|
||||
|
||||
nSmooth :: Integer -> [Integer]
|
||||
nSmooth p = 1 : foldr u [] factors
|
||||
where
|
||||
factors = takeWhile (<=p) primes
|
||||
primes = map unPrime [nextPrime 1..]
|
||||
u n s = r
|
||||
where
|
||||
r = merge s (map (n*) (1:r))
|
||||
|
||||
pierpoints :: PierPointKind -> [Integer]
|
||||
pierpoints k = do
|
||||
n <- nSmooth 3
|
||||
let x = case k of First -> succ n
|
||||
Second -> pred n
|
||||
guard (isPrime x) >> [x]
|
||||
|
||||
main :: IO ()
|
||||
main = do
|
||||
printf "\nFirst 50 Pierpont primes of the first kind:\n"
|
||||
mapM_ (\row -> mapM_ (printf "%12s" . commas) row >> printf "\n") (rows $ pierpoints First)
|
||||
printf "\nFirst 50 Pierpont primes of the second kind:\n"
|
||||
mapM_ (\row -> mapM_ (printf "%12s" . commas) row >> printf "\n") (rows $ pierpoints Second)
|
||||
printf "\n250th Pierpont prime of the first kind: %s\n" (commas $ pierpoints First !! 249)
|
||||
printf "\n250th Pierpont prime of the second kind: %s\n\n" (commas $ pierpoints Second !! 249)
|
||||
where
|
||||
rows = chunksOf 10 . take 50
|
||||
commas = reverse . intercalate "," . chunksOf 3 . reverse . show
|
||||
12
Task/Pierpont-primes/J/pierpont-primes-1.j
Normal file
12
Task/Pierpont-primes/J/pierpont-primes-1.j
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
5 10$(#~ 1 p:])1+/:~,*//2 3x^/i.20
|
||||
2 3 5 7 13 17 19 37 73 97
|
||||
109 163 193 257 433 487 577 769 1153 1297
|
||||
1459 2593 2917 3457 3889 10369 12289 17497 18433 39367
|
||||
52489 65537 139969 147457 209953 331777 472393 629857 746497 786433
|
||||
839809 995329 1179649 1492993 1769473 1990657 2654209 5038849 5308417 8503057
|
||||
5 10$(#~ 1 p:])_1+/:~,*//2 3x^/i.20
|
||||
2 3 5 7 11 17 23 31 47 53
|
||||
71 107 127 191 383 431 647 863 971 1151
|
||||
2591 4373 6143 6911 8191 8747 13121 15551 23327 27647
|
||||
62207 73727 131071 139967 165887 294911 314927 442367 472391 497663
|
||||
524287 786431 995327 1062881 2519423 10616831 17915903 25509167 30233087 57395627
|
||||
4
Task/Pierpont-primes/J/pierpont-primes-2.j
Normal file
4
Task/Pierpont-primes/J/pierpont-primes-2.j
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
249{ (#~ 1 p:])1+/:~,*//2 3x^/i.112
|
||||
62518864539857068333550694039553
|
||||
249{ (#~ 1 p:])_1+/:~,*//2 3x^/i.112
|
||||
4111131172000956525894875083702271
|
||||
65
Task/Pierpont-primes/Java/pierpont-primes.java
Normal file
65
Task/Pierpont-primes/Java/pierpont-primes.java
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
import java.math.BigInteger;
|
||||
import java.text.NumberFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class PierpontPrimes {
|
||||
|
||||
public static void main(String[] args) {
|
||||
NumberFormat nf = NumberFormat.getNumberInstance();
|
||||
display("First 50 Pierpont primes of the first kind:", pierpontPrimes(50, true));
|
||||
display("First 50 Pierpont primes of the second kind:", pierpontPrimes(50, false));
|
||||
System.out.printf("250th Pierpont prime of the first kind: %s%n%n", nf.format(pierpontPrimes(250, true).get(249)));
|
||||
System.out.printf("250th Pierpont prime of the second kind: %s%n%n", nf.format(pierpontPrimes(250, false).get(249)));
|
||||
}
|
||||
|
||||
private static void display(String message, List<BigInteger> primes) {
|
||||
NumberFormat nf = NumberFormat.getNumberInstance();
|
||||
System.out.printf("%s%n", message);
|
||||
for ( int i = 1 ; i <= primes.size() ; i++ ) {
|
||||
System.out.printf("%10s ", nf.format(primes.get(i-1)));
|
||||
if ( i % 10 == 0 ) {
|
||||
System.out.printf("%n");
|
||||
}
|
||||
}
|
||||
System.out.printf("%n");
|
||||
}
|
||||
|
||||
public static List<BigInteger> pierpontPrimes(int n, boolean first) {
|
||||
List<BigInteger> primes = new ArrayList<BigInteger>();
|
||||
if ( first ) {
|
||||
primes.add(BigInteger.valueOf(2));
|
||||
n -= 1;
|
||||
}
|
||||
|
||||
BigInteger two = BigInteger.valueOf(2);
|
||||
BigInteger twoTest = two;
|
||||
BigInteger three = BigInteger.valueOf(3);
|
||||
BigInteger threeTest = three;
|
||||
int twoIndex = 0, threeIndex = 0;
|
||||
List<BigInteger> twoSmooth = new ArrayList<BigInteger>();
|
||||
|
||||
BigInteger one = BigInteger.ONE;
|
||||
BigInteger mOne = BigInteger.valueOf(-1);
|
||||
int count = 0;
|
||||
while ( count < n ) {
|
||||
BigInteger min = twoTest.min(threeTest);
|
||||
twoSmooth.add(min);
|
||||
if ( min.compareTo(twoTest) == 0 ) {
|
||||
twoTest = two.multiply(twoSmooth.get(twoIndex));
|
||||
twoIndex++;
|
||||
}
|
||||
if ( min.compareTo(threeTest) == 0 ) {
|
||||
threeTest = three.multiply(twoSmooth.get(threeIndex));
|
||||
threeIndex++;
|
||||
}
|
||||
BigInteger test = min.add(first ? one : mOne);
|
||||
if ( test.isProbablePrime(10) ) {
|
||||
primes.add(test);
|
||||
count++;
|
||||
}
|
||||
}
|
||||
return primes;
|
||||
}
|
||||
|
||||
}
|
||||
25
Task/Pierpont-primes/Jq/pierpont-primes-1.jq
Normal file
25
Task/Pierpont-primes/Jq/pierpont-primes-1.jq
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
def is_prime:
|
||||
. as $n
|
||||
| if ($n < 2) then false
|
||||
elif ($n % 2 == 0) then $n == 2
|
||||
elif ($n % 3 == 0) then $n == 3
|
||||
elif ($n % 5 == 0) then $n == 5
|
||||
elif ($n % 7 == 0) then $n == 7
|
||||
elif ($n % 11 == 0) then $n == 11
|
||||
elif ($n % 13 == 0) then $n == 13
|
||||
elif ($n % 17 == 0) then $n == 17
|
||||
elif ($n % 19 == 0) then $n == 19
|
||||
else {i:23}
|
||||
| until( (.i * .i) > $n or ($n % .i == 0); .i += 2)
|
||||
| .i * .i > $n
|
||||
end;
|
||||
|
||||
# pretty-printing
|
||||
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;
|
||||
|
||||
def table($ncols; $colwidth):
|
||||
nwise($ncols) | map(lpad($colwidth)) | join(" ");
|
||||
65
Task/Pierpont-primes/Jq/pierpont-primes-2.jq
Normal file
65
Task/Pierpont-primes/Jq/pierpont-primes-2.jq
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
# Input: the target number of primes of the form p(u,v) == 2^u * 3^v ± 1.
|
||||
# The main idea is to let u run from 0:m and v run from 0:n where n ~~ 0.63 * m.
|
||||
# Initially we start with plausible values for m and n ($maxm and $maxn respectively),
|
||||
# and then check whether these have been chosen conservatively enough.
|
||||
#
|
||||
def pierponts($firstkind):
|
||||
. as $N
|
||||
| (if $firstkind then 1 else -1 end) as $incdec
|
||||
|
||||
# Input: [$maxm, $maxn]
|
||||
# Output: an array of objects of the form {p, m, n}
|
||||
# where .p is prime and .m and .n are the corresponding powers of 2 and 3
|
||||
| def pp:
|
||||
. as [$maxm, $maxn]
|
||||
| [ ({p2:1, m:0},
|
||||
(foreach range(0; $maxm) as $m (1; . * 2; {p2: ., m: ($m + 1)}))) as $a
|
||||
| ({p3:1, n:0},
|
||||
(foreach range(0; $maxn) as $n (1; . * 3; {p3: ., n: ($n + 1)}))) as $b
|
||||
| {p: ($a.p2 * $b.p3 + $incdec), m: $a.m, n: $b.n}
|
||||
| select(.p|is_prime) ]
|
||||
| unique_by(.p)
|
||||
# | (length|debug) as $debug # informative
|
||||
| .;
|
||||
|
||||
# input: output of pp
|
||||
# check that length is sufficient, and that $maxm and $maxn are large enough
|
||||
def adequate($maxm; $maxn):
|
||||
# ( ".[$N-1].m is \(.[$N-1].m) vs $maxm=\($maxm)" | debug) as $debug |
|
||||
# ( ".[$N-1].n is \(.[$N-1].n) vs $maxn=\($maxn)" | debug) as $debug |
|
||||
length > $N
|
||||
and .[$N-1].m < $maxm - 3 # -2 is not sufficient
|
||||
and .[$N-1].n < $maxn - 3 # -2 is not sufficient
|
||||
;
|
||||
|
||||
# If our search has not been `adequate` then increase $maxm and $maxn
|
||||
# input: [maxm, maxn, ([maxm,maxn]|pp)]
|
||||
# output: pp
|
||||
def adapt:
|
||||
. as [$maxm, $maxn, $one]
|
||||
| if ($one|adequate($maxm; $maxn)) then $one
|
||||
else [$maxm + 2, $maxn + 1.6] as $maxplus
|
||||
# | ("retrying with \($maxplus)" | debug) as $debug
|
||||
| ($maxplus|pp) as $two
|
||||
| $maxplus + [$two] | adapt
|
||||
end;
|
||||
|
||||
# We start by selecting m and n so that
|
||||
# m*n >> $N, i.e., 0.63 * m^2 >> $N , so m >> sqrt(1.585 * $N)
|
||||
# Using 7 as the constant to start with is sufficient to avoid too much rework.
|
||||
((9 * $N) | sqrt) as $maxm
|
||||
| (0.63 * $maxm + 1) as $maxn
|
||||
| [$maxm, $maxn] as $max
|
||||
| ($max | pp) as $pp
|
||||
| ($max + [$pp]) | [adapt[:$N][].p] ;
|
||||
|
||||
# The stretch goal:
|
||||
def stretch:
|
||||
250
|
||||
| "\nThe \(.)th Pierpoint prime of the first kind is \(pierponts(true)[-1])",
|
||||
"\nThe \(.)th Pierpoint prime of the second kind is \(pierponts(false)[-1])" ;
|
||||
|
||||
# The primary task:
|
||||
50
|
||||
| "\nThe first \(.) Pierpoint primes of the first kind:", (pierponts(true) | table(10;8)),
|
||||
"\nThe first \(.) Pierpoint primes of the second kind:", (pierponts(false) | table(10;8))
|
||||
32
Task/Pierpont-primes/Julia/pierpont-primes.julia
Normal file
32
Task/Pierpont-primes/Julia/pierpont-primes.julia
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
using Primes
|
||||
|
||||
function pierponts(N, firstkind = true)
|
||||
ret, incdec = BigInt[], firstkind ? 1 : -1
|
||||
for k2 in 0:10000, k3 in 0:k2, switch in false:true
|
||||
i, j = switch ? (k3, k2) : (k2, k3)
|
||||
n = BigInt(2)^i * BigInt(3)^j + incdec
|
||||
if isprime(n) && !(n in ret)
|
||||
push!(ret, n)
|
||||
if length(ret) == N * 2
|
||||
return sort(ret)[1:N]
|
||||
end
|
||||
end
|
||||
end
|
||||
throw("Failed to find $(N * 2) primes")
|
||||
end
|
||||
|
||||
println("The first 50 Pierpont primes (first kind) are: ", pierponts(50))
|
||||
|
||||
println("\nThe first 50 Pierpont primes (second kind) are: ", pierponts(50, false))
|
||||
|
||||
println("\nThe 250th Pierpont prime (first kind) is: ", pierponts(250)[250])
|
||||
|
||||
println("\nThe 250th Pierpont prime (second kind) is: ", pierponts(250, false)[250])
|
||||
|
||||
println("\nThe 1000th Pierpont prime (first kind) is: ", pierponts(1000)[1000])
|
||||
|
||||
println("\nThe 1000th Pierpont prime (second kind) is: ", pierponts(1000, false)[1000])
|
||||
|
||||
println("\nThe 2000th Pierpont prime (first kind) is: ", pierponts(2000)[2000])
|
||||
|
||||
println("\nThe 2000th Pierpont prime (second kind) is: ", pierponts(2000, false)[2000])
|
||||
78
Task/Pierpont-primes/Kotlin/pierpont-primes.kotlin
Normal file
78
Task/Pierpont-primes/Kotlin/pierpont-primes.kotlin
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
import java.math.BigInteger
|
||||
import kotlin.math.min
|
||||
|
||||
val one: BigInteger = BigInteger.ONE
|
||||
val two: BigInteger = BigInteger.valueOf(2)
|
||||
val three: BigInteger = BigInteger.valueOf(3)
|
||||
|
||||
fun pierpont(n: Int): List<List<BigInteger>> {
|
||||
val p = List(2) { MutableList(n) { BigInteger.ZERO } }
|
||||
p[0][0] = two
|
||||
var count = 0
|
||||
var count1 = 1
|
||||
var count2 = 0
|
||||
val s = mutableListOf<BigInteger>()
|
||||
s.add(one)
|
||||
var i2 = 0
|
||||
var i3 = 0
|
||||
var k = 1
|
||||
var n2: BigInteger
|
||||
var n3: BigInteger
|
||||
var t: BigInteger
|
||||
while (count < n) {
|
||||
n2 = s[i2] * two
|
||||
n3 = s[i3] * three
|
||||
if (n2 < n3) {
|
||||
t = n2
|
||||
i2++
|
||||
} else {
|
||||
t = n3
|
||||
i3++
|
||||
}
|
||||
if (t > s[k - 1]) {
|
||||
s.add(t)
|
||||
k++
|
||||
t += one
|
||||
if (count1 < n && t.isProbablePrime(10)) {
|
||||
p[0][count1] = t
|
||||
count1++
|
||||
}
|
||||
t -= two
|
||||
if (count2 < n && t.isProbablePrime(10)) {
|
||||
p[1][count2] = t
|
||||
count2++
|
||||
}
|
||||
count = min(count1, count2)
|
||||
}
|
||||
}
|
||||
return p
|
||||
}
|
||||
|
||||
fun main() {
|
||||
val p = pierpont(2000)
|
||||
|
||||
println("First 50 Pierpont primes of the first kind:")
|
||||
for (i in 0 until 50) {
|
||||
print("%8d ".format(p[0][i]))
|
||||
if ((i - 9) % 10 == 0) {
|
||||
println()
|
||||
}
|
||||
}
|
||||
|
||||
println("\nFirst 50 Pierpont primes of the second kind:")
|
||||
for (i in 0 until 50) {
|
||||
print("%8d ".format(p[1][i]))
|
||||
if ((i - 9) % 10 == 0) {
|
||||
println()
|
||||
}
|
||||
}
|
||||
|
||||
println("\n250th Pierpont prime of the first kind: ${p[0][249]}")
|
||||
println("\n250th Pierpont prime of the first kind: ${p[1][249]}")
|
||||
|
||||
println("\n1000th Pierpont prime of the first kind: ${p[0][999]}")
|
||||
println("\n1000th Pierpont prime of the first kind: ${p[1][999]}")
|
||||
|
||||
println("\n2000th Pierpont prime of the first kind: ${p[0][1999]}")
|
||||
println("\n2000th Pierpont prime of the first kind: ${p[1][1999]}")
|
||||
}
|
||||
50
Task/Pierpont-primes/Lua/pierpont-primes.lua
Normal file
50
Task/Pierpont-primes/Lua/pierpont-primes.lua
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
local function isprime(n)
|
||||
if n < 2 then return false end
|
||||
if n % 2 == 0 then return n==2 end
|
||||
if n % 3 == 0 then return n==3 end
|
||||
local f, limit = 5, math.sqrt(n)
|
||||
for f = 5, limit, 6 do
|
||||
if n % f == 0 then return false end
|
||||
if n % (f+2) == 0 then return false end
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
local function s3iter()
|
||||
local s, i2, i3 = {1}, 1, 1
|
||||
return function()
|
||||
local n2, n3, val = 2*s[i2], 3*s[i3], s[#s]
|
||||
s[#s+1] = math.min(n2, n3)
|
||||
i2, i3 = i2 + (n2<=n3 and 1 or 0), i3 + (n2>=n3 and 1 or 0)
|
||||
return val
|
||||
end
|
||||
end
|
||||
|
||||
local function pierpont(n)
|
||||
local list1, list2, s3next = {}, {}, s3iter()
|
||||
while #list1 < n or #list2 < n do
|
||||
local s3 = s3next()
|
||||
if #list1 < n then
|
||||
if isprime(s3+1) then list1[#list1+1] = s3+1 end
|
||||
end
|
||||
if #list2 < n then
|
||||
if isprime(s3-1) then list2[#list2+1] = s3-1 end
|
||||
end
|
||||
end
|
||||
return list1, list2
|
||||
end
|
||||
|
||||
local N = 50
|
||||
local p1, p2 = pierpont(N)
|
||||
|
||||
print("First 50 Pierpont primes of the first kind:")
|
||||
for i, p in ipairs(p1) do
|
||||
io.write(string.format("%8d%s", p, (i%10==0 and "\n" or " ")))
|
||||
end
|
||||
|
||||
print()
|
||||
|
||||
print("First 50 Pierpont primes of the second kind:")
|
||||
for i, p in ipairs(p2) do
|
||||
io.write(string.format("%8d%s", p, (i%10==0 and "\n" or " ")))
|
||||
end
|
||||
56
Task/Pierpont-primes/M2000-Interpreter/pierpont-primes.m2000
Normal file
56
Task/Pierpont-primes/M2000-Interpreter/pierpont-primes.m2000
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
Module Pierpoint_Primes {
|
||||
Form 80
|
||||
Set Fast !
|
||||
const NPP=50
|
||||
dim pier(1 to 2, 1 to NPP), np(1 to 2) = 0
|
||||
def long x = 1, j
|
||||
while np(1)<=NPP or np(2)<=NPP
|
||||
x++
|
||||
j = @is_pierpont(x)
|
||||
if j>0 Else Continue
|
||||
if j mod 2 = 1 then np(1)++ :if np(1) <= NPP then pier(1, np(1)) = x
|
||||
if j > 1 then np(2)++ : if np(2) <= NPP then pier(2, np(2)) = x
|
||||
end while
|
||||
|
||||
print "First ";NPP;" Pierpont primes of the first kind:"
|
||||
for j = 1 to NPP
|
||||
print pier(2, j),
|
||||
next j
|
||||
if pos>0 then print
|
||||
print "First ";NPP;" Pierpont primes of the second kind:"
|
||||
for j = 1 to NPP
|
||||
print pier(1, j),
|
||||
next j
|
||||
if pos>0 then print
|
||||
Set Fast
|
||||
function is_prime(n as decimal)
|
||||
if n < 2 then = false : exit function
|
||||
if n <4 then = true : exit function
|
||||
if n mod 2 = 0 then = false : exit function
|
||||
local i as long
|
||||
for i = 3 to int(sqrt(n))+1 step 2
|
||||
if n mod i = 0 then = false : exit function
|
||||
next i
|
||||
= true
|
||||
end function
|
||||
|
||||
function is_23(n as long)
|
||||
while n mod 2 = 0
|
||||
n = n div 2
|
||||
end while
|
||||
while n mod 3 = 0
|
||||
n = n div 3
|
||||
end while
|
||||
if n = 1 then = true else = false
|
||||
end function
|
||||
|
||||
function is_pierpont(n as long)
|
||||
if not @is_prime(n) then = 0& : exit function 'not prime
|
||||
Local p1 = @is_23(n+1), p2 = @is_23(n-1)
|
||||
if p1 and p2 then = 3 : exit function 'pierpont prime of both kinds
|
||||
if p1 then = 1 : exit function 'pierpont prime of the 1st kind
|
||||
if p2 then = 2 : exit function 'pierpont prime of the 2nd kind
|
||||
= 0 'prime, but not pierpont
|
||||
end function
|
||||
}
|
||||
Pierpoint_Primes
|
||||
27
Task/Pierpont-primes/Mathematica/pierpont-primes.math
Normal file
27
Task/Pierpont-primes/Mathematica/pierpont-primes.math
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
ClearAll[FindUpToMax]
|
||||
FindUpToMax[max_Integer, b_Integer] := Module[{res, num},
|
||||
res = {};
|
||||
Do[
|
||||
num = 2^u 3^v + b;
|
||||
If[PrimeQ[num], AppendTo[res, num]]
|
||||
,
|
||||
{u, 0, Ceiling@Log[2, max]}
|
||||
,
|
||||
{v, 0, Ceiling@Log[3, max]}
|
||||
];
|
||||
res //= Select[LessEqualThan[max]];
|
||||
res //= Sort;
|
||||
res
|
||||
]
|
||||
Print["Piermont primes of the first kind:"]
|
||||
Take[FindUpToMax[10^10, +1], UpTo[50]]
|
||||
Print["Piermont primes of the second kind:"]
|
||||
Take[FindUpToMax[10^10, -1], UpTo[50]]
|
||||
Print["250th Piermont prime of the first kind:"]
|
||||
Part[FindUpToMax[10^34, +1], 250]
|
||||
Print["250th Piermont prime of the second kind:"]
|
||||
Part[FindUpToMax[10^34, -1], 250]
|
||||
Print["1000th Piermont prime of the first kind:"]
|
||||
Part[FindUpToMax[10^130, +1], 1000]
|
||||
Print["1000th Piermont prime of the second kind:"]
|
||||
Part[FindUpToMax[10^150, -1], 1000]
|
||||
57
Task/Pierpont-primes/Nim/pierpont-primes.nim
Normal file
57
Task/Pierpont-primes/Nim/pierpont-primes.nim
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
import math, strutils
|
||||
|
||||
func isPrime(n: int): bool =
|
||||
## Check if "n" is prime by trying successive divisions.
|
||||
## "n" is supposed not to be a multiple of 2 or 3.
|
||||
var d = 5
|
||||
var delta = 2
|
||||
while d <= int(sqrt(n.toFloat)):
|
||||
if n mod d == 0: return false
|
||||
inc d, delta
|
||||
delta = 6 - delta
|
||||
result = true
|
||||
|
||||
func isProduct23(n: int): bool =
|
||||
## Check if "n" has only 2 and 3 for prime divisors
|
||||
## (i.e. that "n = 2^u * 3^v").
|
||||
var n = n
|
||||
while (n and 1) == 0: n = n shr 1
|
||||
while n mod 3 == 0: n = n div 3
|
||||
result = n == 1
|
||||
|
||||
iterator pierpont(maxCount: Positive; k: int): int =
|
||||
## Yield the Pierpoint primes of first or second kind according
|
||||
## to the value of "k" (+1 for first kind, -1 for second kind).
|
||||
yield 2
|
||||
yield 3
|
||||
var n = 5
|
||||
var delta = 2 # 2 and 4 alternatively to skip the multiples of 2 and 3.
|
||||
yield n
|
||||
var count = 3
|
||||
while count < maxCount:
|
||||
inc n, delta
|
||||
delta = 6 - delta
|
||||
if isProduct23(n - k) and isPrime(n):
|
||||
inc count
|
||||
yield n
|
||||
|
||||
template pierpont1*(maxCount: Positive): int = pierpont(maxCount, +1)
|
||||
template pierpont2*(maxCount: Positive): int = pierpont(maxCount, -1)
|
||||
|
||||
|
||||
when isMainModule:
|
||||
|
||||
echo "First 50 Pierpont primes of the first kind:"
|
||||
var count = 0
|
||||
for n in pierpont1(50):
|
||||
stdout.write ($n).align(9)
|
||||
inc count
|
||||
if count mod 10 == 0: stdout.write '\n'
|
||||
|
||||
echo ""
|
||||
echo "First 50 Pierpont primes of the second kind:"
|
||||
count = 0
|
||||
for n in pierpont2(50):
|
||||
stdout.write ($n).align(9)
|
||||
inc count
|
||||
if count mod 10 == 0: stdout.write '\n'
|
||||
53
Task/Pierpont-primes/Perl/pierpont-primes.pl
Normal file
53
Task/Pierpont-primes/Perl/pierpont-primes.pl
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
use strict;
|
||||
use warnings;
|
||||
use feature 'say';
|
||||
use bigint try=>"GMP";
|
||||
use ntheory qw<is_prime>;
|
||||
|
||||
# index of mininum value in list
|
||||
sub min_index { my $b = $_[my $i = 0]; $_[$_] < $b && ($b = $_[$i = $_]) for 0..$#_; $i }
|
||||
|
||||
sub iter1 { my $m = shift; my $e = 0; return sub { $m ** $e++; } }
|
||||
sub iter2 { my $m = shift; my $e = 1; return sub { $m * ($e *= 2) } }
|
||||
|
||||
sub pierpont {
|
||||
my($max ) = shift || die 'Must specify count of primes to generate.';
|
||||
my($kind) = @_ ? shift : 1;
|
||||
die "Unknown type: $kind. Must be one of 1 (default) or 2" unless $kind == 1 || $kind == 2;
|
||||
$kind = -1 if $kind == 2;
|
||||
|
||||
my $po3 = 3;
|
||||
my $add_one = 3;
|
||||
my @iterators;
|
||||
push @iterators, iter1(2);
|
||||
push @iterators, iter1(3); $iterators[1]->();
|
||||
my @head = ($iterators[0]->(), $iterators[1]->());
|
||||
|
||||
my @pierpont;
|
||||
do {
|
||||
my $key = min_index(@head);
|
||||
my $min = $head[$key];
|
||||
push @pierpont, $min + $kind if is_prime($min + $kind);
|
||||
|
||||
$head[$key] = $iterators[$key]->();
|
||||
|
||||
if ($min >= $add_one) {
|
||||
push @iterators, iter2($po3);
|
||||
$add_one = $head[$#iterators] = $iterators[$#iterators]->();
|
||||
$po3 *= 3;
|
||||
}
|
||||
} until @pierpont == $max;
|
||||
@pierpont;
|
||||
}
|
||||
|
||||
my @pierpont_1st = pierpont(250,1);
|
||||
my @pierpont_2nd = pierpont(250,2);
|
||||
|
||||
say "First 50 Pierpont primes of the first kind:";
|
||||
my $fmt = "%9d"x10 . "\n";
|
||||
for my $row (0..4) { printf $fmt, map { $pierpont_1st[10*$row + $_] } 0..9 }
|
||||
say "\nFirst 50 Pierpont primes of the second kind:";
|
||||
for my $row (0..4) { printf $fmt, map { $pierpont_2nd[10*$row + $_] } 0..9 }
|
||||
|
||||
say "\n250th Pierpont prime of the first kind: " . $pierpont_1st[249];
|
||||
say "\n250th Pierpont prime of the second kind: " . $pierpont_2nd[249];
|
||||
69
Task/Pierpont-primes/Phix/pierpont-primes.phix
Normal file
69
Task/Pierpont-primes/Phix/pierpont-primes.phix
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
(phixonline)-->
|
||||
<span style="color: #000080;font-style:italic;">-- demo/rosetta/Pierpont_primes.exw</span>
|
||||
<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: #008080;">function</span> <span style="color: #000000;">pierpont</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #004080;">sequence</span> <span style="color: #000000;">p</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{{</span><span style="color: #7060A8;">mpz_init</span><span style="color: #0000FF;">(</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)},{}},</span>
|
||||
<span style="color: #000000;">s</span> <span style="color: #0000FF;">=</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: #004080;">integer</span> <span style="color: #000000;">i2</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: #000000;">1</span>
|
||||
<span style="color: #004080;">mpz</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">n2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">n3</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">t</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_inits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">3</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #004080;">atom</span> <span style="color: #000000;">t1</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">time</span><span style="color: #0000FF;">()+</span><span style="color: #000000;">1</span>
|
||||
<span style="color: #008080;">while</span> <span style="color: #7060A8;">min</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]),</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">[</span><span style="color: #000000;">2</span><span style="color: #0000FF;">]))</span> <span style="color: #0000FF;"><</span> <span style="color: #000000;">n</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #7060A8;">mpz_mul_si</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i2</span><span style="color: #0000FF;">],</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #7060A8;">mpz_mul_si</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i3</span><span style="color: #0000FF;">],</span><span style="color: #000000;">3</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #7060A8;">mpz_cmp</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n3</span><span style="color: #0000FF;">)<</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
|
||||
<span style="color: #7060A8;">mpz_set</span><span style="color: #0000FF;">(</span><span style="color: #000000;">t</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n2</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #000000;">i2</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
|
||||
<span style="color: #008080;">else</span>
|
||||
<span style="color: #7060A8;">mpz_set</span><span style="color: #0000FF;">(</span><span style="color: #000000;">t</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n3</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #000000;">i3</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #7060A8;">mpz_cmp</span><span style="color: #0000FF;">(</span><span style="color: #000000;">t</span><span style="color: #0000FF;">,</span><span style="color: #000000;">s</span><span style="color: #0000FF;">[$])</span> <span style="color: #0000FF;">></span> <span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
|
||||
<span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">,</span> <span style="color: #7060A8;">mpz_init_set</span><span style="color: #0000FF;">(</span><span style="color: #000000;">t</span><span style="color: #0000FF;">))</span>
|
||||
<span style="color: #7060A8;">mpz_add_ui</span><span style="color: #0000FF;">(</span><span style="color: #000000;">t</span><span style="color: #0000FF;">,</span><span style="color: #000000;">t</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">])</span> <span style="color: #0000FF;"><</span> <span style="color: #000000;">n</span> <span style="color: #008080;">and</span> <span style="color: #7060A8;">mpz_prime</span><span style="color: #0000FF;">(</span><span style="color: #000000;">t</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
|
||||
<span style="color: #000000;">p</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">],</span><span style="color: #7060A8;">mpz_init_set</span><span style="color: #0000FF;">(</span><span style="color: #000000;">t</span><span style="color: #0000FF;">))</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #7060A8;">mpz_sub_ui</span><span style="color: #0000FF;">(</span><span style="color: #000000;">t</span><span style="color: #0000FF;">,</span><span style="color: #000000;">t</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">[</span><span style="color: #000000;">2</span><span style="color: #0000FF;">])</span> <span style="color: #0000FF;"><</span> <span style="color: #000000;">n</span> <span style="color: #008080;">and</span> <span style="color: #7060A8;">mpz_prime</span><span style="color: #0000FF;">(</span><span style="color: #000000;">t</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
|
||||
<span style="color: #000000;">p</span><span style="color: #0000FF;">[</span><span style="color: #000000;">2</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">[</span><span style="color: #000000;">2</span><span style="color: #0000FF;">],</span><span style="color: #7060A8;">mpz_init_set</span><span style="color: #0000FF;">(</span><span style="color: #000000;">t</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;">if</span> <span style="color: #7060A8;">time</span><span style="color: #0000FF;">()></span><span style="color: #000000;">t1</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;">"Found: 1st:%d/%d, 2nd:%d/%d\r"</span><span style="color: #0000FF;">,</span>
|
||||
<span style="color: #0000FF;">{</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]),</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">[</span><span style="color: #000000;">2</span><span style="color: #0000FF;">]),</span><span style="color: #000000;">n</span><span style="color: #0000FF;">})</span>
|
||||
<span style="color: #000000;">t1</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">time</span><span style="color: #0000FF;">()+</span><span style="color: #000000;">1</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
|
||||
<span style="color: #008080;">return</span> <span style="color: #000000;">p</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
|
||||
|
||||
<span style="color: #000080;font-style:italic;">--constant limit = 2000 -- 2 mins
|
||||
--constant limit = 1000 -- 8.1s</span>
|
||||
<span style="color: #008080;">constant</span> <span style="color: #000000;">limit</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">250</span> <span style="color: #000080;font-style:italic;">-- 0.1s</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: #004080;">sequence</span> <span style="color: #000000;">p</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">pierpont</span><span style="color: #0000FF;">(</span><span style="color: #000000;">limit</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">constant</span> <span style="color: #000000;">fs</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"first"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"second"</span><span style="color: #0000FF;">}</span>
|
||||
|
||||
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fs</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</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;">"First 50 Pierpont primes of the %s kind:\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">fs</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]})</span>
|
||||
<span style="color: #008080;">for</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">50</span> <span style="color: #008080;">do</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;">"%8s "</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #7060A8;">mpz_get_str</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">][</span><span style="color: #000000;">j</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;">j</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;">"\n"</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;">for</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;">"\n"</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
|
||||
|
||||
<span style="color: #008080;">constant</span> <span style="color: #000000;">t</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">250</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1000</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2000</span><span style="color: #0000FF;">}</span>
|
||||
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">t</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #004080;">integer</span> <span style="color: #000000;">ti</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">t</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #000000;">ti</span><span style="color: #0000FF;">></span><span style="color: #000000;">limit</span> <span style="color: #008080;">then</span> <span style="color: #008080;">exit</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #008080;">for</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fs</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #004080;">string</span> <span style="color: #000000;">zs</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">shorten</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">mpz_get_str</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">][</span><span style="color: #000000;">ti</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;">"%dth Pierpont prime of the %s kind: %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">ti</span><span style="color: #0000FF;">,</span><span style="color: #000000;">fs</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">],</span><span style="color: #000000;">zs</span><span style="color: #0000FF;">})</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</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;">"\n"</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</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;">"Took %s\n"</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>
|
||||
<!--
|
||||
88
Task/Pierpont-primes/Prolog/pierpont-primes.pro
Normal file
88
Task/Pierpont-primes/Prolog/pierpont-primes.pro
Normal file
|
|
@ -0,0 +1,88 @@
|
|||
?- use_module(library(heaps)).
|
||||
|
||||
three_smooth(Lz) :-
|
||||
singleton_heap(H, 1, nothing),
|
||||
lazy_list(next_3smooth, 0-H, Lz).
|
||||
|
||||
next_3smooth(Top-H0, N-H2, N) :-
|
||||
min_of_heap(H0, Top, _), !,
|
||||
get_from_heap(H0, Top, _, H1),
|
||||
next_3smooth(Top-H1, N-H2, N).
|
||||
next_3smooth(_-H0, N-H3, N) :-
|
||||
get_from_heap(H0, N, _, H1),
|
||||
N2 is N * 2,
|
||||
N3 is N * 3,
|
||||
add_to_heap(H1, N2, nothing, H2),
|
||||
add_to_heap(H2, N3, nothing, H3).
|
||||
|
||||
first_kind(K) :-
|
||||
three_smooth(Ns), member(N, Ns),
|
||||
K is N + 1,
|
||||
prime(K).
|
||||
|
||||
second_kind(K) :-
|
||||
three_smooth(Ns), member(N, Ns),
|
||||
K is N - 1,
|
||||
prime(K).
|
||||
|
||||
show(Seq, N) :-
|
||||
format("The first ~w values of ~s are: ", [N, Seq]),
|
||||
once(findnsols(N, X, call(Seq, X), L)),
|
||||
write(L), nl,
|
||||
once(offset(249, call(Seq, TwoFifty))),
|
||||
format("The 250th value of ~w is ~w~n", [Seq, TwoFifty]).
|
||||
|
||||
main :-
|
||||
show(first_kind, 50), nl,
|
||||
show(second_kind, 50), nl,
|
||||
halt.
|
||||
|
||||
% primality checker -- Miller Rabin preceded with a round of trial divisions.
|
||||
|
||||
prime(N) :-
|
||||
integer(N),
|
||||
N > 1,
|
||||
divcheck(
|
||||
N,
|
||||
[ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31,
|
||||
37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79,
|
||||
83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137,
|
||||
139, 149],
|
||||
Result),
|
||||
((Result = prime, !); miller_rabin_primality_test(N)).
|
||||
|
||||
divcheck(_, [], unknown) :- !.
|
||||
divcheck(N, [P|_], prime) :- P*P > N, !.
|
||||
divcheck(N, [P|Ps], State) :- N mod P =\= 0, divcheck(N, Ps, State).
|
||||
|
||||
miller_rabin_primality_test(N) :-
|
||||
bases(Bases, N),
|
||||
forall(member(A, Bases), strong_fermat_pseudoprime(N, A)).
|
||||
|
||||
miller_rabin_precision(16).
|
||||
|
||||
bases([31, 73], N) :- N < 9_080_191, !.
|
||||
bases([2, 7, 61], N) :- N < 4_759_123_141, !.
|
||||
bases([2, 325, 9_375, 28_178, 450_775, 9_780_504, 1_795_265_022], N) :-
|
||||
N < 18_446_744_073_709_551_616, !. % 2^64
|
||||
bases(Bases, N) :-
|
||||
miller_rabin_precision(T), RndLimit is N - 2,
|
||||
length(Bases, T), maplist(random_between(2, RndLimit), Bases).
|
||||
|
||||
strong_fermat_pseudoprime(N, A) :- % miller-rabin strong pseudoprime test with base A.
|
||||
succ(Pn, N), factor_2s(Pn, S, D),
|
||||
X is powm(A, D, N),
|
||||
((X =:= 1, !); \+ composite_witness(N, S, X)).
|
||||
|
||||
composite_witness(_, 0, _) :- !.
|
||||
composite_witness(N, K, X) :-
|
||||
X =\= N-1,
|
||||
succ(Pk, K), X2 is (X*X) mod N, composite_witness(N, Pk, X2).
|
||||
|
||||
factor_2s(N, S, D) :- factor_2s(0, N, S, D).
|
||||
factor_2s(S, D, S, D) :- D /\ 1 =\= 0, !.
|
||||
factor_2s(S0, D0, S, D) :-
|
||||
succ(S0, S1), D1 is D0 >> 1,
|
||||
factor_2s(S1, D1, S, D).
|
||||
|
||||
?- main.
|
||||
78
Task/Pierpont-primes/Python/pierpont-primes.py
Normal file
78
Task/Pierpont-primes/Python/pierpont-primes.py
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
import random
|
||||
|
||||
# Copied from https://rosettacode.org/wiki/Miller-Rabin_primality_test#Python
|
||||
def is_Prime(n):
|
||||
"""
|
||||
Miller-Rabin primality test.
|
||||
|
||||
A return value of False means n is certainly not prime. A return value of
|
||||
True means n is very likely a prime.
|
||||
"""
|
||||
if n!=int(n):
|
||||
return False
|
||||
n=int(n)
|
||||
#Miller-Rabin test for prime
|
||||
if n==0 or n==1 or n==4 or n==6 or n==8 or n==9:
|
||||
return False
|
||||
|
||||
if n==2 or n==3 or n==5 or n==7:
|
||||
return True
|
||||
s = 0
|
||||
d = n-1
|
||||
while d%2==0:
|
||||
d>>=1
|
||||
s+=1
|
||||
assert(2**s * d == n-1)
|
||||
|
||||
def trial_composite(a):
|
||||
if pow(a, d, n) == 1:
|
||||
return False
|
||||
for i in range(s):
|
||||
if pow(a, 2**i * d, n) == n-1:
|
||||
return False
|
||||
return True
|
||||
|
||||
for i in range(8):#number of trials
|
||||
a = random.randrange(2, n)
|
||||
if trial_composite(a):
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
def pierpont(ulim, vlim, first):
|
||||
p = 0
|
||||
p2 = 1
|
||||
p3 = 1
|
||||
pp = []
|
||||
for v in xrange(vlim):
|
||||
for u in xrange(ulim):
|
||||
p = p2 * p3
|
||||
if first:
|
||||
p = p + 1
|
||||
else:
|
||||
p = p - 1
|
||||
if is_Prime(p):
|
||||
pp.append(p)
|
||||
p2 = p2 * 2
|
||||
p3 = p3 * 3
|
||||
p2 = 1
|
||||
pp.sort()
|
||||
return pp
|
||||
|
||||
def main():
|
||||
print "First 50 Pierpont primes of the first kind:"
|
||||
pp = pierpont(120, 80, True)
|
||||
for i in xrange(50):
|
||||
print "%8d " % pp[i],
|
||||
if (i - 9) % 10 == 0:
|
||||
print
|
||||
print "First 50 Pierpont primes of the second kind:"
|
||||
pp2 = pierpont(120, 80, False)
|
||||
for i in xrange(50):
|
||||
print "%8d " % pp2[i],
|
||||
if (i - 9) % 10 == 0:
|
||||
print
|
||||
print "250th Pierpont prime of the first kind:", pp[249]
|
||||
print "250th Pierpont prime of the second kind:", pp2[249]
|
||||
|
||||
main()
|
||||
31
Task/Pierpont-primes/Quackery/pierpont-primes.quackery
Normal file
31
Task/Pierpont-primes/Quackery/pierpont-primes.quackery
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
[ stack ] is pierponts
|
||||
[ stack ] is kind
|
||||
[ stack ] is quantity
|
||||
|
||||
[ 1 - -2 * 1+ kind put
|
||||
1+ quantity put
|
||||
' [ -1 ] pierponts put
|
||||
' [ 2 3 ] smoothwith
|
||||
[ -1 peek
|
||||
kind share +
|
||||
dup isprime not iff
|
||||
[ drop false ] done
|
||||
pierponts share -1 peek
|
||||
over = iff
|
||||
[ drop false ] done
|
||||
pierponts take
|
||||
swap join
|
||||
dup size swap
|
||||
pierponts put
|
||||
quantity share = ]
|
||||
drop
|
||||
quantity release
|
||||
kind release
|
||||
pierponts take
|
||||
behead drop ] is pierpontprimes
|
||||
|
||||
say "Pierpont primes of the first kind." cr
|
||||
50 1 pierpontprimes echo
|
||||
cr cr
|
||||
say "Pierpont primes of the second kind." cr
|
||||
50 2 pierpontprimes echo
|
||||
36
Task/Pierpont-primes/REXX/pierpont-primes.rexx
Normal file
36
Task/Pierpont-primes/REXX/pierpont-primes.rexx
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
/*REXX program finds and displays Pierpont primes of the first and second kinds. */
|
||||
parse arg n . /*obtain optional argument from the CL.*/
|
||||
if n=='' | n=="," then n= 50 /*Not specified? Then use the default.*/
|
||||
numeric digits n /*ensure enough decimal digs (bit int).*/
|
||||
big= copies(9, digits() ) /*BIG: used as a max number (a limit).*/
|
||||
@.= '2nd'; @.1= '1st'
|
||||
do t=1 to -1 by -2; usum= 0; vsum= 0; s= 0 /*T is 1, then -1.*/
|
||||
#= 0 /*number of Pierpont primes (so far). */
|
||||
$=; do j=0 until #>=n /*$: the list " " " " */
|
||||
if usum<=s then usum= get(2, 3); if vsum<=s then vsum= get(3, 2)
|
||||
s= min(vsum, usum); if \isPrime(s) then iterate /*get min; Not prime? */
|
||||
#= # + 1; $= $ s /*bump counter; append.*/
|
||||
end /*j*/
|
||||
say
|
||||
w= length(word($, #) ) /*biggest prime length.*/
|
||||
say center(n " Pierpont primes of the " @.t ' kind', max(10 *(w+1), 80), "═")
|
||||
|
||||
do p=1 by 10 to #; _=; do k=p for 10; _= _ right( word($, k), w)
|
||||
end /*k*/
|
||||
if _\=='' then say substr( strip(_, "T"), 2)
|
||||
end /*p*/
|
||||
end /*t*/
|
||||
exit 0 /*stick a fork in it, we're all done. */
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
isPrime: procedure; parse arg x '' -1 _; if x<17 then return wordpos(x,"2 3 5 7 11 13")>0
|
||||
if _==5 then return 0; if x//2==0 then return 0 /*not prime. */
|
||||
if x//3==0 then return 0; if x//7==0 then return 0 /* " " */
|
||||
do j=11 by 6 until j*j>x /*skip ÷ 3's.*/
|
||||
if x//j==0 then return 0; if x//(j+2)==0 then return 0 /*not prime. */
|
||||
end /*j*/; return 1 /*it's prime.*/
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
get: parse arg c1,c2; m=big; do ju=0; pu= c1**ju; if pu+t>s then return min(m, pu+t)
|
||||
do jv=0; pv= c2**jv; if pv >s then iterate ju
|
||||
_= pu*pv + t; if _ >s then m= min(_, m)
|
||||
end /*jv*/
|
||||
end /*ju*/ /*see the RETURN (above). */
|
||||
34
Task/Pierpont-primes/Raku/pierpont-primes-1.raku
Normal file
34
Task/Pierpont-primes/Raku/pierpont-primes-1.raku
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
use ntheory:from<Perl5> <is_prime>;
|
||||
|
||||
sub pierpont ($kind is copy = 1) {
|
||||
fail "Unknown type: $kind. Must be one of 1 (default) or 2" if $kind !== 1|2;
|
||||
$kind = -1 if $kind == 2;
|
||||
my $po3 = 3;
|
||||
my $add-one = 3;
|
||||
my @iterators = [1,2,4,8 … *].iterator, [3,9,27 … *].iterator;
|
||||
my @head = @iterators».pull-one;
|
||||
|
||||
gather {
|
||||
loop {
|
||||
my $key = @head.pairs.min( *.value ).key;
|
||||
my $min = @head[$key];
|
||||
@head[$key] = @iterators[$key].pull-one;
|
||||
|
||||
take $min + $kind if "{$min + $kind}".&is_prime;
|
||||
|
||||
if $min >= $add-one {
|
||||
@iterators.push: ([|((2,4,8).map: * * $po3) … *]).iterator;
|
||||
$add-one = @head[+@iterators - 1] = @iterators[+@iterators - 1].pull-one;
|
||||
$po3 *= 3;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
say "First 50 Pierpont primes of the first kind:\n" ~ pierpont[^50].rotor(10)».fmt('%8d').join: "\n";
|
||||
|
||||
say "\nFirst 50 Pierpont primes of the second kind:\n" ~ pierpont(2)[^50].rotor(10)».fmt('%8d').join: "\n";
|
||||
|
||||
say "\n250th Pierpont prime of the first kind: " ~ pierpont[249];
|
||||
|
||||
say "\n250th Pierpont prime of the second kind: " ~ pierpont(2)[249];
|
||||
39
Task/Pierpont-primes/Raku/pierpont-primes-2.raku
Normal file
39
Task/Pierpont-primes/Raku/pierpont-primes-2.raku
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
sub smooth-numbers (*@list) {
|
||||
cache my \Smooth := gather {
|
||||
my %i = (flat @list) Z=> (Smooth.iterator for ^@list);
|
||||
my %n = (flat @list) Z=> 1 xx *;
|
||||
|
||||
loop {
|
||||
take my $n := %n{*}.min;
|
||||
|
||||
for @list -> \k {
|
||||
%n{k} = %i{k}.pull-one * k if %n{k} == $n;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# Testing various smooth numbers
|
||||
|
||||
for 'OEIS: A092506 - 2 + Fermat primes:', (2), 1, 6,
|
||||
"\nOEIS: A000668 - Mersenne primes:", (2), -1, 10,
|
||||
"\nOEIS: A005109 - Pierpont primes 1st:", (2,3), 1, 20,
|
||||
"\nOEIS: A005105 - Pierpont primes 2nd:", (2,3), -1, 20,
|
||||
"\nOEIS: A077497:", (2,5), 1, 20,
|
||||
"\nOEIS: A077313:", (2,5), -1, 20,
|
||||
"\nOEIS: A002200 - (\"Hamming\" primes 1st):", (2,3,5), 1, 20,
|
||||
"\nOEIS: A293194 - (\"Hamming\" primes 2nd):", (2,3,5), -1, 20,
|
||||
"\nOEIS: A077498:", (2,7), 1, 20,
|
||||
"\nOEIS: A077314:", (2,7), -1, 20,
|
||||
"\nOEIS: A174144 - (\"Humble\" primes 1st):", (2,3,5,7), 1, 20,
|
||||
"\nOEIS: A347977 - (\"Humble\" primes 2nd):", (2,3,5,7), -1, 20,
|
||||
"\nOEIS: A077499:", (2,11), 1, 20,
|
||||
"\nOEIS: A077315:", (2,11), -1, 20,
|
||||
"\nOEIS: A173236:", (2,13), 1, 20,
|
||||
"\nOEIS: A173062:", (2,13), -1, 20
|
||||
|
||||
-> $title, $primes, $add, $count {
|
||||
|
||||
say "$title smooth \{$primes\} {$add > 0 ?? '+' !! '-'} 1 ";
|
||||
put smooth-numbers(|$primes).map( * + $add ).grep( *.is-prime )[^$count]
|
||||
}
|
||||
56
Task/Pierpont-primes/Ring/pierpont-primes.ring
Normal file
56
Task/Pierpont-primes/Ring/pierpont-primes.ring
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
load "stdlib.ring"
|
||||
|
||||
see "working..." + nl
|
||||
|
||||
pierpont = []
|
||||
limit1 = 18
|
||||
limit2 = 8505000
|
||||
limit3 = 50
|
||||
limit4 = 21
|
||||
limit5 = 30500000
|
||||
|
||||
for n = 0 to limit1
|
||||
for m = 0 to limit1
|
||||
num = pow(2,n)*pow(3,m) + 1
|
||||
if isprime(num) and num < limit2
|
||||
add(pierpont,num)
|
||||
ok
|
||||
if num > limit2
|
||||
exit
|
||||
ok
|
||||
next
|
||||
next
|
||||
|
||||
pierpont = sort(pierpont)
|
||||
|
||||
see "First 50 Pierpont primes of the first kind:" + nl + nl
|
||||
|
||||
for n = 1 to limit3
|
||||
see "" + n + ". " + pierpont[n] + nl
|
||||
next
|
||||
|
||||
see "done1..." + nl
|
||||
|
||||
pierpont = []
|
||||
|
||||
for n = 0 to limit4
|
||||
for m = 0 to limit4
|
||||
num = pow(2,n)*pow(3,m) - 1
|
||||
if isprime(num) and num < limit5
|
||||
add(pierpont,num)
|
||||
ok
|
||||
if num > limit5
|
||||
exit
|
||||
ok
|
||||
next
|
||||
next
|
||||
|
||||
pierpont = sort(pierpont)
|
||||
|
||||
see "First 50 Pierpont primes of the second kind:" + nl + nl
|
||||
|
||||
for n = 1 to limit3
|
||||
see "" + n + ". " + pierpont[n] + nl
|
||||
next
|
||||
|
||||
see "done2..." + nl
|
||||
30
Task/Pierpont-primes/Ruby/pierpont-primes.rb
Normal file
30
Task/Pierpont-primes/Ruby/pierpont-primes.rb
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
require 'gmp'
|
||||
|
||||
def smooth_generator(ar)
|
||||
return to_enum(__method__, ar) unless block_given?
|
||||
next_smooth = 1
|
||||
queues = ar.map{|num| [num, []] }
|
||||
loop do
|
||||
yield next_smooth
|
||||
queues.each {|m, queue| queue << next_smooth * m}
|
||||
next_smooth = queues.collect{|m, queue| queue.first}.min
|
||||
queues.each{|m, queue| queue.shift if queue.first == next_smooth }
|
||||
end
|
||||
end
|
||||
|
||||
def pierpont(num = 1)
|
||||
return to_enum(__method__, num) unless block_given?
|
||||
smooth_generator([2,3]).each{|smooth| yield smooth+num if GMP::Z(smooth + num).probab_prime? > 0}
|
||||
end
|
||||
|
||||
def puts_cols(ar, n=10)
|
||||
ar.each_slice(n).map{|slice|puts slice.map{|n| n.to_s.rjust(10)}.join }
|
||||
end
|
||||
|
||||
n, m = 50, 250
|
||||
puts "First #{n} Pierpont primes of the first kind:"
|
||||
puts_cols(pierpont.take(n))
|
||||
puts "#{m}th Pierpont prime of the first kind: #{pierpont.take(250).last}",""
|
||||
puts "First #{n} Pierpont primes of the second kind:"
|
||||
puts_cols(pierpont(-1).take(n))
|
||||
puts "#{m}th Pierpont prime of the second kind: #{pierpont(-1).take(250).last}"
|
||||
29
Task/Pierpont-primes/Sidef/pierpont-primes.sidef
Normal file
29
Task/Pierpont-primes/Sidef/pierpont-primes.sidef
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
func smooth_generator(primes) {
|
||||
var s = primes.len.of { [1] }
|
||||
{
|
||||
var n = s.map { .first }.min
|
||||
{ |i|
|
||||
s[i].shift if (s[i][0] == n)
|
||||
s[i] << (n * primes[i])
|
||||
} * primes.len
|
||||
n
|
||||
}
|
||||
}
|
||||
|
||||
func pierpont_primes(n, k = 1) {
|
||||
var g = smooth_generator([2,3])
|
||||
1..Inf -> lazy.map { g.run + k }.grep { .is_prime }.first(n)
|
||||
}
|
||||
|
||||
say "First 50 Pierpont primes of the 1st kind: "
|
||||
say pierpont_primes(50, +1).join(' ')
|
||||
|
||||
say "\nFirst 50 Pierpont primes of the 2nd kind: "
|
||||
say pierpont_primes(50, -1).join(' ')
|
||||
|
||||
for n in (250, 500, 1000) {
|
||||
var p = pierpont_primes(n, +1).last
|
||||
var q = pierpont_primes(n, -1).last
|
||||
say "\n#{n}th Pierpont prime of the 1st kind: #{p}"
|
||||
say "#{n}th Pierpont prime of the 2nd kind: #{q}"
|
||||
}
|
||||
57
Task/Pierpont-primes/Swift/pierpont-primes.swift
Normal file
57
Task/Pierpont-primes/Swift/pierpont-primes.swift
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
import BigInt
|
||||
import Foundation
|
||||
|
||||
public func pierpoint(n: Int) -> (first: [BigInt], second: [BigInt]) {
|
||||
var primes = (first: [BigInt](repeating: 0, count: n), second: [BigInt](repeating: 0, count: n))
|
||||
|
||||
primes.first[0] = 2
|
||||
|
||||
var count1 = 1, count2 = 0
|
||||
var s = [BigInt(1)]
|
||||
var i2 = 0, i3 = 0, k = 1
|
||||
var n2 = BigInt(0), n3 = BigInt(0), t = BigInt(0)
|
||||
|
||||
while min(count1, count2) < n {
|
||||
n2 = s[i2] * 2
|
||||
n3 = s[i3] * 3
|
||||
|
||||
if n2 < n3 {
|
||||
t = n2
|
||||
i2 += 1
|
||||
} else {
|
||||
t = n3
|
||||
i3 += 1
|
||||
}
|
||||
|
||||
if t <= s[k - 1] {
|
||||
continue
|
||||
}
|
||||
|
||||
s.append(t)
|
||||
k += 1
|
||||
t += 1
|
||||
|
||||
if count1 < n && t.isPrime(rounds: 10) {
|
||||
primes.first[count1] = t
|
||||
count1 += 1
|
||||
}
|
||||
|
||||
t -= 2
|
||||
|
||||
if count2 < n && t.isPrime(rounds: 10) {
|
||||
primes.second[count2] = t
|
||||
count2 += 1
|
||||
}
|
||||
}
|
||||
|
||||
return primes
|
||||
}
|
||||
|
||||
|
||||
let primes = pierpoint(n: 250)
|
||||
|
||||
print("First 50 Pierpoint primes of the first kind: \(Array(primes.first.prefix(50)))\n")
|
||||
print("First 50 Pierpoint primes of the second kind: \(Array(primes.second.prefix(50)))")
|
||||
print()
|
||||
print("250th Pierpoint prime of the first kind: \(primes.first[249])")
|
||||
print("250th Pierpoint prime of the second kind: \(primes.second[249])")
|
||||
61
Task/Pierpont-primes/Wren/pierpont-primes.wren
Normal file
61
Task/Pierpont-primes/Wren/pierpont-primes.wren
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
import "/big" for BigInt
|
||||
import "/fmt" for Fmt
|
||||
|
||||
var pierpont = Fn.new { |n, first|
|
||||
var p = [ List.filled(n, null), List.filled(n, null) ]
|
||||
for (i in 0...n) {
|
||||
p[0][i] = BigInt.zero
|
||||
p[1][i] = BigInt.zero
|
||||
}
|
||||
p[0][0] = BigInt.two
|
||||
var count = 0
|
||||
var count1 = 1
|
||||
var count2 = 0
|
||||
var s = [BigInt.one]
|
||||
var i2 = 0
|
||||
var i3 = 0
|
||||
var k = 1
|
||||
while (count < n) {
|
||||
var n2 = s[i2] * 2
|
||||
var n3 = s[i3] * 3
|
||||
var t
|
||||
if (n2 < n3) {
|
||||
t = n2
|
||||
i2 = i2 + 1
|
||||
} else {
|
||||
t = n3
|
||||
i3 = i3 + 1
|
||||
}
|
||||
if (t > s[k-1]) {
|
||||
s.add(t.copy())
|
||||
k = k + 1
|
||||
t = t.inc
|
||||
if (count1 < n && t.isProbablePrime(5)) {
|
||||
p[0][count1] = t.copy()
|
||||
count1 = count1 + 1
|
||||
}
|
||||
t = t - 2
|
||||
if (count2 < n && t.isProbablePrime(5)) {
|
||||
p[1][count2] = t.copy()
|
||||
count2 = count2 + 1
|
||||
}
|
||||
count = count1.min(count2)
|
||||
}
|
||||
}
|
||||
return p
|
||||
}
|
||||
|
||||
var p = pierpont.call(250, true)
|
||||
System.print("First 50 Pierpont primes of the first kind:")
|
||||
for (i in 0...50) {
|
||||
Fmt.write("$8i ", p[0][i])
|
||||
if ((i-9)%10 == 0) System.print()
|
||||
}
|
||||
System.print("\nFirst 50 Pierpont primes of the second kind:")
|
||||
for (i in 0...50) {
|
||||
Fmt.write("$8i ", p[1][i])
|
||||
if ((i-9)%10 == 0) System.print()
|
||||
}
|
||||
|
||||
System.print("\n250th Pierpont prime of the first kind: %(p[0][249])")
|
||||
System.print("\n250th Pierpont prime of the second kind: %(p[1][249])")
|
||||
43
Task/Pierpont-primes/XPL0/pierpont-primes.xpl0
Normal file
43
Task/Pierpont-primes/XPL0/pierpont-primes.xpl0
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
func IsPrime(N); \Return 'true' if N is prime
|
||||
int N, D;
|
||||
[if N < 2 then return false;
|
||||
if (N&1) = 0 then return N = 2;
|
||||
if rem(N/3) = 0 then return N = 3;
|
||||
D:= 5;
|
||||
while D*D <= N do
|
||||
[if rem(N/D) = 0 then return false;
|
||||
D:= D+2;
|
||||
if rem(N/D) = 0 then return false;
|
||||
D:= D+4;
|
||||
];
|
||||
return true;
|
||||
];
|
||||
|
||||
func Pierpont(N); \Return 'true' if N is a multiple of 2^U*3^V
|
||||
int N;
|
||||
[while (N&1) = 0 do N:= N>>1;
|
||||
while N>1 do
|
||||
[N:= N/3;
|
||||
if rem(0) # 0 then return false;
|
||||
];
|
||||
return true;
|
||||
];
|
||||
|
||||
int Kind, N, Count;
|
||||
[Format(9, 0);
|
||||
Kind:= 1;
|
||||
repeat Count:= 0; N:= 2;
|
||||
loop [if IsPrime(N) then
|
||||
[if Pierpont(N-Kind) then
|
||||
[Count:= Count+1;
|
||||
RlOut(0, float(N));
|
||||
if rem(Count/10) = 0 then CrLf(0);
|
||||
if Count >= 50 then quit;
|
||||
];
|
||||
];
|
||||
N:= N+1;
|
||||
];
|
||||
CrLf(0);
|
||||
Kind:= -Kind;
|
||||
until Kind = 1;
|
||||
]
|
||||
29
Task/Pierpont-primes/Zkl/pierpont-primes-1.zkl
Normal file
29
Task/Pierpont-primes/Zkl/pierpont-primes-1.zkl
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
var [const] BI=Import("zklBigNum"); // libGMP
|
||||
var [const] one=BI(1), two=BI(2), three=BI(3);
|
||||
|
||||
fcn pierPonts(n){ //-->((bigInt first kind primes) (bigInt second))
|
||||
pps1,pps2 := List(BI(2)), List();
|
||||
count1, count2, s := 1, 0, List(BI(1)); // n==2_000, s-->266_379 elements
|
||||
i2,i3,k := 0, 0, 1;
|
||||
n2,n3,t := BI(0),BI(0),BI(0);
|
||||
while(count1.min(count2) < n){
|
||||
n2.set(s[i2]).mul(two); // .mul, .add, .sub are in-place
|
||||
n3.set(s[i3]).mul(three);
|
||||
if(n2<n3){ t.set(n2); i2+=1; }
|
||||
else { t.set(n3); i3+=1; }
|
||||
if(t > s[k-1]){
|
||||
s.append(t.copy());
|
||||
k+=1;
|
||||
t.add(one);
|
||||
if(count1<n and t.probablyPrime()){
|
||||
pps1.append(t.copy());
|
||||
count1+=1;
|
||||
}
|
||||
if(count2<n and t.sub(two).probablyPrime()){
|
||||
pps2.append(t.copy());
|
||||
count2+=1;
|
||||
}
|
||||
}
|
||||
}
|
||||
return(pps1,pps2)
|
||||
}
|
||||
12
Task/Pierpont-primes/Zkl/pierpont-primes-2.zkl
Normal file
12
Task/Pierpont-primes/Zkl/pierpont-primes-2.zkl
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
pps1,pps2 := pierPonts(2_000);
|
||||
|
||||
println("The first 50 Pierpont primes (first kind):");
|
||||
foreach r in (5){ pps1[r*10,10].apply("%10d".fmt).concat().println() }
|
||||
|
||||
println("\nThe first 50 Pierpont primes (second kind):");
|
||||
foreach r in (5){ pps2[r*10,10].apply("%10d".fmt).concat().println() }
|
||||
|
||||
foreach n in (T(250, 1_000, 2_000)){
|
||||
println("\n%4dth Pierpont prime, first kind: ".fmt(n), pps1[n-1]);
|
||||
println( " second kind: ", pps2[n-1]);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue