Message Boards Message Boards


Function for "Primorials"?

Posted 10 months ago
8 Replies
2 Total Likes

So, been trying to figure out how to do something, and researching it online, and came across the term "Primorial(s)"... Meaning the Product of the first n Prime Numbers. (Say, multiplying the first n=10 Prime Numbers together, etc.)

Is there a function in Mathematica for this? I'm having a tough time finding one. Seems fairly basic. Like I'm pretty sure there's a function to list the n-th Prime number. Yep:

So, one would think it would be simple to implement a Function that multiplies the first n such primes together, and call that function "Primorial [n]," or whatever?

I see an entry on Math World:

But, I'm having a hard time locating an actual Mathematica function for it? Is there one?

If not, could Wolfram please create one and add it perhaps to the next iterative .x release of Mathematica? For those of us interested in Prime #'s, Prime Theory, Prime Sieving, and such.

There's actually a fairly real-world usage case for it in Sieving. Would be handy to have a simple function for it. Where you could just be like Primorial[10]; and have it return the answer... Which looks to be around ... jeez, even Wolfram|Alpha can't parse my input "Product of the first 10 prime numbers." It'll do "Sum" fine. Sigh Okay, doing it manually... Around ... 6,469,693,230

So, yeah, it'd be nice if one had a simple function to input:


and get output:



Does this exist, or can it be added?

If it doesn't exist? How can it be done in Mathematica? Can one do like a table / array or something and the take the product of all of it?

And then, for the thing I kind of want to do, count the digits of the result with "IntegerLength[]"?

8 Replies
Posted 10 months ago

Okay, sort of solved my own problem after a bit of laborious searching out functions...

FirstPrime = 1;
LastPrime = 8;
PrimeRange = Range[FirstPrime, LastPrime];
PrimeList = Prime[PrimeRange]
Times @@ PrimeList
IntegerLength[Times @@ PrimeList]


{2, 3, 5, 7, 11, 13, 17, 19}



Guess that gets done what I wanted to get done.

But, would be really nice if Mathematica had a built-in "Primorial [n]" function to do the heavy lifting of the first part (mutliplying the first n Prime Numbers together).

Seems like it would be pretty simple to put together an official function, based on the above... Something that'd basically just be like:

Primorial [8]

Out: 9699690

Then one could say, like:


Out: 7


Why does this matter? Well, for one it's just sort of interesting from a pure math perspective.

My particular interest is actually wondering how many ordinal Prime Numbers one would have to multiply together in order to get a billion digit number (not itself Prime, obviously, as it would be composite of multiple Primes but getting into like billion-digit+ territory).

Posted 10 months ago

Just for kicks-and-giggles, suppressing a bunch of display output to get a final number:

The Product of the first 10,000 Primes renders a 45,337-digit composite number.

n=100,000: 563,921-digits long.

n=1,000,000: 6,722,809-digits long

n=10,000,000: 77,919,922-digits long (that took a minute to compute!)

n=100,000,000: 885,105,237-digits long. (Heh! That took a while... Almost there)

n=200,000,000: ...we'll have to see how long this takes, assuming it can complete it. ;)

I suspect a 1 billion+ digit Product will fall somewhere between the Products of the first 100mil & 200mil primes. But, what do I know? I just play here. ^_^ (If 200mil primes is over 1bn digits, I can probably narrow it down by halving the difference and halving again 'til narrowed down to exactly how many Primes are needed for the Product to cross the 1bn threshold... Might take a while and a few iterations, tho'...)

EDIT: Well, darn, seems like the Mathematica Kernel craps out after about 20 minutes to 1/2 hour on trying to compute on 200,000,000. Sigh ;)

Posted 2 months ago

I have worked this same problem. I see no contact for you, you can reach out at I have had the same desire for a built-in function

Posted 2 months ago

Hi Bill,

Did you look at the primorial function implementation I provided as an answer? Were you looking for something different?

Posted 2 months ago

I am not looking for anything, just trying to assist Mr Gmirkin up at his higher range. I understand the code completely.

A back-of-the-envelope computation suggests that to get n digits, one requires in the ballpark of LogIntegral[Log[10.]*10^n] primes. For n=10^9, that's around 112,307,000.

Quick check at 10^5. Approximate the count:

In[1002]:= LogIntegral[Log[10.]*10^5]

(* Out[1002]= 20500.5 *)

Check how many digits we get:

In[1004]:= Log[10., Product[Prime[j], {j, 20500}] // N]

(* Out[1004]= 100068. *)
Posted 10 months ago

Hi Michael,

One way to implement primorial.

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

(* 6469693230 *)
Posted 10 months ago

Hmm, yeah, that does seem a perhaps slightly more compact way to do it than what I did. :)


Would be nifty if they just had a straight function for it, of course...

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract