Problem 45 of Project Euler required one to find numbers which are triangular, pentagonal and hexagonal together. Checking for triangularity is redundancy as all hexagonal numbers are also triangular. So it boils down to solving the equation:
n(3n – 1) / 2 = m(2m – 1)
Using the Diophantine Equation solver at http://www.alpertron.com.ar/QUAD.HTM, we get the solution as
The solution comes very easily from here.
public class Problem_45 {
/**
* Find a number which is Pentagonal and also Hexagonal
*
* @param index The nth number which is required
* @return The nth number which is both Pentagonal and Hexagonal
*/
public static int findPentaHexaNumber(int index) {
int p = 1;
int h = 1;
do {
// This is the solution to the Diophantine Equation
// p(3p-1)/2 = h(2h-1)
int p2 = 97 * p + 112 * h - 44;
h = 84 * p + 97 * h - 38;
p = p2;
index--;
} while (index != 0);
return h * (2 * h - 1);
}
}
Popularity: 9% [?]
Related posts:
