Finding All Prime Numbers in the Range of 2 and N

Problem Statement

In a previous example, we have discussed how to determine if a positive integer is a prime number. In this one, we shall find all prime numbers in the range of 2 and N, where N is an input integer.

Write a program to read a value of N, make sure that the value of N is greater than or equal to 2, and display all prime numbers in the range of 2 and N. In case the value of N is less than 2, your program should keep asking the user to try again until a value that is greater than or equal to 2 is read.

Solution

! ---------------------------------------------------------------
! This program finds all prime numbers in the range of 2 and an
! input integer.
! ---------------------------------------------------------------

PROGRAM  Primes
IMPLICIT  NONE

INTEGER  :: Range, Number, Divisor, Count

WRITE(*,*)  'What is the range ? '
DO                                   ! keep trying to read a good input
IF (Range >= 2)  EXIT             ! if it is GOOD, exit
WRITE(*,*)  'The range value must be >= 2.  Your input = ', Range
WRITE(*,*)  'Please try again:'   ! otherwise, bug the user
END DO

Count = 1                            ! input is correct. start counting
WRITE(*,*)                           ! since 2 is a prime
WRITE(*,*)  'Prime number #', Count, ': ', 2
DO Number = 3, Range, 2              ! try all odd numbers 3, 5, 7, ...

Divisor = 3                       ! divisor starts with 3
DO
IF (Divisor*Divisor > Number .OR. MOD(Number,Divisor) == 0)  EXIT
Divisor = Divisor + 2          ! if does not evenly divide, next odd
END DO

IF (Divisor*Divisor > Number) THEN     ! are all divisor exhausted?
Count = Count + 1              ! yes, this Number is a prime
WRITE(*,*)  'Prime number #', Count, ': ', Number
END IF
END DO

WRITE(*,*)
WRITE(*,*)  'There are ', Count, ' primes in the range of 2 and ', Range

END PROGRAM  Primes

Program Input and Output

• The following shows an interaction between the user and program. First, the users type in -10, which is less than 2. This program displays the input value and a message asking the user to try again. The user then types in 0, which is still less than 2, causing the same message to occur. The users types in 1 and the same message appears. Finally, after the user types in 5, the program reports that there are three prime numbers in the range of 2 and 5, namely: 2, 3, and 5.
What is the range ?
-10
The range value must be >= 2.  Your input = -10
0
The range value must be >= 2.  Your input = 0
1
The range value must be >= 2.  Your input = 1
5

Prime number #1: 2
Prime number #2: 3
Prime number #3: 5

There are 3 primes in the range of 2 and 5

• The following is generated with input 100. There are 25 prime numbers in the range of 2 and 100.
What is the range ?
100

Prime number #1: 2
Prime number #2: 3
Prime number #3: 5
Prime number #4: 7
Prime number #5: 11
Prime number #6: 13
Prime number #7: 17
Prime number #8: 19
Prime number #9: 23
Prime number #10: 29
Prime number #11: 31
Prime number #12: 37
Prime number #13: 41
Prime number #14: 43
Prime number #15: 47
Prime number #16: 53
Prime number #17: 59
Prime number #18: 61
Prime number #19: 67
Prime number #20: 71
Prime number #21: 73
Prime number #22: 79
Prime number #23: 83
Prime number #24: 89
Prime number #25: 97

There are 25 primes in the range of 2 and 100

Discussion

• We shall use part of the program shown in a previous example for checking if an integer is a prime number. Please refer to that example for the details.
• How do we write a bullet-proof program so that the values for N and Range in the program are always correct? Here is the way our program uses:
WRITE(*,*)  'What is the range ? '
DO