Primorial numbers | Rosetta Code | #11

URL to the problem page: http://rosettacode.org/wiki/Primorial_numbers


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, primorialis the product of the first n (successive) primes.
In some sense, generating primorial numbers is similar to factorials.
As with factorials, primorial numbers get large quickly.

Show the first ten primorial numbers (0 ──► 9, inclusive).



#include <iostream>
using namespace std;

int main() {
    int primorial[100], cnt = 1, cnt2;
    primorial[0] = 1;
    cout << "First ten primorial numbers (0 ---> 9, inclusive) are:" << endl << primorial[0] << endl;
    for (int i = 2; cnt < 10; i++) {
        cnt2 = 0;
        for (int j = 2; j <= sqrt(i); j++) {
            if (i % j == 0) {
                cnt2++;
                break;
            }
        }
        if (cnt2 == 0) {
            primorial[cnt] = primorial[cnt - 1] * i;
            cout << primorial[cnt] << endl;
            cnt++;
        }
    }
    return 0;
}



Comments

My photo
Ercan Tomac
instagram.com/ercantomac