How to resolve the algorithm Euclid-Mullin sequence step by step in the Wren programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Euclid-Mullin sequence step by step in the Wren programming language
Table of Contents
Problem Statement
The 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. 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. Compute the next 11 elements of the sequence. OEIS sequence A000945
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Euclid-Mullin sequence step by step in the Wren programming language
Source code in the wren programming language
import "./big" for BigInt
var zero = BigInt.zero
var one = BigInt.one
var two = BigInt.two
var ten = BigInt.ten
var k100 = BigInt.new(100000)
var smallestPrimeFactorWheel = Fn.new { |n, max|
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, k100)
if (s) return s
var c = one
while (true) {
var d = BigInt.pollardRho(n, 2, c)
if (d == 0) {
if (c == ten) Fiber.abort("Pollard Rho doesn't appear to be working.")
c = c + one
} else {
// get the smallest prime factor of 'd'
var factor = smallestPrimeFactorWheel.call(d, d)
// check whether n/d has a smaller prime factor
s = smallestPrimeFactorWheel.call(n/d, factor)
return s ? BigInt.min(s, factor) : factor
}
}
}
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
}
/* Euclid_mullin_sequence_2.wren */
import "./gmp" for Mpz
var k100 = Mpz.from(100000)
var smallestPrimeFactorTrial = Fn.new { |n, max|
if (n.probPrime(15) > 0) return n
var k = Mpz.one
while (k * k <= n) {
k.nextPrime
if (k > max) return null
if (n.isDivisible(k)) return k
}
}
var smallestPrimeFactor = Fn.new { |n|
var s = smallestPrimeFactorTrial.call(n, k100)
if (s) return s
var c = Mpz.one
while (true) {
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 {
// get the smallest prime factor of 'd'
var factor = smallestPrimeFactorTrial.call(d, d)
// check whether n/d has a smaller prime factor
s = smallestPrimeFactorTrial.call(n/d, factor)
return s ? Mpz.min(s, factor) : factor
}
}
}
var k = 27
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
}
You may also check:How to resolve the algorithm Trigonometric functions step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm 21 game step by step in the Ring programming language
You may also check:How to resolve the algorithm Dice game probabilities step by step in the zkl programming language
You may also check:How to resolve the algorithm Strip control codes and extended characters from a string step by step in the XPL0 programming language
You may also check:How to resolve the algorithm Numerical integration step by step in the REXX programming language