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.
Click here to download this program.! --------------------------------------------------------------- ! 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 READ(*,*) Range ! ask for an input integer 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
What is the range ? -10 The range value must be >= 2. Your input = -10 Please try again: 0 The range value must be >= 2. Your input = 0 Please try again: 1 The range value must be >= 2. Your input = 1 Please try again: 5 Prime number #1: 2 Prime number #2: 3 Prime number #3: 5 There are 3 primes in the range of 2 and 5
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
It first asks for a number. The actual READ is in the DO-loop. After reading in a value for Range, this value is checked to see if it is greater than or equal to 2. If it is, EXIT and find prime numbers, since we have read in a good input. Otherwise, the input is incorrect and the program shows a message and loops back to read a new one.WRITE(*,*) 'What is the range ? ' DO READ(*,*) Range IF (Range >= 2) EXIT ... incorrect input here ... END DO
DO Number = 3, Range, 2 ... determine if Number is a prime number ... ... if Number is a prime number, display it ... END DO