forked from nayuki/Project-Euler-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp012.java
More file actions
39 lines (31 loc) · 753 Bytes
/
p012.java
File metadata and controls
39 lines (31 loc) · 753 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
34
35
36
37
38
39
/*
* Solution to Project Euler problem 12
* By Nayuki Minase
*
* http://nayuki.eigenstate.org/page/project-euler-solutions
* https://github.com/nayuki/Project-Euler-solutions
*/
public final class p012 implements EulerSolution {
public static void main(String[] args) {
System.out.println(new p012().run());
}
public String run() {
int num = 0;
for (int i = 1; ; i++) {
num += i; // num is triangle number i
if (countDivisors(num) > 500)
return Integer.toString(num);
}
}
private static int countDivisors(int n) {
int count = 0;
int end = Library.sqrt(n);
for (int i = 1; i < end; i++) {
if (n % i == 0)
count += 2;
}
if (end * end == n) // Perfect square
count++;
return count;
}
}