-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp044.cpp
46 lines (35 loc) · 778 Bytes
/
p044.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
/*
Pentagon numbers
https://projecteuler.net/problem=44
*/
#include <iostream>
#include <vector>
const size_t L = 3000;
inline size_t Pn(size_t n)
{
return n * (3 * n - 1) / 2;
}
int main()
{
std::vector<bool> is_pentagonal(Pn(2 * L) + 1);
for (size_t n = 1; n < 2 * L; ++n)
{
is_pentagonal[Pn(n)] = true;
}
for (size_t j = 1; j < L; ++j)
{
size_t Pj = Pn(j);
for (size_t k = j + 1; k < L; ++k)
{
size_t Pk = Pn(k);
size_t diff = Pk - Pj;
size_t sum = Pk + Pj;
if (is_pentagonal[diff] && is_pentagonal[sum])
{
printf("%zu\n", diff);
//printf("%zd %zd D=%zd\n", j, k, d);
}
}
}
return 0;
}