Cant find the fix for my program at LeetCode Challenge of PrimeNumbers

Viewed 179

I have tried several ways changing my code in LeetCode but I can't find the fix, the challenge is the next one :

<<Count the number of prime numbers less than a non-negative number, n.

Example:

Input: 10 Output: 4 Explanation: There are 4 prime numbers less than 10, they are 2, 3, 5, 7.>>

My proposal code is the next one:

 import java.util.Scanner;
    class Solution {
        public int countPrimes(int n) {
        Scanner sc = new Scanner(System.in);
        int sum = 0;
        int cont = 0;
        int prime = 0;
        prime = sc.nextInt();
        int a[] = new int [prime];
        for(int i = 0; i < a.length; i++) {
            a[i] = i;
            cont = 0;
            
            for(int y = 1; y< a.length; y++) {
                if(a[i] % y == 0) {
                    cont ++;
                }
            }
            if (cont == 2) {
                sum ++;
            }
        }
        return sum;
    }
}

Meanwhile the error marks as follows:

Submission Result: Compile Error More Details 
Line 7: error: cannot find symbol [in __Driver__.java] int ret = new Solution().countPrimes(param_1); ^ symbol: method countPrimes(int) location: class Solution
Run Code Status: Runtime Error
×
Run Code Result:
Your input
10
Your answer
java.util.NoSuchElementException
  at line 937, java.base/java.util.Scanner.throwFor
  at line 1594, java.base/java.util.Scanner.next
  at line 2258, java.base/java.util.Scanner.nextInt
  at line 2212, java.base/java.util.Scanner.nextInt
  at line 8, Solution.countPrimes
  at line 54, __DriverSolution__.__helper__
  at line 84, __Driver__.main
Show Diff
Runtime: N/A

Please help!

3 Answers

This'll also pass through:

public class Solution {
    public static final int countPrimes(int n) {
        // mapping for if the number is divisible by prime numbers, which would make that number a composite number
        boolean[] notPrime = new boolean[n];
        // counting prime numbers
        int count = 0;

        for (int i = 2; i < n; i++) {
            // If the index of notPrime would be false, we have a prime number, we go through the if, otherwise we continue
            if (notPrime[i] == false) {
                // Increment the number of prime numbers
                count++;

                // Look into future numbers
                for (int j = 2; i * j < n; j++) {
                    // find composite numbers and set their indices to true
                    notPrime[i * j] = true;
                }
            }
        }

        return count;
    }
}

References

You got confused with the input/output part: you don't need any scanner to do this, just:

 class Solution 
 {
      public static int countPrimes(int n) 
       {
          int sum = 0;
           int a[] = new int [n];
           for(int i = 0; i < a.length; i++) {
                a[i] = i;
                int  cont = 0;
            
                for(int y = 1; y< a.length; y++) {
                    if(a[i] % y == 0) {
                       cont++;
                     }
                 }
          if (cont == 2) {
              sum++;
          }
        }
       return sum; //this is the output
    }
        
     public static void main(String args[])
    ´{
        countPrimes(10); //this is the input
     }
}

Proof:

enter image description here


Et voilá. LeetCode accepts the input (10) and the output (4). That's all you need :)

Your answer gets a scanner object which is not needed therefore you could remove it.

You also create an array which will decrease performance, which I recommend not using as you do not need to display the prime numbers but only keep track of them.

public static int countPrimes(int n) {

    int sum=0;

    for(int i = n; i > 1; i-- ){
        
        int count = 0; //Keep track of the number of primes.
        
        for(int j = 2; j < i; j++){
            if(i % j == 0){
                count++;
            }
        }
        
        if(count==0) {
            sum++;
        }
      
    }

    return sum;

    }
Related