Time (s): ~0.005
package margusmartseppcode.From_40_to_49;
public class Problem_45 {
public static void main(String[] args) {
int tri, pen = 40755, hex = 40755, iTri = 286, iPen = 165, iHex = 143;
for (tri = 41041; tri != pen || tri != hex; tri += ++iTri) {
if (tri > pen) pen += (iPen++ * 3) + 1;
if (tri > hex) hex += (iHex++ * 4) + 1;
}
System.out.println(tri);
}
}
Time (s): ~0.006
package margusmartseppcode.From_40_to_49;
public class Problem_45 {
static boolean isPentagonal(long n) {
return ((Math.sqrt(1 + 24 * n) + 1) / 6) % 1 == 0;
}
public static void main(String[] args) {
long hex = 0;
for (int n = 165; !isPentagonal(hex); n++)
hex = n * (2 * n - 1);
System.out.println(hex);
}
}
No comments:
Post a Comment