-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem045.java
More file actions
33 lines (28 loc) · 853 Bytes
/
problem045.java
File metadata and controls
33 lines (28 loc) · 853 Bytes
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
package project_beuler;
public class problem045 {
// 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.
public static void main(String[] args) {
// TODO Auto-generated method stub
// a(a+1)/2
// b(3b-1)/2
// c(2c-1)
// a(a+1) = b(3b-1) = c(2c-1)
for(long a = 286; true; a++) {
long b; long c;
for(b = a; b*(3*b-1) > a*(a+1); b--);
if(a*(a+1) != b*(3*b-1)) continue;
for(c = b; c*(2*c-1) > b*(3*b-1)/2; c--);
if(b*(3*b-1)/2 == c*(2*c-1)) {
System.out.println(c*(2*c-1));
break;
}
}
}
}