Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Define a function that returns the number of prime in range in processing language

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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;
    }



>Solution :

    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;
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading