Euler Problem 45

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

Triangle   T_(n)=n(n+1)/2   1, 3, 6, 10, 15, …
Pentagonal   P_(n)=n(3n−1)/2   1, 5, 12, 22, 35, …
Hexagonal   H_(n)=n(2n−1)   1, 6, 15, 28, 45, …

It can be verified that T_(285) = P_(165) = H_(143) = 40755.

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

Flip each formula using the quadratic equation. Double check your calculations. Iterate over a few billion. Done.

public class Euler45
{
    public static void main(String[] args)
    {
        Euler45 e = new Euler45();
        System.out.format(“Problem: %s\n”, e.getClass().getName());
        System.out.format(“Answer = %s\n”, e.Problem());
    }
    public String Problem ()
    {
        long next = 0;
        for (long i = 1000000000L; i <= 2000000000L; i++)
            if ((Math.sqrt(1+8*i)-1) % 2 == 0)
                if ((Math.sqrt(1+24*i)+1) % 6 == 0)
                    if ((Math.sqrt(1+8*i)+1) % 4 == 0)
                    {
                        next = i;
                        break;
                    }
        return String.valueOf(next);
    }
}



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s