Triangular, pentagonal, and hexagonal | Project Euler | Problem #45

URL to the problem page: https://projecteuler.net/problem=45

Triangle, pentagonal, and hexagonal numbers are generated by the following formulae:

Triangle T=n(n+1)/2     1, 3, 6, 10, 15, ...
Pentagonal P=n(3n−1)/2     1, 5, 12, 22, 35, ...
Hexagonal H=n(2n−1)     1, 6, 15, 28, 45, ...

It can be verified that T₂₈₅ = P₁₆₅ = H₁₄₃ = 40755.

Find the next triangle number that is also pentagonal and hexagonal.



#include <iostream>
using namespace std;

int main()
{
    long long i, j, a, result = 0, triangular, pentagonal, hexagonal;

    for (i = 286; result <= 0; i++) {
        triangular = (i * (i + 1)) / 2;
        pentagonal = 0;
        for (j = 166; pentagonal <= triangular; j++) {
            pentagonal = (j * ((3 * j) - 1)) / 2;
            if (pentagonal == triangular) {
                hexagonal = 0;
                for (a = 144; hexagonal <= triangular; a++) {
                    hexagonal = a * ((2 * a) - 1);
                    if (hexagonal == pentagonal) {
                        cout << "First triangle number that is also pentagonal and hexagonal after 40755 is  =  " << triangular << endl;
                        result++;
                        break;
                    }
                }
            }
            if (result > 0) {
                break;
            }
        }
    }
    return 0;
}



Comments

My photo
Ercan Tomac
instagram.com/ercantomac