-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem012.java
More file actions
30 lines (30 loc) · 896 Bytes
/
Problem012.java
File metadata and controls
30 lines (30 loc) · 896 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
public class Problem12 {
public static void main(String[] args) {
int i = 1;
int topDivisors = 0;
int topTriangle = 1;
while(topDivisors < 501) {
int triangle = 0;
int divisors = 0;
for (int j = 1; j <= i; j++) {
triangle += j;
}
for (int div = 1; div < Math.sqrt(triangle); div++) {
if (triangle % div == 0) {
divisors++;
}
}
divisors *= 2;
if (Math.sqrt(triangle) % 1 == 0) {
divisors++;
}
if (divisors > topDivisors) {
topDivisors = divisors;
topTriangle = triangle;
System.out.println(triangle + ": " + divisors);
}
i++;
}
System.out.println(topTriangle);
}
}