-
Notifications
You must be signed in to change notification settings - Fork 0
/
TriangularPentagonalAndHexagonal.cpp
60 lines (49 loc) · 1.26 KB
/
TriangularPentagonalAndHexagonal.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
52
53
54
55
56
57
58
59
60
// This file is written by Alireza Kamyab 5/5/2021
// Answer to proble projecteuler.net/problem=45
#include <iostream>
#include <cmath>
#include <cstdint>
using namespace std;
bool isTriangular(uint64_t t);
bool isPentagonal(uint64_t p);
bool isHexagonal(uint64_t h);
uint64_t hexagonal(int n);
uint64_t triangular(int n);
uint64_t pentagonal(int n);
int main(){
for(int i = 144; true; i++){
uint64_t hex = hexagonal(i);
if(isPentagonal(hex) && isTriangular(hex)){
cout << hex << endl;
return 0;
}
}
return 0;
}
bool isTriangular(uint64_t t){
uint64_t delta = 1 + (8 * t);
float root = ((-1) + sqrt(delta)) / (2);
if((int)root == root) return true;
else return false;
}
bool isPentagonal(uint64_t p){
uint64_t delta = 1 + (24 * p);
double root = ((1) + sqrt(delta)) / (6);
if((int)root == root) return true;
else return false;
}
bool isHexagonal(uint64_t h){
uint64_t delta = 1 + (8 * h);
double root = ((1) + sqrt(delta)) / (4);
if((int)root == root) return true;
else return false;
}
uint64_t hexagonal(int n){
return n * ((2 * n) - 1);
}
uint64_t triangular(int n){
return (n * (n + 1)) / 2;
}
uint64_t pentagonal(int n){
return (n * ((3 * n) - 1)) / 2;
}