-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEuler_7
50 lines (37 loc) · 946 Bytes
/
Euler_7
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
40
41
42
43
44
45
46
47
48
49
50
/**
By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.
What is the 10 001st prime number?
*/
public class EulerSeven
{
public static void main ( String[] args )
{
long goal = 1000000000L;
int count = 4; //because isPrime doesn't pick up 2,3,5, or 7 start count at 4
for(int j=8; j<goal; j++){ //j starts at 8 b/c isPrime doesn't detect 7
if(isPrime(j)) {
count++;
if(count == 10001) {
System.out.println(j);
break;
}
}
}
}
public static boolean isPrime ( int num )
{
boolean prime = true;
int limit = (int) Math.sqrt ( num );
//makes prime detection faster but not able to detect first 4 primes (2,3,5,7)
if(num%2==0 || num%3==0 || num%5==0 || num%7==0) prime = false;
for ( int i = 3; i <= limit; i+=2 )
{
if ( num % i == 0 )
{
prime = false;
break;
}
}
return prime;
}
}