How to resolve the algorithm Primorial numbers step by step in the Wren programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Primorial numbers step by step in the Wren programming language
Table of Contents
Problem Statement
Primorial numbers are those formed by multiplying successive prime numbers.
The primorial number series is: To express this mathematically, primorialn is the product of the first n (successive) primes:
In some sense, generating primorial numbers is similar to factorials. As with factorials, primorial numbers get large quickly.
By length (above), it is meant the number of decimal digits in the numbers.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Primorial numbers step by step in the Wren programming language
Source code in the wren programming language
import "/big" for BigInt
import "/math" for Int
import "/fmt" for Fmt
var vecprod = Fn.new { |primes|
var le = primes.count
if (le == 0) return BigInt.one
var s = List.filled(le, null)
for (i in 0...le) s[i] = BigInt.new(primes[i])
while (le > 1) {
var c = (le/2).floor
for(i in 0...c) s[i] = s[i] * s[le-i-1]
if (le & 1 == 1) c = c + 1
le = c
}
return s[0]
}
var primes = Int.primeSieve(1.3e6) // enough to generate first 100,000 primes
var prod = 1
System.print("The first ten primorial numbers are:")
for (i in 0..9) {
System.print("%(i): %(prod)")
prod = prod * primes[i]
}
System.print("\nThe following primorials have the lengths shown:")
// first multiply the first 100,000 primes together in pairs to reduce BigInt conversions needed
var primes2 = List.filled(50000, 0)
for (i in 0...50000) primes2[i] = primes[2*i] * primes[2*i+1]
for (i in [10, 100, 1000, 10000, 100000]) {
Fmt.print("$6d: $d", i, vecprod.call(primes2[0...i/2]).toString.count)
}
import "./math" for Int
import "./gmp" for Mpz
import "./fmt" for Fmt
var limit = 16 * 1e6 // more than enough to find first million primes
var primes = Int.primeSieve(limit-1)
primes.insert(0, 1)
System.print("The first ten primorial numbers are:")
var z = Mpz.new()
for (i in 0..9) System.print("%(i): %(z.primorial(primes[i]))")
System.print("\nThe following primorials have the lengths shown:")
for (i in [1e1, 1e2, 1e3, 1e4, 1e5, 1e6]) {
Fmt.print("$7d: $d", i, z.primorial(primes[i]).digitsInBase(10))
}
You may also check:How to resolve the algorithm Loops/While step by step in the PL/SQL programming language
You may also check:How to resolve the algorithm Averages/Root mean square step by step in the GEORGE programming language
You may also check:How to resolve the algorithm Arbitrary-precision integers (included) step by step in the Nim programming language
You may also check:How to resolve the algorithm Subleq step by step in the XPL0 programming language
You may also check:How to resolve the algorithm String append step by step in the Euphoria programming language