-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem44.C
50 lines (37 loc) · 942 Bytes
/
problem44.C
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
/* problem44.C */
// written by lina <lina.oahz@gmail.com>
// start: Sun Feb 24 22:06:26 SGT 2013
// end: Sun Feb 24 22:59:49 SGT 2013
#include <iostream>
#include <cmath>
#include <math.h>
using namespace std;
#define limit 100000000
int is_pentagonal(long p){
long sq;
sq = (long) sqrt(1+24*p);
if(sq*sq == 1+24*p && (1+sq)%6 == 0)
return 1;
return 0;
}
int main(){
long j, k;
long *pn;
pn = new long[limit/5];
int index = 0;
int min = limit;
for(j=1; j<=limit; j++)
if( is_pentagonal(j) )
pn[index++] = j;
for(j=0; j<index-1; j++){
for(k=j+1; k<index; k++){
if( pn[k] - pn[j] > min)
break;
if( is_pentagonal(pn[j] + pn[k]) && is_pentagonal(pn[k]-pn[j]))
min = min > pn[k]-pn[j] ? pn[k]-pn[j]: min;
}
}
cout << min << endl;
delete [] pn;
return 0;
}