Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
60 lines (56 loc) · 1.19 KB

File metadata and controls

60 lines (56 loc) · 1.19 KB
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;
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.