# Checking If a Positive Integer Is a Prime Number

### Problem Statement

An positive integer greater than or equal to 2 is a prime number if the only divisor of this integer is 1 and itself.

Write a program that reads in an arbitrary integer and determines if it is a prime number.

### Solution

```! --------------------------------------------------------------------
! Given an integer, this program determines if it is a prime number.
! This program first makes sure the input is 2.  In this case, it is
! a prime number.  Then, it checks to see the input is an even
! number.  If the input is odd, then this program divides the input
! with 3, 5, 7, ....., until one of two conditions is met:
!    (1)  if one these odd number evenly divides the input, the
!         input is not a prime number;
!    (2)  if the divisor is greater than the square toot of the
!         input, the input is a prime.
! --------------------------------------------------------------------

PROGRAM  Prime
IMPLICIT  NONE

INTEGER  :: Number                   ! the input number
INTEGER  :: Divisor                  ! the running divisor

IF (Number < 2) THEN                 ! not a prime if < 2
WRITE(*,*)  'Illegal input'
ELSE IF (Number == 2) THEN           ! is a prime if = 2
WRITE(*,*)  Number, ' is a prime'
ELSE IF (MOD(Number,2) == 0) THEN    ! not a prime if even
WRITE(*,*)  Number, ' is NOT a prime'
ELSE                                 ! we have an odd number here
Divisor = 3                       ! divisor starts with 3
DO                                ! divide the input number
IF (Divisor*Divisor > Number .OR. MOD(Number, Divisor) == 0)  EXIT
Divisor = Divisor + 2          ! increase to next odd
END DO
IF (Divisor*Divisor > Number) THEN     ! which condition fails?
WRITE(*,*)  Number, ' is a prime'
ELSE
WRITE(*,*)  Number, ' is NOT a prime'
END IF
END IF
END PROGRAM  Prime
```

### Program Input and Output

• If the input value is -1, the output is a message saying the input is not legal.
```Illegal input
```
• If the input is 2, it is a prime number.
```2 is a prime
```
• If the input is 3, it is also a prime number.
```3 is a prime
```
• If the input is 46, it is not a prime number since it is divisible by 2.
```46 is NOT a prime
```
• If the input is 97, it is a prime number.
```97 is a prime
```
• If the input is 9797, it is not a prime since it is divisible by 97.
```9797 is NOT a prime
```

### Discussion

• Since the input is an arbitrary integer, the program first makes sure its value is greater than or equal to 2; otherwise, a message is displayed.
• If the input is greater than or equal to 2, the program checks if it is actually equal to 2. If it is, just reports "2 is a prime".
• The next step is screening out all even numbers. Note that 2 has been checked before the control gets to the second ELSE-IF. If the input is divisible by 2, it is not a prime number.
• If the control can reach here, the input is an odd number greater than or equal to 3. Then, the program uses 3, 5, 7, 9, 11, ... these odd numbers as divisors to divide the input value stored in Number. These divisors are successively stored in Divisor.
• Of course, these divisors should start with 3; but, the question is when to stop. A naive answer would be "let us try up to Number-1" This is too slow since Number cannot be evenly divided by Number-1.

A better choice is the square root of Number? Why is this strange value? If Number is divisible by a, then we can write Number=a*b for some b. If a is less than or equal to b, then a must be smaller than or equal to the square root of Number.

Therefore, the upper limit of Divisor is the square root of Number. Stated in a slightly different way, it is "the square of Divisor is less than or equal to Number". This is better since it only uses integer arithmetic, while the one using square root involves REAL numbers.

• In the DO-loop, the value for Divisor starts with 3. As long as the square of Divisor is less than or equal to Number and Number is not divisible by Divisor, the iteration continues.

Since Divisor can only be odd numbers, step-size is 2.

This loop continues until one of the two conditions holds. If Divisor*Divisor > Number holds, then all odd numbers that are greater than or equal to 3 and less than or equal to the square root of Number have been tried and none of them can evenly divide Number. Therefore, Number is a prime number.

If MOD(Number,Divisor) == 0 holds, Divisor divides Number and Number is not a prime.

• Let us take a look at a few examples:
1. Let Number be 3. Divisor starts with 3. Since condition Divisor*Divisor > Number holds immediately, 3 is a prime number.
2. Let Number be 5. Divisor starts with 3. Since condition Divisor*Divisor > Number holds immediately, 5 is a prime number.
3. Let Number be 11. Divisor starts with 3. In the first iteration, both Divisor*Divisor > Number and MOD(Number,Divisor) == 0 fail. So, Divisor is increased by 2, becoming 5. In the second iteration, Divisor*Divisor > Number holds and 11 is a prime.
4. Let Number be 91. Divisor starts with 3. In the first iteration, both Divisor*Divisor > Number and MOD(Number,Divisor) == 0 fail. So, Divisor is increased by 2, becoming 5. In the second iteration, both conditions still fail and Divisor is increased to 7. In the third iteration, MOD(Number,Divisor) == 0 holds and 91 is not a prime.