-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathProblem_10.java
More file actions
40 lines (32 loc) · 801 Bytes
/
Problem_10.java
File metadata and controls
40 lines (32 loc) · 801 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
40
/*
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
Find the sum of all the primes below two million.
*/
class Problem_10 {
public static boolean isPrime(long n)
{
if (n < 2) {return false;}
else if (n == 2) {return true;}
for (int a = 3; a < Math.sqrt(n) + 1; a += 2)
{
if (n % a == 0)
{
return false;
}
}
return true;
}
public static void main(String[] args)
{
long s = 2;
int b;
for(b = 3; b < 2000000; b += 2)
{
if(isPrime(b)== true)
{
s = s + b;
}
}
System.out.println(s);
}
}