-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEuler3.java
37 lines (31 loc) · 851 Bytes
/
Euler3.java
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
package Euler;
import java.math.BigInteger;
import java.text.DecimalFormat;
import java.text.NumberFormat;
public class Euler3 {
public static boolean isPrime(long i){
for (int j = 2; j < i; j++){
if(i %j == 0){
return false;
}
}
return true;
}
public static void main(String[] args) {
long startTime = System.currentTimeMillis();
long limit = Long.parseLong("600851475143");
long lim = (int) Math.sqrt(limit);
for(long i = (lim/100) ; i >= 0; i--){
if(isPrime(i)){
if(limit%i == 0 ){
System.out.println(i);
break;
}
}
}
long endTime = System.currentTimeMillis();
NumberFormat formatter = new DecimalFormat("#0.00000");
System.out.println((endTime-startTime)+" ms");
System.out.print("\nExecution time is " + formatter.format((endTime - startTime) / 1000d) + " seconds\n");
}
}