Sheerpower Logo
R.4  The sieve of Eratosthenes
Calculates prime numbers less than an upper bound determined by the user. This program uses a well-known algorithm called "The sieve of Eratosthenes", developed by a Greek mathematician about 2200 years ago. Historical Note: The most famous discovery of this great ancient scientist is how to calculate the earth's circumference from simple astronomical observations.
!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% ! Program: PrimeNumbers.spsrc ! System : Prime Number Calculation ! Author : ChatGPT ! Date : June 21, 2024 ! Purpose: Calculate and display prime numbers up to a given upper bound. !%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% ! Initialization const upper_bound = 100_000 print 'This program returns the prime numbers' print 'less or equal than an upper bound determined by you.' start timer ! Perform Sieve of Eratosthenes do_sieve print 'Number of primes from zero to'; upper_bound; ': '; sieve; 'primes found.' print 'Elapsed time in seconds: '; _elapsed ! Display prime numbers display_primes stop !%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% ! R o u t i n e s !%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% !%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% ! Routine: do_sieve ! Purpose: Perform the Sieve of Eratosthenes to find prime numbers. !%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% routine do_sieve dim flags?(1 to upper_bound) ! Initialize array to assume all numbers are prime for i = 2 to upper_bound flags?(i) = true next i ! Sieve process up_to_here = sqr(upper_bound) for i = 2 to up_to_here if flags?(i) = false then iterate for for j = i to (upper_bound / i) flags?(i * j) = false next j sieve = sieve + 1 next i ! Count remaining primes for i = up_to_here + 1 to upper_bound if flags?(i) = true then sieve = sieve + 1 next i end routine !%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% ! Routine: display_primes ! Purpose: Display the prime numbers found by the sieve. !%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% routine display_primes for i = 1 to upper_bound if flags?(i) = true then print i, next i print end routine end

Program Explanation:

1. Program Header:

The program begins with a header comment block that provides basic information about the program, including its name, purpose, author, and date. These comments help clarify what the program is designed to do.

2. Constant Declaration:

The constant upper_bound is set to 100,000, which means the program will calculate prime numbers up to this value.

3. User Instructions:

These print statements provide information to the user about what the program does.

4. Timer Start:

Starts a timer to measure how long it takes to perform the prime number calculation.

5. Sieve of Eratosthenes Routine Call:

The program calls the do_sieve routine, which performs the actual calculation of prime numbers using the Sieve of Eratosthenes algorithm.

6. Print Number of Primes:

After the sieve has been executed, this statement prints the total number of prime numbers found.

7. Print Elapsed Time:

Prints the time taken to complete the calculation.

8. Display Primes Routine Call:

Calls the display_primes routine, which outputs the list of prime numbers found.

9. End Program:

Stops the program execution.

10. Routines Section:

A comment block indicating the start of the routines section of the program.

11. Routine Definition (do_sieve):

The do_sieve routine is defined. This routine will execute the Sieve of Eratosthenes algorithm to identify prime numbers.

12. Array Declaration:

An array flags? is declared with a size from 1 to upper_bound. This array will be used to mark numbers as prime or not prime.

13. Initialize Array:

This loop initializes the flags? array, setting all values from 2 to upper_bound to true, which means they are initially assumed to be prime.

14. Sieve Process Start:

The variable up_to_here is set to the square root of upper_bound. The outer loop starts at 2 and runs up to this square root value. This is because any non-prime number greater than the square root must have a factor less than or equal to the square root.

15. Skip Non-Primes:

If the current number i is already marked as non-prime (flags?(i) = false), the loop skips to the next iteration.

16. Mark Non-Primes:

For each prime i, the inner loop marks all multiples of i (i.e., i * j) as non-prime by setting flags?(i * j) to false.

17. Count Primes:

Each time a prime number is found (i.e., flags?(i) is true), the sieve variable, which counts the total number of primes, is incremented.

18. Count Remaining Primes:

After completing the sieve process up to the square root of upper_bound, the program counts any remaining prime numbers up to upper_bound.

19. End of Routine (do_sieve):

Marks the end of the do_sieve routine.

20. Routine Definition (display_primes):

The display_primes routine is defined. This routine will print all the prime numbers that were found by the sieve.

21. Display Prime Numbers:

This loop iterates through all numbers from 1 to upper_bound. If flags?(i) is true (indicating that i is a prime number), it prints the number i.

22. End of Routine (display_primes):

Marks the end of the display_primes routine. The final print statement outputs a new line after all the primes have been printed.

23. Program End:

The end statement marks the end of the program.

Summary: This program calculates and displays prime numbers up to 100,000 using the Sieve of Eratosthenes algorithm. It efficiently identifies prime numbers by marking non-prime multiples.
Hide Description

    

       


      

Enter or modify the code below, and then click on RUN

Looking for the full power of Sheerpower?
Check out the Sheerpower website. Free to download. Free to use.
Wide screen