-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
66 lines (53 loc) · 1.41 KB
/
main.c
File metadata and controls
66 lines (53 loc) · 1.41 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
58
59
60
61
62
63
64
65
66
#include<math.h>
#include <stdbool.h>
#include <stdio.h>
#include <threads.h>
#include <time.h>
typedef unsigned long long int i64;
i64 P(i64 n) {
return (n * (3*n -1))/2;
}
// 3n^2 - n - 2Pn = 0 => discriminant = 1 + 4 * 3 * 2Pn
bool is_pentagon(i64 Pn) {
i64 discriminant = 1 + 24 * Pn;
double d_root = sqrt((double)discriminant);
i64 greater_root = ((1 + d_root) / 6);
return (greater_root>0) && P(greater_root) == Pn;
}
int main(int argc, char* argv[]){
clock_t start = clock();
i64 n = 1;
while(1){
i64 D = P(n);
i64 d = 0;
i64 i = 1;
i64 k = i;
bool found_answer = false;
while(1){
if(P(k)-P(i)==D && is_pentagon(P(k)+P(i))){
printf("P%lld=%lld, P%lld=%lld, D=%lld sum=%lld ", i, P(i), k, P(k), D, P(k)+P(i));
found_answer = true;
break;
}
if (P(k)-P(i)>=D) {
if (d==1){
found_answer = false;
break;
}
i++;
k = i;
d = 0;
} else {
d++;
k = i + d;
}
}
if(found_answer){
break;
}
n++;
}
clock_t end = clock();
double elapsed = (double)(end - start)/CLOCKS_PER_SEC;
printf("(elapsed=%fs)\n", elapsed);
}