Transforming the code to use recursion

Finding the first few hundred thousand primes is a trivial problem as many of the answers here show. If you are looking for a challenge with prime numbers try one of these:

  • write a program that finds the N-th prime number, where N is supplied by the user when invoking the procedure. For example (nth-prime 5) would return 11 since 11 is the fifth prime (assuming primes start at 2). The user will test this function by passing values greater than 10000000 (ten million) to nth-prime.
  • write a program that finds the first prime number (not a pseudo prime number) after a value X, where X is supplied by the user when invoking the procedure. For example (first-prime-after 5) would return 7, since 7 is the first prime greater than 5. The user will test this function by passing values greater than 18446744073709551616 to first-prime-after (i.e values larger than 64 bits).

Enjoy,
Alex.

1 Like

Natural number primes do start at 2.
The elements of a ring are classified as

* zero
* units (those elements with multiplicative inverses)
* primes (which can be factored into multiple factors only if all but one factor is a unit)
* composites

So the natural number 1 is a unit, not a prime. 2 is a prime.
In the integers, 1 and -1 are units, and -2 is a prime.
In the reals, every real numner except 0 is a unit.

-- hendrik