FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Java/ProjectEuler/Problem07.java at master · oribach/Java · GitHub
oribach
/
Java
Public
forked from
TheAlgorithms/Java
Notifications
You must be signed in to change notification settings
Fork
1
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
/
Problem07.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
60 lines (56 loc) · 1.19 KB
Breadcrumbs
Java
/
ProjectEuler
/
Problem07.java
Copy path
File metadata and controls
60 lines (56 loc) · 1.19 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
56
57
58
59
60
package
ProjectEuler
;
/**
* By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is
* 13.
*
* <p>What is the 10 001st prime number?
*
* <p>link: https://projecteuler.net/problem=7
*/
public
class
Problem07
{
public
static
void
main
(
String
[]
args
) {
int
[][]
testNumbers
= {
{
1
,
2
},
{
2
,
3
},
{
3
,
5
},
{
4
,
7
},
{
5
,
11
},
{
6
,
13
},
{
20
,
71
},
{
50
,
229
},
{
100
,
541
}
};
for
(
int
[]
number
:
testNumbers
) {
assert
solution1
(
number
[
0
]) ==
number
[
1
];
}
}
/***
* Checks if a number is prime or not
* @param number the number
* @return {@code true} if {@code number} is prime
*/
private
static
boolean
isPrime
(
int
number
) {
if
(
number
==
2
) {
return
true
;
}
if
(
number
<
2
||
number
%
2
==
0
) {
return
false
;
}
for
(
int
i
=
3
,
limit
= (
int
)
Math
.
sqrt
(
number
);
i
<=
limit
;
i
+=
2
) {
if
(
number
%
i
==
0
) {
return
false
;
}
}
return
true
;
}
private
static
int
solution1
(
int
n
) {
int
count
=
0
;
int
number
=
1
;
while
(
count
!=
n
) {
if
(
isPrime
(++
number
)) {
count
++;
}
}
return
number
;
}
}
Back
|
FazBrowse Home
|
New Git URL