Primorial numbers: Difference between revisions

m
 
(3 intermediate revisions by 3 users not shown)
Line 93:
primorial(100000) has 563921 digits
</pre>
 
=={{header|Arturo}}==
 
<syntaxhighlight lang="arturo">primes: [2] ++ select.first: 99999 range.step: 2 3 ∞ => prime?
 
primorial: function [n][
if 0 = n -> return 1
product take primes n
]
 
print "First ten primorials:"
loop 0..9 => [print primorial &]
print ""
loop 1..5 'm -> print [
"primorial" 10^m "has" size ~"|primorial 10^m|" "digits"
]</syntaxhighlight>
 
{{out}}
 
<pre>First ten primorials:
1
2
6
30
210
2310
30030
510510
9699690
223092870
 
primorial 10 has 10 digits
primorial 100 has 220 digits
primorial 1000 has 3393 digits
primorial 10000 has 45337 digits
primorial 100000 has 563921 digits</pre>
 
=={{header|C}}==
Line 1,547 ⟶ 1,583:
primorial(10^4) has length 45337
primorial(10^5) has length 563921</pre>
 
=={{header|JavaScript}}==
===Exact integers===
{{works with|Node.js}}
Uses the native BigInt type availiable in Node.js. As this takes a while, some values between 10 000 and 100 000 are shown to help suggest it is still doing stuff...
<syntaxhighlight lang="javascript">
{ // calculate and show some primorial numbers
'use strict'
let primorial = 1n
let prime = 1n
let pn = []
const maxNumber = 2000000
let isPrime = []
for( let i = 1; i <= maxNumber; i ++ ){ isPrime[ i ] = i % 2 != 0 }
isPrime[ 1 ] = false
isPrime[ 2 ] = true
const rootMaxNumber = Math.floor( Math.sqrt( maxNumber ) )
for( let s = 3; s <= rootMaxNumber; s += 2 ){
if( isPrime[ s ] ){
for( let p = s * s; p <= maxNumber; p += s ){ isPrime[ p ] = false }
}
}
 
const primeMax = 100000
pn[ 0 ] = 1
let nextToShow = 10
for( let i = 1; i <= primeMax; i ++ ){
// find the next prime
prime += 1n
while( ! isPrime[ prime ] ){ prime += 1n }
primorial *= prime
if( i < 10 ){
pn[ i ] = primorial
}
else if( i == nextToShow ){
if( nextToShow < 10000 ){
nextToShow *= 10
}
else{
nextToShow += 10000
}
if( i == 10 ){ console.log( "primorials 0-9: ", pn.toString() ) }
// show the number of digits in the primorial
let p = primorial
let length = 0
while( p > 0 ){
length += 1
p /= 10n
}
console.log( "length of primorial " + i + " is "+ length )
}
}
}
</syntaxhighlight>
{{out}}
<pre>
primorials 0-9: 1,2,6,30,210,2310,30030,510510,9699690,223092870
length of primorial 10 is 10
length of primorial 100 is 220
length of primorial 1000 is 3393
length of primorial 10000 is 45337
length of primorial 20000 is 97389
length of primorial 30000 is 151937
length of primorial 40000 is 208100
length of primorial 50000 is 265460
length of primorial 60000 is 323772
length of primorial 70000 is 382876
length of primorial 80000 is 442655
length of primorial 90000 is 503026
length of primorial 100000 is 563921
</pre>
 
Runtime is around 10 minutes on the Windows 11 laptop I'm using (too long for TIO.RUN).
 
===Using a reduced number of digits===
{{works with|Node.js}}
A modification of the Exact integers version, also using the native BigInt type.
As we only need to show digit counts, this deviates from the task requirement to use exact integers by only keeping around 500 leading digits., which is MUCH faster.
<syntaxhighlight lang="javascript">
{ // calculate and show some primorial numbers
'use strict'
let primorial = 1n
let prime = 1n
let pn = []
const maxNumber = 2000000
let isPrime = []
for( let i = 1; i <= maxNumber; i ++ ){ isPrime[ i ] = i % 2 != 0 }
isPrime[ 1 ] = false
isPrime[ 2 ] = true
const rootMaxNumber = Math.floor( Math.sqrt( maxNumber ) )
for( let s = 3; s <= rootMaxNumber; s += 2 ){
if( isPrime[ s ] ){
for( let p = s * s; p <= maxNumber; p += s ){ isPrime[ p ] = false }
}
}
 
const primeMax = 100000
pn[ 0 ] = 1
let nextToShow = 10
let reducedDigits = 0
const n500 = 10n**500n
const n1000 = 10n**1000n
for( let i = 1; i <= primeMax; i ++ ){
// find the next prime
prime += 1n
while( ! isPrime[ prime ] ){ prime += 1n }
primorial *= prime
if( i < 10 ){
pn[ i ] = primorial
}
else if( i == nextToShow ){
if( nextToShow < 10000 ){
nextToShow *= 10
}
else{
nextToShow += 10000
}
if( i == 10 ){ console.log( "primorials 0-9: ", pn.toString() ) }
// show the number of digits in the primorial
let p = primorial
let length = 0
while( p > 0 ){
length += 1
p /= 10n
}
console.log( "length of primorial " + i + " is "+ ( reducedDigits + length ) )
}
if( primorial > n1000 ){
// the number has more than 1000 digits - reduce it to 500-ish
primorial /= n500
reducedDigits += 500
}
}
}
</syntaxhighlight>
{{out}}
<pre>
primorials 0-9: 1,2,6,30,210,2310,30030,510510,9699690,223092870
length of primorial 10 is 10
length of primorial 100 is 220
length of primorial 1000 is 3393
length of primorial 10000 is 45337
length of primorial 20000 is 97389
length of primorial 30000 is 151937
length of primorial 40000 is 208100
length of primorial 50000 is 265460
length of primorial 60000 is 323772
length of primorial 70000 is 382876
length of primorial 80000 is 442655
length of primorial 90000 is 503026
length of primorial 100000 is 563921
</pre>
 
Runtime is around 1 second on TIO.RUN.
 
=={{header|jq}}==
Line 2,876 ⟶ 3,068:
{{libheader|Wren-fmt}}
It takes very little time to sieve for the first 100,000 primes (< 0.2 seconds) but, despite using the 'binary splitting' algorithm and a trick of my own, multiplying them all together is very slow compared to the GMP-based solutions taking more than 4 minutes on my machine.
<syntaxhighlight lang="ecmascriptwren">import "./big" for BigInt
import "./math" for Int
import "./fmt" for Fmt
 
var vecprod = Fn.new { |primes|
Line 2,934 ⟶ 3,126:
{{libheader|Wren-gmp}}
Far quicker, of course than the CLI version, completing in around 2.0 seconds.
<syntaxhighlight lang="ecmascriptwren">import "./math" for Int
import "./gmp" for Mpz
import "./fmt" for Fmt
3,060

edits