-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem012.cc
More file actions
59 lines (53 loc) · 1.29 KB
/
Copy pathproblem012.cc
File metadata and controls
59 lines (53 loc) · 1.29 KB
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
#include <iostream>
#include <vector>
#include <cstdint>
std::vector<std::uint64_t> gen_primes(const std::uint64_t &max);
std::uint64_t count_divisors(std::uint64_t number);
int main(int argc, char **argv) {
std::uint64_t current = 3;
std::uint64_t index = 3;
while (count_divisors(current) < 500)
current += index++;
std::cout << current << '\n';
return 0;
}
/**
* Uses the division function as described here:
* http://en.wikipedia.org/wiki/Divisor_function#Properties
*/
std::uint64_t count_divisors(std::uint64_t number) {
static std::vector<std::uint64_t> primes = gen_primes(30000);
int i = 0;
std::uint64_t res = 1;
while (number != 1) {
int exponent = 0;
while (number % primes[i] == 0) {
number /= primes[i];
++exponent;
}
++i;
res *= exponent + 1;
}
return res;
}
/**
* Uses the primes generator algorithm from TAOCP vol. 1
*/
std::vector<std::uint64_t> gen_primes(const std::uint64_t &max) {
unsigned n = 3;
std::vector<std::uint64_t> primes;
primes.push_back(2);
primes.push_back(n);
while (n <= max) {
n += 2;
for (unsigned k = 1; k < primes.size(); ++k) {
if (n % primes[k] == 0)
break;
if (n / primes[k] <= primes[k]) {
primes.push_back(n);
break;
}
}
}
return primes;
}