Suppose that a function isPrime(n) is available to you, which yields true if n is a prime number. (An integer n is prime if n > 1 and n is only divisible by itself and 1.) The first seven prime numbers are 2, 3, 5, 7, 11, 13 and 17.

Write a definition of function numPrimes(n) that produces a count of the number of prime integers that are less than or equal to n. Use recursion. (For this problem, function isPrime(n) is provided for you.)

Hint.

  1. What is numPrimes(n) when n < 2?

  2. When n is prime, numPrimes(n) is one larger than numPrimes(n-1). For example, numPrimes(4) = 2 and numPrimes(5) = 3, since numPrimes(5) counts one more prime number, 5.

  3. When n is not prime, numPrimes(n) is the same as numPrimes(n-1). For example, numPrimes(7) = 4 and numPrimes(8) = 4.

 

    [Language: Cinnameg  Kind: function definition*]

(*You can define more than one function if you put a semicolon between definitions (but not between cases of one definition). Be sure to define a function before you use it.)