Jump to content

Primorial numbers: Difference between revisions

Added JavaScript
m (→‎{{header|Wren}}: Minor tidy)
(Added JavaScript)
Line 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 (to 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}}==
3,044

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.