Primorial numbers
Primorial numbers is the number series formed by multiplying successive prime numbers.
The series is:
- primorial(0) = 1 (by definition)
- primorial(1) = 2 (2)
- primorial(2) = 6 (2*3)
- primorial(3) = 30 (2*3*5)
- primorial(4) = 210 (2*3*5*7)
- primorial(5) = 2310 (2*3*5*7*11)
- primorial(6) = 30030 (2*3*5*7*11*13)
- ∙ ∙ ∙
To express this mathematically, primorialn is
the product of the first n (successive) primes:
where primek is the kth prime number.
In some sense, generating primorial numbers is similar to factorials. As with factorials, primorial numbers get large quickly.
task requirements:
- Show the first ten primorial numbers (0 ──► 9, inclusive).
- Show the length of primorial numbers whose index is: 10 100 1,000 10,000 and 100,000.
- Show the length of the one millionth primorial number (optionally).
links:
See the MathWorld webpage: primorial
See the Wikipedia webpage: primorial.
See the OEIS webpage: A2110.
REXX
<lang rexx>/*REXX program computes some primorial numbers for low #s, and for 10^n. */ parse arg N H . /*get optional arguments: N, L, H */ if N== | N==',' then N=10 /*was N given? Then use the default.*/ if H== | H==',' then H=100000 /*was H given? Then use the default.*/ numeric digits 600000; w=length(digits()) /* be able to handle large numbers.*/ @.=.; @.0=1; @.1=2; @.2=3; @.3=5; @.4=7; @.5=11; @.6=13 /*some low primes.*/
s.1=4; s.2=9; s.3=25; s.4=49; s.5=121; s.6=169 /*squared primes. */
- =6 /*number of primes*/
do j=0 for N /*calculate the first N primorial #s.*/ say right(j,length(N)) ' primorial is: ' primorial(j) end /*j*/
p=1; say
do k=1 for H /*process a large range of numbers. */ p=p*prime(k) /*calculate the next primorial number. */ parse var k L 2 -1 R /*get the left and rightmost dec digits*/ if R\==0 then iterate /*if right─most decimal digit\==0, skip*/ if L\==1 then iterate /* " left─most " " \==1, " */ if strip(k,,0)\==1 then iterate /*Not a power of 10? Then skip this K.*/ say right(k,w) ' primorial length in decimal digits is:' right(length(p),w) end /*k*/
exit /*stick a fork in it, we're all done. */ /*──────────────────────────────────PRIMORIAL subroutine──────────────────────*/ primorial: procedure expose @. s. #; parse arg y; !=1 /*obtain the arg Y.*/
do p=0 to y; !=!*prime(p); end /*p*/ /*calculate product.*/
return ! /*return with the #.*/ /*──────────────────────────────────PRIME subroutine──────────────────────────*/ prime: procedure expose @. s. #; parse arg n; if @.n\==. then return @.n numeric digits 9 /*limit digs to min.*/
do j=@.#+2 by 2 /*start looking at #*/ if j//2==0 then iterate; if j//3==0 then iterate /*divisible by 2│3 ?*/ parse var j -1 _; if _==5 then iterate /*right-most dig≡5? */ if j//7==0 then iterate; if j//11==0 then iterate /*divisible by 7│11?*/ do k=6 while s.k<=j; if j//@.k==0 then iterate j /*divide by primes. */ end /*k*/ #=#+1; @.#=j; s.#=j*j; return j /*next prime; return*/ end /*j*/</lang>
output when using the default inputs:
0 primorial is: 1 1 primorial is: 2 2 primorial is: 6 3 primorial is: 30 4 primorial is: 210 5 primorial is: 2310 6 primorial is: 30030 7 primorial is: 510510 8 primorial is: 9699690 9 primorial is: 223092870 10 primorial length in decimal digits is: 10 100 primorial length in decimal digits is: 220 1000 primorial length in decimal digits is: 3393 10000 primorial length in decimal digits is: 45337 100000 primorial length in decimal digits is: 563921