Count Primes

To solve this coding challenge, the task is to count the number of prime numbers less than a given integer
n
. A prime number is an integer greater than 1 that has no other integer divisors other than 1 and itself.

Explanation

The Sieve of Eratosthenes is an efficient algorithm to find all prime numbers up to a specified integer. It works by iteratively marking the multiples of each prime number starting from 2. Here’s a step-by-step detailed explanation for solving this problem using the Sieve of Eratosthenes method:
  1. Initialization:
    • Create a list
      is_prime
      of boolean values. The index of this list represents the number, and the boolean value at that index indicates whether it is prime (
      True
      ) or not (
      False
      ). Initialize the list with
      True
      values, but specifically mark the indices 0 and 1 as
      False
      since 0 and 1 are not prime numbers.
  2. Iterate and Mark Non-Primes:
    • Start iterating from the first prime number, which is 2. For each prime number, mark all of its multiples as
      False
      , as these multiples cannot be prime.
  3. Continue the Process:
    • Continue this process up to the square root of
      n
      because a larger factor of a number will necessarily be a multiple of a smaller factor, which would have been marked earlier.
  4. Count the Primes:
    • The remaining
      True
      values in the list
      is_prime
      indicate prime numbers. The count of
      True
      values in the list up to
      n
      will give the number of primes less than
      n
      .
    Let’s break this down into an extensive and detailed pseudocode.

    Detailed Steps in Pseudocode

  5. Define Function and Edge Case Check:
    • If
      n
      is less than 2, return 0 as there are no prime numbers less than 2.
                                                
    function countPrimes(n):
    # Check if n is less than 2
    if n < 2:
    return 0
    
                                            
  6. Create a Boolean List and Initialize:
    • Create a list called
      is_prime
      with
      n
      entries all set to
      True
      .
    • Set
      is_prime[0]
      and
      is_prime[1]
      to
      False
      .
                                                
    # Initialize is_prime list with True values
    is_prime = [True] * n
    # Set the indices 0 and 1 to False, since 0 and 1 are not prime
    is_prime[0] = False
    is_prime[1] = False
    
                                            
  7. Iterate and Mark Non-Prime Numbers:
    • Iterate over each number starting from
      2
      to the integer square root of
      n
      .
    • For a number at index
      i
      which is still marked as
      True
      , mark all its multiples as
      False
      .
                                                
    # Iterate from 2 to sqrt(n)
    for i from 2 to sqrt(n) do:
    # If i is still considered prime
    if is_prime[i]:
    # Mark all multiples of i as False (non-prime)
    for multiple in range(i * i, n, i):
    is_prime[multiple] = False
    
                                            
  8. Count the Prime Numbers:
    • Sum the
      True
      values in
      is_prime
      list to get the count of prime numbers less than
      n
      .
                                            
    # Count the number of True values in is_prime
    prime_count = sum(is_prime)
    return prime_count

                                        
This will effectively give us the number of prime numbers less than
n
using the Sieve of Eratosthenes algorithm. The pseudocode encompasses all necessary steps while iterating and marking non-prime numbers efficiently.