
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 nonprime number greater than the square root must have a factor less than or equal to the square root.
15. Skip NonPrimes:
If the current number i
is already marked as nonprime (flags?(i) = false
), the loop skips to the next iteration.
16. Mark NonPrimes:
For each prime i
, the inner loop marks all multiples of i
(i.e., i * j
) as nonprime 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 nonprime 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. 