Define a function that when passed an integer (n), returns the number of primes in the range [1, n]. For example,
if n = 5, return 3, since the numbers 2, 3 and 5 are primes.
if n = -5, return 0.
if n = 10, return 4, since the numbers 2, 3, 5, and 7 are primes. Question need to be using processing.
boolean isPrime(int n) {
if(n < 2) {
return false;
}
for(int i=2; i*i<=n; i++) {
if(n%i==0) {
return false;
}
}
return true;
}
int countPrimes(int n) {
if(n <= 2){
return 0;
} else if(n == 3){
return 1;
}
int count = 0;
for(int i = 2; i <= n; i++){
if(IsPrime( i )){
count++;
}
}
return count;
}
boolean IsPrime(int num) {
for(int i=2;i<=num/2;i++){
if(num % i == 0){
return false;
}
}
return true;
}
}