FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Java/ProjectEuler/Problem10.java at master · ppdouble/Java · GitHub
ppdouble
/
Java
Public
forked from
TheAlgorithms/Java
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
Java
/
ProjectEuler
/
Problem10.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
55 lines (51 loc) · 1.11 KB
Breadcrumbs
Java
/
ProjectEuler
/
Problem10.java
Copy path
File metadata and controls
55 lines (51 loc) · 1.11 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
51
52
53
54
55
package
ProjectEuler
;
/**
* The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
*
* <p>Find the sum of all the primes below two million.
*
* <p>link: https://projecteuler.net/problem=10
*/
public
class
Problem10
{
public
static
void
main
(
String
[]
args
) {
long
[][]
testNumbers
= {
{
2000000
,
142913828922L
},
{
10000
,
5736396
},
{
5000
,
1548136
},
{
1000
,
76127
},
{
10
,
17
},
{
7
,
10
}
};
for
(
long
[]
testNumber
:
testNumbers
) {
assert
solution1
(
testNumber
[
0
]) ==
testNumber
[
1
];
}
}
/***
* Checks if a number is prime or not
* @param n the number
* @return {@code true} if {@code n} is prime
*/
private
static
boolean
isPrime
(
int
n
) {
if
(
n
==
2
) {
return
true
;
}
if
(
n
<
2
||
n
%
2
==
0
) {
return
false
;
}
for
(
int
i
=
3
,
limit
= (
int
)
Math
.
sqrt
(
n
);
i
<=
limit
;
i
+=
2
) {
if
(
n
%
i
==
0
) {
return
false
;
}
}
return
true
;
}
private
static
long
solution1
(
long
n
) {
long
sum
=
0
;
for
(
int
i
=
2
;
i
<
n
; ++
i
) {
if
(
isPrime
(
i
)) {
sum
+=
i
;
}
}
return
sum
;
}
}
Back
|
FazBrowse Home
|
New Git URL