-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem44.cpp
51 lines (46 loc) · 1.11 KB
/
problem44.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
#include <iostream>
#include <vector>
#include <cmath>
#include <limits>
using namespace std;
using long_t = unsigned long long;
vector<long_t> pentagonals;
void generatePentagonals(int N)
{
for (long_t i = 0; i <= N;i++){
int p = i*(3*i-1)/2;
pentagonals.push_back(p);
}
}
bool isflint(double x)
{
if (ceil(x) == x)
return true;
return false;
}
bool isPentagonal(long_t n){
double x1 = (1+sqrt(1+24*n))/6;
double x2 = (1+sqrt(1-24*n))/6;
if ( (x1 >= 0 && isflint(x1)) || (x2 >=0 && isflint(x2))){
return true;
}
return false;
}
int main()
{
int N = 10000;
generatePentagonals(N);
long_t min_diff(std::numeric_limits<long_t>::max());
for (int i = 1; i<N;i++){
for (int j = i+1; j<N;j++){
auto sum = pentagonals[j] + pentagonals[i];
auto diff = pentagonals[j] - pentagonals[i];
if (isPentagonal(sum) && isPentagonal(diff)){
min_diff = diff;
cout<<pentagonals[i]<<endl;
cout<<pentagonals[j]<<endl;
}
}
}
cout<<min_diff<<endl;
}