forked from ProAlgos/ProAlgos-Cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprimorial.hpp
92 lines (74 loc) · 2.39 KB
/
primorial.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
Primorial
---------
Calculate the primorial Pn# for the prime number definition of primorial
and n# for the natural number definition of primorial.
Pn# is defined as the product of the first n primes.
n# is defined as the product of all primes less than or equal to n.
Time complexity
---------------
O(N^2) where N is n for the natural definition and Pn for the prime
definition.
Space complexity
----------------
O(N) where N is n for the natural definition and Pn for the prime
definition.
*/
#ifndef PRIMORIAL_HPP
#define PRIMORIAL_HPP
#include <climits>
#include <vector>
typedef unsigned long long int ULL;
// primorial_natural(n) goes beyond the range of ULL where n > 52
const unsigned int MAX_N_NATURAL = 52;
// primorial(n) goes beyond the range of ULL where n > 15
const unsigned int MAX_N = 15;
// Computes the primorial defined by natural numbers: https://oeis.org/A034386
// Returns 0 if n# is too big to fit in an ULL
// Otherwise returns the primorial n#, where n# = the product of all primes ≤ n
ULL primorial_natural(unsigned int n) {
// Check for unsigned integer wraparound
if (n > MAX_N_NATURAL) {
return 0;
}
std::vector<unsigned int> primes;
ULL product = 1;
for (unsigned int i = 2; i <= n; i++) {
bool is_prime = true;
for (auto it = primes.begin(); is_prime && it != primes.end(); it++) {
if (i % *it == 0) {
is_prime = false;
}
}
if (is_prime) {
product *= i;
primes.push_back(i);
}
}
return product;
}
// Computes the primorial defined by prime numbers: https://oeis.org/A002110
// Returns 0 if Pn# is too big to fit in an ULL
// Otherwise returns the primorial Pn#, where Pn# = the product of the first n primes
ULL primorial(unsigned int n) {
// Check for unsigned integer wraparound
if (n > MAX_N) {
return 0;
}
std::vector<unsigned int> primes;
ULL product = 1;
for (unsigned int i = 2; primes.size() < n; i++) {
bool is_prime = true;
for (auto it = primes.begin(); is_prime && it != primes.end(); it++) {
if (i % *it == 0) {
is_prime = false;
}
}
if (is_prime) {
product *= i;
primes.push_back(i);
}
}
return product;
}
#endif // PRIMORIAL_HPP