-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem12.java
More file actions
35 lines (28 loc) · 768 Bytes
/
Copy pathProblem12.java
File metadata and controls
35 lines (28 loc) · 768 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
package com.prog.ProjectEulerPrograms;
public class Problem12 {
public static void main(String[] args) {
int number = 0, i = 1;
// log start time
long start = System.currentTimeMillis();
while(numberOfDivisor(number) < 500) {
number += i;
i++;
}
// log end time
long end = System.currentTimeMillis();
System.out.println(number+" is the answer!");
System.out.println("Execution time in milliseconds: "+(end - start)+" ms");
}
private static int numberOfDivisor(int number) {
int sqrt = (int) Math.sqrt(number);
int count = 0;
for(int i = 1; i<=sqrt; i++) {
if(number % i == 0)
count += 2;
}
// check for perfect square
if(sqrt*sqrt == number)
count--;
return count;
}
}