# What is a good way to compute successive primorials with Mathematica?

Recall that the primorial of a positive integer $$n$$ is the product of the prime numbers smaller than $$n$$. One can define a primorial function in Mathematica quite easily:

primorial[n_?Positive] := Product[Prime[i], {i, 1, n}];

primorial[10]
(* 6469693230 *)


I am interested in calculating primorial[i] for $$2 in an efficient manner. Obviously, I can't just do a

Table[primorial[i],{i,2,10^8}]


which would be quite the opposite of efficient. On the other hand, I could use a running product of the first $$k$$ primes in a loop, and multiply with the next prime at each new iteration. But that does not feel to me like the ''Mathematica way'' to do things.

What would be an efficient way of achieving what I've described?

• A benchmark comparison of all solutions provided would be desirable. Both memory use and time. Commented Jan 3, 2023 at 15:14
• If you are interested on avoiding recomputing the same list many times, something similar to memoization, see this other Q&A Commented Jan 4, 2023 at 11:06
• For those not familiar with primorials: it's actually not the product of the "prime numbers smaller than n" as stated in the question but the product of the 1st to the n-th prime numbers. So primorial[10] is the product of the first 10 prime numbers. Not just 2x3x5x7.
– noox
Commented Jan 4, 2023 at 14:33

Mathematica code is already provided in the The On-Line Encyclopedia of Integer Sequences, here http://oeis.org/A002110

# oeis Sol 1

primorial1[n_] := FoldList[Times, 1, Prime[Range[n]]]


# oeis Sol 2

primorial2[n_] := Array[Product[Prime[i], {i, #}]&, n] (* José María Grau Ribas, Feb 15 2010 *)


# oeis Sol 3

 primorial3[n_] := Join[{1}, Denominator[Accumulate[1/Prime[Range[n]]]]] (* Harvey P. Dale, Apr 11 2012 *)


You didn't ask for it, but one may want to have a solution using memoization . See this other answer here.

• The first solution is quite elegant, and should suffice. Commented Jan 4, 2023 at 2:00
• @DavidG.Stork agree, but I wanted to put all the solutions listed on OEIS. I would have wanted also to do some clever Memoization that stored only the largest pre-computed list primorial3[m], and evaluated to Take[primorial3[m],n] for $n<m$ and updated the definitions of primorial3 with FoldList[Times, 1, Prime[Range[m,n]]] for $n>m$. But the OP didn't need that. And my first attempt looked ugly. Commented Jan 4, 2023 at 9:10

Luckily Prime is Listable and we can make an elegant solution with FoldList:

FoldList[Times, Prime[Range[5]]]

(* Out: {2, 6, 30, 210, 2310} *)


Replace 5 with any number you want.

Not sure if the following is considered a good way, but at least it gave results in realistic time frames and did not crash anything.

Create the list of primes and then multiply the elements of the list with a trick from the answer of Andrew Moylan in this post

Consider

list = Table[Prime[ii], {ii, 1, 10^8}]


this takes a little bit, but managed to give a result

Then, you perform

Exp[Total[Log[N[list]]]]


to get

Checking against the known result

(Exp[Total[Log[N[list[[1 ;; 10]]]]]])/
Times @@ list[[1 ;; 10]]