-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEuler045.java
More file actions
executable file
·46 lines (39 loc) · 1.2 KB
/
Copy pathEuler045.java
File metadata and controls
executable file
·46 lines (39 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import java.util.Date;
/*
Project 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.
*/
public class Euler045 {
public static void main(String[] args) {
Date start, end;
start = new Date();
for(int i = 144;;i++){
int l = hexagonal(i);
if(isPentagonal(l) && isTriangle(l)){
System.out.println(l);
break;
}
}
end = new Date();
System.out.println("Execution Time: " + (end.getTime() -start.getTime()));
}
public static boolean isPentagonal(double p){
return isInteger((1+ Math.sqrt(1+24*p))/6) ;
}
public static boolean isTriangle(double p){
return isInteger((1+Math.sqrt(1+8*p))/2);
}
public static boolean isInteger(double p){
return p == Math.floor(p);
}
public static int hexagonal(int n) {
return n * (2 * n - 1);
}
}