public static long Problem45() { /* * Triangle, pentagonal, and hexagonal numbers are generated by the following formulae: * * Triangle Tn=n(n+1)/2 1, 3, 6, 10, 15, ... * Pentagonal Pn=n(3n−1)/2 1, 5, 12, 22, 35, ... * Hexagonal Hn=n(2n−1) 1, 6, 15, 28, 45, ... * It can be verified that T285 = P165 = H143 = 40755. * * Find the next triangle number that is also pentagonal and hexagonal. */ long n = 286L; long l = 0L; do { l = (n * (n + 1)) / 2; n = n + 1; } while (!Formulas.isTriangular(l) || !Formulas.isPentagonal(l) || !Formulas.isHexagonal(l)); return(l); }
public static long Problem44() { int result = 0; bool notFound = true; int i = 1; while (notFound) { i++; int n = i * (3 * i - 1) / 2; for (int j = i - 1; j > 0; j--) { int m = j * (3 * j - 1) / 2; if (Formulas.isPentagonal(n - m) && Formulas.isPentagonal(n + m)) { result = n - m; notFound = false; break; } } } return(result); }