-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprimeNumbers.java
More file actions
38 lines (33 loc) · 1.11 KB
/
primeNumbers.java
File metadata and controls
38 lines (33 loc) · 1.11 KB
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
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
/**
* Created by pranikchainani on 8/20/16.
*/
public class primeNumbers {
public static List<Integer> listOfPrimes(int maxNumber) {
// for (int i = 2; i <= maxNumber; i++)
// {
// if (isPrime(i))
// {
// primes.add(i);
// }
// }
return IntStream.rangeClosed(2, maxNumber)
.boxed()
.filter(number -> isPrime(number))
.collect(Collectors.toList());
}
public static boolean isPrime(int number)
{
return IntStream.rangeClosed(2, (int) Math.floor(Math.sqrt(number)))
.noneMatch(element -> number % element == 0);
// we also have functions like allMatch and anyMatch
// First we select a range of numbers between 2 and sqrt(number)
// We acheck if the number is divisible by the range taken
}
public static void main(String[] args) {
System.out.println(listOfPrimes(50));
}
}