Primorial numbers are those formed by multiplying successive prime numbers.



The primorial number 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:



   



p
r
i
m
o
r
i
a

l

n


=

∏

k
=
1


n


p
r
i
m

e

k




{\displaystyle primorial_{n}=\prod _{k=1}^{n}prime_{k}}




 ─── where   



p
r
i
m

e

k




{\displaystyle prime_{k}}

   is the   kth   prime number.




In some sense, generating primorial numbers is similar to factorials.

As with factorials, primorial numbers get large quickly.



Task

   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   (optional). 
   Use exact integers, not approximations. 


By   length   (above), it is meant the number of decimal digits in the numbers. 




Related tasks

   Sequence of primorial primes
   Factorial
   Fortunate_numbers


See also

   the MathWorld webpage:   primorial
   the Wikipedia   webpage:   primorial.
   the   OEIS   webpage:   A002110.


#include <gmpxx.h>
#include <primesieve.hpp>
 
#include <cstdint>
#include <iomanip>
#include <iostream>
 
size_t digits(const mpz_class& n) { return n.get_str().length(); }
 
mpz_class primorial(unsigned int n) {
    mpz_class p;
    mpz_primorial_ui(p.get_mpz_t(), n);
    return p;
}
 
int main() {
    uint64_t index = 0;
    primesieve::iterator pi;
    std::cout << "First 10 primorial numbers:\n";
    for (mpz_class pn = 1; index < 10; ++index) {
        unsigned int prime = pi.next_prime();
        std::cout << index << ": " << pn << '\n';
        pn *= prime;
    }
    std::cout << "\nLength of primorial number whose index is:\n";
    for (uint64_t power = 10; power <= 1000000; power *= 10) {
        uint64_t prime = primesieve::nth_prime(power);
        std::cout << std::setw(7) << power << ": "
                  << digits(primorial(prime)) << '\n';
    }
    return 0;
}