Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
2
Task/Euclid-Mullin-sequence/00-META.yaml
Normal file
2
Task/Euclid-Mullin-sequence/00-META.yaml
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
---
|
||||
from: http://rosettacode.org/wiki/Euclid-Mullin_sequence
|
||||
17
Task/Euclid-Mullin-sequence/00-TASK.txt
Normal file
17
Task/Euclid-Mullin-sequence/00-TASK.txt
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
;Definition
|
||||
The [https://en.wikipedia.org/wiki/Euclid%E2%80%93Mullin_sequence Euclid–Mullin sequence] is an infinite sequence of distinct prime numbers, in which each element is the least prime factor of one plus the product of all earlier elements.
|
||||
|
||||
The first element is usually assumed to be 2. So the second element is : (2) + 1 = 3 and the third element is : (2 x 3) + 1 = 7 as this is prime.
|
||||
|
||||
Although intermingled with smaller elements, the sequence can produce very large elements quite quickly and only the first 51 have been computed at the time of writing.
|
||||
|
||||
;Task
|
||||
Compute and show here the first '''16''' elements of the sequence or, if your language does not support arbitrary precision arithmetic, as many as you can.
|
||||
|
||||
;Stretch goal
|
||||
Compute the next '''11''' elements of the sequence.
|
||||
|
||||
;Reference
|
||||
[https://oeis.org/A000945 OEIS sequence A000945]
|
||||
<br><br>
|
||||
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
BEGIN # find elements of the Euclid-Mullin sequence: starting from 2, #
|
||||
# the next element is the smallest prime factor of 1 + the product #
|
||||
# of the previous elements #
|
||||
print( ( " 2" ) );
|
||||
LONG LONG INT product := 2;
|
||||
FROM 2 TO 16 DO
|
||||
LONG LONG INT next := product + 1;
|
||||
# find the first prime factor of next #
|
||||
LONG LONG INT p := 3;
|
||||
BOOL found := FALSE;
|
||||
WHILE p * p <= next AND NOT ( found := next MOD p = 0 ) DO
|
||||
p +:= 2
|
||||
OD;
|
||||
IF found THEN next := p FI;
|
||||
print( ( " ", whole( next, 0 ) ) );
|
||||
product *:= next
|
||||
OD
|
||||
END
|
||||
25
Task/Euclid-Mullin-sequence/AWK/euclid-mullin-sequence.awk
Normal file
25
Task/Euclid-Mullin-sequence/AWK/euclid-mullin-sequence.awk
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
# syntax: GAWK -f EUCLID-MULLIN_SEQUENCE.AWK
|
||||
# converted from FreeBASIC
|
||||
BEGIN {
|
||||
limit = 7 # we'll stop here
|
||||
arr[0] = 2
|
||||
printf("%s ",arr[0])
|
||||
for (i=1; i<=limit; i++) {
|
||||
k = 3
|
||||
while (1) {
|
||||
em = 1
|
||||
for (j=0; j<=i-1; j++) {
|
||||
em = (em * arr[j]) % k
|
||||
}
|
||||
em = (em + 1) % k
|
||||
if (em == 0) {
|
||||
arr[i] = k
|
||||
printf("%s ",arr[i])
|
||||
break
|
||||
}
|
||||
k += 2
|
||||
}
|
||||
}
|
||||
printf("\n")
|
||||
exit(0)
|
||||
}
|
||||
|
|
@ -0,0 +1,41 @@
|
|||
define size = 16, em = 0
|
||||
dim list[size]
|
||||
|
||||
let list[0] = 2
|
||||
print 2
|
||||
|
||||
for i = 1 to 15
|
||||
|
||||
let k = 3
|
||||
|
||||
do
|
||||
|
||||
let em = 1
|
||||
|
||||
for j = 0 to i - 1
|
||||
|
||||
let em = ( em * list[j] ) % k
|
||||
|
||||
next j
|
||||
|
||||
let em = ( em + 1 ) % k
|
||||
|
||||
if em = 0 then
|
||||
|
||||
let list[i] = k
|
||||
print list[i]
|
||||
break
|
||||
|
||||
endif
|
||||
|
||||
let k = k + 2
|
||||
|
||||
wait
|
||||
|
||||
loop
|
||||
|
||||
next i
|
||||
|
||||
print "done."
|
||||
|
||||
end
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
//Euclid-Mullin sequence. Nigel Galloway: October 29th., 2021
|
||||
let(|Prime|_|)(n,g)=if Open.Numeric.Primes.MillerRabin.IsProbablePrime &g then Some(n*g,n*g+1I) else None
|
||||
let n=Seq.unfold(fun(n,g)->match n,g with Prime n->Some(g,n) |_->let g=Open.Numeric.Primes.Extensions.PrimeExtensions.PrimeFactors g|>Seq.item 1 in Some(g,(n*g,n*g+1I)))(1I,2I)
|
||||
n|>Seq.take 16|>Seq.iter(printfn "%A")
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
Func Firstfac(n) =
|
||||
j := 3;
|
||||
up := Sqrt(n);
|
||||
|
||||
while j <= up do
|
||||
if Divides(j,n) then Return(j) fi;
|
||||
j:=j+2;
|
||||
od;
|
||||
Return(n).;
|
||||
|
||||
Array eu[16];
|
||||
eu[1]:=2;
|
||||
!(eu[1],' ');
|
||||
for i=2 to 16 do
|
||||
eu[i]:=Firstfac(1+Prod<k=1,i-1>[eu[k]]);
|
||||
!(eu[i],' ');
|
||||
od;
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
dim as ulongint E(0 to 15), k
|
||||
dim as integer i, em
|
||||
E(0) = 2 : print 2
|
||||
for i=1 to 15
|
||||
k=3
|
||||
do
|
||||
em = 1
|
||||
for j as uinteger = 0 to i-1
|
||||
em = (em*E(j)) mod k
|
||||
next j
|
||||
em = (em + 1) mod k
|
||||
if em = 0 then
|
||||
E(i)=k
|
||||
print E(i)
|
||||
exit do
|
||||
end if
|
||||
k = k + 2
|
||||
loop
|
||||
next i
|
||||
129
Task/Euclid-Mullin-sequence/Go/euclid-mullin-sequence.go
Normal file
129
Task/Euclid-Mullin-sequence/Go/euclid-mullin-sequence.go
Normal file
|
|
@ -0,0 +1,129 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
big "github.com/ncw/gmp"
|
||||
"log"
|
||||
)
|
||||
|
||||
var (
|
||||
zero = big.NewInt(0)
|
||||
one = big.NewInt(1)
|
||||
two = big.NewInt(2)
|
||||
three = big.NewInt(3)
|
||||
four = big.NewInt(4)
|
||||
five = big.NewInt(5)
|
||||
six = big.NewInt(6)
|
||||
ten = big.NewInt(10)
|
||||
max = big.NewInt(100000)
|
||||
)
|
||||
|
||||
func pollardRho(n, c *big.Int) *big.Int {
|
||||
g := func(x, y *big.Int) *big.Int {
|
||||
x2 := new(big.Int)
|
||||
x2.Mul(x, x)
|
||||
x2.Add(x2, c)
|
||||
return x2.Mod(x2, y)
|
||||
}
|
||||
x, y, z := big.NewInt(2), big.NewInt(2), big.NewInt(1)
|
||||
d := new(big.Int)
|
||||
count := 0
|
||||
for {
|
||||
x = g(x, n)
|
||||
y = g(g(y, n), n)
|
||||
d.Sub(x, y)
|
||||
d.Abs(d)
|
||||
d.Mod(d, n)
|
||||
z.Mul(z, d)
|
||||
count++
|
||||
if count == 100 {
|
||||
d.GCD(nil, nil, z, n)
|
||||
if d.Cmp(one) != 0 {
|
||||
break
|
||||
}
|
||||
z.Set(one)
|
||||
count = 0
|
||||
}
|
||||
}
|
||||
if d.Cmp(n) == 0 {
|
||||
return zero
|
||||
}
|
||||
return d
|
||||
}
|
||||
|
||||
func smallestPrimeFactorWheel(n *big.Int) *big.Int {
|
||||
if n.ProbablyPrime(15) {
|
||||
return n
|
||||
}
|
||||
z := new(big.Int)
|
||||
if z.Rem(n, two).Cmp(zero) == 0 {
|
||||
return two
|
||||
}
|
||||
if z.Rem(n, three).Cmp(zero) == 0 {
|
||||
return three
|
||||
}
|
||||
if z.Rem(n, five).Cmp(zero) == 0 {
|
||||
return five
|
||||
}
|
||||
k := big.NewInt(7)
|
||||
i := 0
|
||||
inc := []*big.Int{four, two, four, two, four, six, two, six}
|
||||
for z.Mul(k, k).Cmp(n) <= 0 {
|
||||
if z.Rem(n, k).Cmp(zero) == 0 {
|
||||
return k
|
||||
}
|
||||
k.Add(k, inc[i])
|
||||
if k.Cmp(max) > 0 {
|
||||
break
|
||||
}
|
||||
i = (i + 1) % 8
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func smallestPrimeFactor(n *big.Int) *big.Int {
|
||||
s := smallestPrimeFactorWheel(n)
|
||||
if s != nil {
|
||||
return s
|
||||
}
|
||||
c := big.NewInt(1)
|
||||
s = new(big.Int).Set(n)
|
||||
for n.Cmp(max) > 0 {
|
||||
d := pollardRho(n, c)
|
||||
if d.Cmp(zero) == 0 {
|
||||
if c.Cmp(ten) == 0 {
|
||||
log.Fatal("Pollard Rho doesn't appear to be working.")
|
||||
}
|
||||
c.Add(c, one)
|
||||
} else {
|
||||
// can't be sure PR will find the smallest prime factor first
|
||||
if d.Cmp(s) < 0 {
|
||||
s.Set(d)
|
||||
}
|
||||
n.Quo(n, d)
|
||||
if n.ProbablyPrime(5) {
|
||||
if n.Cmp(s) < 0 {
|
||||
return n
|
||||
}
|
||||
return s
|
||||
}
|
||||
}
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
func main() {
|
||||
k := 19
|
||||
fmt.Println("First", k, "terms of the Euclid–Mullin sequence:")
|
||||
fmt.Println(2)
|
||||
prod := big.NewInt(2)
|
||||
z := new(big.Int)
|
||||
count := 1
|
||||
for count < k {
|
||||
z.Add(prod, one)
|
||||
t := smallestPrimeFactor(z)
|
||||
fmt.Println(t)
|
||||
prod.Mul(prod, t)
|
||||
count++
|
||||
}
|
||||
}
|
||||
91
Task/Euclid-Mullin-sequence/Java/euclid-mullin-sequence.java
Normal file
91
Task/Euclid-Mullin-sequence/Java/euclid-mullin-sequence.java
Normal file
|
|
@ -0,0 +1,91 @@
|
|||
import java.math.BigInteger;
|
||||
import java.util.ArrayList;
|
||||
import java.util.BitSet;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
|
||||
public class EulerMullinSequence {
|
||||
|
||||
public static void main(String[] aArgs) {
|
||||
primes = listPrimesUpTo(1_000_000);
|
||||
|
||||
System.out.println("The first 27 terms of the Euler-Mullin sequence:");
|
||||
System.out.println(2);
|
||||
for ( int i = 1; i < 27; i++ ) {
|
||||
System.out.println(nextEulerMullin());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static BigInteger nextEulerMullin() {
|
||||
BigInteger smallestPrime = smallestPrimeFactor(product.add(BigInteger.ONE));
|
||||
product = product.multiply(smallestPrime);
|
||||
|
||||
return smallestPrime;
|
||||
}
|
||||
|
||||
private static BigInteger smallestPrimeFactor(BigInteger aNumber) {
|
||||
if ( aNumber.isProbablePrime(probabilityLevel) ) {
|
||||
return aNumber;
|
||||
}
|
||||
|
||||
for ( BigInteger prime : primes ) {
|
||||
if ( aNumber.mod(prime).signum() == 0 ) {
|
||||
return prime;
|
||||
}
|
||||
}
|
||||
|
||||
BigInteger factor = pollardsRho(aNumber);
|
||||
return smallestPrimeFactor(factor);
|
||||
}
|
||||
|
||||
private static BigInteger pollardsRho(BigInteger aN) {
|
||||
if ( aN.equals(BigInteger.ONE) ) {
|
||||
return BigInteger.ONE;
|
||||
}
|
||||
|
||||
if ( aN.mod(BigInteger.TWO).signum() == 0 ) {
|
||||
return BigInteger.TWO;
|
||||
}
|
||||
|
||||
final BigInteger core = new BigInteger(aN.bitLength(), random);
|
||||
BigInteger x = new BigInteger(aN.bitLength(), random);
|
||||
BigInteger xx = x;
|
||||
BigInteger divisor = null;
|
||||
|
||||
do {
|
||||
x = x.multiply(x).mod(aN).add(core).mod(aN);
|
||||
xx = xx.multiply(xx).mod(aN).add(core).mod(aN);
|
||||
xx = xx.multiply(xx).mod(aN).add(core).mod(aN);
|
||||
divisor = x.subtract(xx).gcd(aN);
|
||||
} while ( divisor.equals(BigInteger.ONE) );
|
||||
|
||||
return divisor;
|
||||
}
|
||||
|
||||
private static List<BigInteger> listPrimesUpTo(int aLimit) {
|
||||
BitSet sieve = new BitSet(aLimit + 1);
|
||||
sieve.set(2, aLimit + 1);
|
||||
|
||||
final int squareRoot = (int) Math.sqrt(aLimit);
|
||||
for ( int i = 2; i <= squareRoot; i = sieve.nextSetBit(i + 1) ) {
|
||||
for ( int j = i * i; j <= aLimit; j = j + i ) {
|
||||
sieve.clear(j);
|
||||
}
|
||||
}
|
||||
|
||||
List<BigInteger> result = new ArrayList<BigInteger>(sieve.cardinality());
|
||||
for ( int i = 2; i >= 0; i = sieve.nextSetBit(i + 1) ) {
|
||||
result.add(BigInteger.valueOf(i));
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private static List<BigInteger> primes;
|
||||
private static BigInteger product = BigInteger.TWO;
|
||||
private static ThreadLocalRandom random = ThreadLocalRandom.current();
|
||||
|
||||
private static final int probabilityLevel = 20;
|
||||
|
||||
}
|
||||
16
Task/Euclid-Mullin-sequence/Jq/euclid-mullin-sequence.jq
Normal file
16
Task/Euclid-Mullin-sequence/Jq/euclid-mullin-sequence.jq
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
# Output: the Euclid-Mullins sequence, beginning with 2
|
||||
def euclid_mullins:
|
||||
foreach range(1; infinite|floor) as $i ( { product: 1 };
|
||||
.next = .product + 1
|
||||
# find the first prime factor of .next
|
||||
| .p = 3
|
||||
| .found = false
|
||||
| until( .p * .p > .next or .found;
|
||||
.found = ((.next % .p) == 0)
|
||||
| if .found then . else .p += 2 end)
|
||||
| if .found then .next = .p else . end
|
||||
| .product *= .next)
|
||||
| .next ;
|
||||
|
||||
# Produce 16 terms
|
||||
limit(16; euclid_mullins)
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
using Primes
|
||||
|
||||
struct EuclidMullin end
|
||||
|
||||
Base.length(em::EuclidMullin) = 1000 # not expected to get to 1000
|
||||
Base.eltype(em::EuclidMullin) = BigInt
|
||||
Base.iterate(em::EuclidMullin, t=big"1") = (p = first(first(factor(t + 1).pe)); (p, t * p))
|
||||
|
||||
println("First 16 Euclid-Mullin numbers: ", join(Iterators.take(EuclidMullin(), 16), ", "))
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
list = {2};
|
||||
Do[
|
||||
prod = Times @@ list;
|
||||
prod++;
|
||||
new = Min[FactorInteger[prod][[All, 1]]];
|
||||
AppendTo[list, new]
|
||||
,
|
||||
{21 - 1}
|
||||
];
|
||||
list
|
||||
83
Task/Euclid-Mullin-sequence/Nim/euclid-mullin-sequence.nim
Normal file
83
Task/Euclid-Mullin-sequence/Nim/euclid-mullin-sequence.nim
Normal file
|
|
@ -0,0 +1,83 @@
|
|||
import integers
|
||||
|
||||
let
|
||||
Zero = newInteger()
|
||||
One = newInteger(1)
|
||||
Two = newInteger(2)
|
||||
Three = newInteger(3)
|
||||
Five = newInteger(5)
|
||||
Ten = newInteger(10)
|
||||
Max = newInteger(100000)
|
||||
None = newInteger(-1)
|
||||
|
||||
proc pollardRho(n, c: Integer): Integer =
|
||||
|
||||
template g(x: Integer): Integer = (x * x + c) mod n
|
||||
|
||||
var
|
||||
x = newInteger(2)
|
||||
y = newInteger(2)
|
||||
z = newInteger(1)
|
||||
d = Max + 1
|
||||
count = 0
|
||||
while true:
|
||||
x = g(x)
|
||||
y = g(g(y))
|
||||
d = abs(x - y) mod n
|
||||
z *= d
|
||||
inc count
|
||||
if count == 100:
|
||||
d = gcd(z, n)
|
||||
if d != One: break
|
||||
z = newInteger(1)
|
||||
count = 0
|
||||
result = if d == n: Zero else: d
|
||||
|
||||
template isEven(n: Integer): bool = isZero(n and 1)
|
||||
|
||||
proc smallestPrimeFactorWheel(n: Integer): Integer =
|
||||
if n.isPrime(5): return n
|
||||
if n.isEven: return Two
|
||||
if isZero(n mod 3): return Three
|
||||
if isZero(n mod 5): return Five
|
||||
var k = newInteger(7)
|
||||
var i = 0
|
||||
const Inc = [4, 2, 4, 2, 4, 6, 2, 6]
|
||||
while k * k <= n:
|
||||
if isZero(n mod k): return k
|
||||
k += Inc[i]
|
||||
if k > Max: return None
|
||||
i = (i + 1) mod 8
|
||||
|
||||
proc smallestPrimeFactor(n: Integer): Integer =
|
||||
var n = n
|
||||
result = smallestPrimeFactorWheel(n)
|
||||
if result != None: return
|
||||
var c = One
|
||||
result = newInteger(n)
|
||||
while n > Max:
|
||||
var d = pollardRho(n, c)
|
||||
if d.isZero:
|
||||
if c == Ten:
|
||||
quit "Pollard Rho doesn't appear to be working.", QuitFailure
|
||||
inc c
|
||||
else:
|
||||
# Can't be sure PR will find the smallest prime factor first.
|
||||
result = min(result, d)
|
||||
n = n div d
|
||||
if n.isPrime(2):
|
||||
return min(result, n)
|
||||
|
||||
proc main() =
|
||||
var k = 19
|
||||
echo "First ", k, " terms of the Euclid–Mullin sequence:"
|
||||
echo 2
|
||||
var prod = newInteger(2)
|
||||
var count = 1
|
||||
while count < k:
|
||||
let t = smallestPrimeFactor(prod + One)
|
||||
echo t
|
||||
prod *= t
|
||||
inc count
|
||||
|
||||
main()
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
E=vector(16)
|
||||
E[1]=2
|
||||
for(i=2,16,E[i]=factor(prod(n=1,i-1,E[n])+1)[1,1])
|
||||
print(E)
|
||||
10
Task/Euclid-Mullin-sequence/Perl/euclid-mullin-sequence.pl
Normal file
10
Task/Euclid-Mullin-sequence/Perl/euclid-mullin-sequence.pl
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
use strict;
|
||||
use warnings;
|
||||
use feature 'say';
|
||||
use ntheory <factor vecprod vecmin>;
|
||||
|
||||
my @Euclid_Mullin = 2;
|
||||
push @Euclid_Mullin, vecmin factor (1 + vecprod @Euclid_Mullin) for 2..16+11;
|
||||
|
||||
say "First sixteen: @Euclid_Mullin[ 0..15]";
|
||||
say "Next eleven: @Euclid_Mullin[16..26]";
|
||||
15
Task/Euclid-Mullin-sequence/Phix/euclid-mullin-sequence.phix
Normal file
15
Task/Euclid-Mullin-sequence/Phix/euclid-mullin-sequence.phix
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
(phixonline)-->
|
||||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
|
||||
<span style="color: #7060A8;">requires</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"1.0.1"</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- (added mpz_set_v())</span>
|
||||
<span style="color: #008080;">include</span> <span style="color: #004080;">mpfr</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
|
||||
|
||||
<span style="color: #004080;">sequence</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
|
||||
<span style="color: #004080;">mpz</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">total</span><span style="color: #0000FF;">,</span><span style="color: #000000;">tmp</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;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">while</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">)<</span><span style="color: #000000;">16</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #7060A8;">mpz_add_si</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tmp</span><span style="color: #0000FF;">,</span><span style="color: #000000;">total</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #7060A8;">mpz_set_v</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tmp</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">mpz_pollard_rho</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tmp</span><span style="color: #0000FF;">)[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">][</span><span style="color: #000000;">1</span><span style="color: #0000FF;">])</span>
|
||||
<span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">mpz_get_str</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tmp</span><span style="color: #0000FF;">))</span>
|
||||
<span style="color: #7060A8;">mpz_mul</span><span style="color: #0000FF;">(</span><span style="color: #000000;">total</span><span style="color: #0000FF;">,</span><span style="color: #000000;">total</span><span style="color: #0000FF;">,</span><span style="color: #000000;">tmp</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
|
||||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"The first 16 Euclid-Mulin numbers: %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">)})</span>
|
||||
<!--
|
||||
14
Task/Euclid-Mullin-sequence/Python/euclid-mullin-sequence.py
Normal file
14
Task/Euclid-Mullin-sequence/Python/euclid-mullin-sequence.py
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
""" Rosetta code task: Euclid-Mullin_sequence """
|
||||
|
||||
from primePy import primes
|
||||
|
||||
def euclid_mullin():
|
||||
""" generate Euclid-Mullin sequence """
|
||||
total = 1
|
||||
while True:
|
||||
next_iter = primes.factor(total + 1)
|
||||
total *= next_iter
|
||||
yield next_iter
|
||||
|
||||
GEN = euclid_mullin()
|
||||
print('First 16 Euclid-Mullin numbers:', ', '.join(str(next(GEN)) for _ in range(16)))
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
use Prime::Factor;
|
||||
|
||||
my @Euclid-Mullin = 2, { state $i = 1; (1 + [×] @Euclid-Mullin[^$i++]).&prime-factors.min } … *;
|
||||
|
||||
put 'First sixteen: ', @Euclid-Mullin[^16];
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
func f(n) is cached {
|
||||
return 2 if (n == 1)
|
||||
lpf(1 + prod(1..^n, {|k| f(k) }))
|
||||
}
|
||||
|
||||
say f.map(1..16)
|
||||
say f.map(17..27)
|
||||
|
|
@ -0,0 +1,79 @@
|
|||
import "./big" for BigInt
|
||||
|
||||
var zero = BigInt.zero
|
||||
var one = BigInt.one
|
||||
var two = BigInt.two
|
||||
var ten = BigInt.ten
|
||||
var max = BigInt.new(100000)
|
||||
|
||||
var pollardRho = Fn.new { |n, c|
|
||||
var g = Fn.new { |x, y| (x*x + c) % n }
|
||||
var x = two
|
||||
var y = two
|
||||
var z = one
|
||||
var d = max + one
|
||||
var count = 0
|
||||
while (true) {
|
||||
x = g.call(x, n)
|
||||
y = g.call(g.call(y, n), n)
|
||||
d = (x - y).abs % n
|
||||
z = z * d
|
||||
count = count + 1
|
||||
if (count == 100) {
|
||||
d = BigInt.gcd(z, n)
|
||||
if (d != one) break
|
||||
z = one
|
||||
count = 0
|
||||
}
|
||||
}
|
||||
if (d == n) return zero
|
||||
return d
|
||||
}
|
||||
|
||||
var smallestPrimeFactorWheel = Fn.new { |n|
|
||||
if (n.isProbablePrime(5)) return n
|
||||
if (n % 2 == zero) return BigInt.two
|
||||
if (n % 3 == zero) return BigInt.three
|
||||
if (n % 5 == zero) return BigInt.five
|
||||
var k = BigInt.new(7)
|
||||
var i = 0
|
||||
var inc = [4, 2, 4, 2, 4, 6, 2, 6]
|
||||
while (k * k <= n) {
|
||||
if (n % k == zero) return k
|
||||
k = k + inc[i]
|
||||
if (k > max) return null
|
||||
i = (i + 1) % 8
|
||||
}
|
||||
}
|
||||
|
||||
var smallestPrimeFactor = Fn.new { |n|
|
||||
var s = smallestPrimeFactorWheel.call(n)
|
||||
if (s) return s
|
||||
var c = one
|
||||
s = n
|
||||
while (n > max) {
|
||||
var d = pollardRho.call(n, c)
|
||||
if (d == 0) {
|
||||
if (c == ten) Fiber.abort("Pollard Rho doesn't appear to be working.")
|
||||
c = c + one
|
||||
} else {
|
||||
// can't be sure PR will find the smallest prime factor first
|
||||
s = BigInt.min(s, d)
|
||||
n = n / d
|
||||
if (n.isProbablePrime(2)) return BigInt.min(s, n)
|
||||
}
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
var k = 16
|
||||
System.print("First %(k) terms of the Euclid–Mullin sequence:")
|
||||
System.print(2)
|
||||
var prod = BigInt.two
|
||||
var count = 1
|
||||
while (count < k) {
|
||||
var t = smallestPrimeFactor.call(prod + one)
|
||||
System.print(t)
|
||||
prod = prod * t
|
||||
count = count + 1
|
||||
}
|
||||
|
|
@ -0,0 +1,53 @@
|
|||
/* euclid_mullin_gmp.wren */
|
||||
|
||||
import "./gmp" for Mpz
|
||||
|
||||
var max = Mpz.from(100000)
|
||||
|
||||
var smallestPrimeFactorWheel = Fn.new { |n|
|
||||
if (n.probPrime(15) > 0) return n
|
||||
if (n.isEven) return Mpz.two
|
||||
if (n.isDivisibleUi(3)) return Mpz.three
|
||||
if (n.isDivisibleUi(5)) return Mpz.five
|
||||
var k = Mpz.from(7)
|
||||
var i = 0
|
||||
var inc = [4, 2, 4, 2, 4, 6, 2, 6]
|
||||
while (k * k <= n) {
|
||||
if (n.isDivisible(k)) return k
|
||||
k.add(inc[i])
|
||||
if (k > max) return null
|
||||
i = (i + 1) % 8
|
||||
}
|
||||
}
|
||||
|
||||
var smallestPrimeFactor = Fn.new { |n|
|
||||
var s = smallestPrimeFactorWheel.call(n)
|
||||
if (s) return s
|
||||
var c = Mpz.one
|
||||
s = n.copy()
|
||||
while (n > max) {
|
||||
var d = Mpz.pollardRho(n, 2, c)
|
||||
if (d.isZero) {
|
||||
if (c == 100) Fiber.abort("Pollard Rho doesn't appear to be working.")
|
||||
c.inc
|
||||
} else {
|
||||
// can't be sure PR will find the smallest prime factor first
|
||||
s.min(d)
|
||||
n.div(d)
|
||||
if (n.probPrime(5) > 0) return Mpz.min(s, n)
|
||||
}
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
var k = 19
|
||||
System.print("First %(k) terms of the Euclid–Mullin sequence:")
|
||||
System.print(2)
|
||||
var prod = Mpz.two
|
||||
var count = 1
|
||||
while (count < k) {
|
||||
var t = smallestPrimeFactor.call(prod + Mpz.one)
|
||||
System.print(t)
|
||||
prod.mul(t)
|
||||
count = count + 1
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue