-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblem0047.cpp
98 lines (82 loc) · 2.22 KB
/
problem0047.cpp
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
93
94
95
96
97
98
/*
The first two consecutive numbers to have two distinct prime factors are:
14 = 2 × 7
15 = 3 × 5
The first three consecutive numbers to have three distinct prime factors are:
644 = 2² × 7 × 23
645 = 3 × 5 × 43
646 = 2 × 17 × 19.
Find the first four consecutive integers to have four distinct prime factors. What is the first of these numbers?
*/
#include <iostream>
#include <vector>
#include <cstring>
#include <cmath>
#define MAX 1000000
using namespace std;
int nextPrime(int, bool[]);
bool fourDistinct(int, vector<int>, bool[]);
void handleMultiples(int, bool[]);
int main() {
bool sieve[MAX];
memset(sieve, true, sizeof(sieve));
sieve[0] = 0;
sieve[1] = 0;
int prime = 2;
vector<int> primes;
for (int i = 0; i * i < MAX; ++i) {
handleMultiples(prime, sieve);
prime = nextPrime(prime, sieve);
}
for (int i = 0; i < MAX; ++i) {
if (sieve[i])
primes.push_back(i);
}
int i = 1;
for (int i = 0; i < 1000000; ++i) {
if (fourDistinct(i, primes, sieve)) {
if (fourDistinct(i+1, primes, sieve)) {
if (fourDistinct(i+2, primes, sieve)) {
if (fourDistinct(i+3, primes, sieve)) {
cout << i << endl;
break;
} else {
++i;
}
} else {
i += 2;
}
} else {
i += 3;
}
} else {
i += 4;
}
}
cout << i << endl;
return 0;
}
int nextPrime(int p, bool sieve[]) {
++p;
while (!sieve[p]) ++p;
return p;
}
void handleMultiples(int p, bool sieve[]) {
for (int i = p * p; i < MAX; i += p)
sieve[i] = false;
}
bool fourDistinct(int x, vector<int>primes, bool sieve[]) {
if (sieve[x])
return false;
int count = 0; // count the number of distinct prime factors of x
for (int i = 0; i < primes.size() && x > 1; ++i) {
if (x % primes[i] == 0) {
++count;
while (x % primes[i] == 0)
x /= primes[i];
if (count > 4)
return false;
}
}
return count == 4;
}