Académique Documents
Professionnel Documents
Culture Documents
Integer
Problem Statement
As we have learned in high school, any positive integer can be factorized into prime factors. For
example, 586390350 can be factorized as follows:
Thus, 586390350 has factors 2, 3, 5, 5,, 7, 7, 13, 17, 19 and 19. Note that all factors are prime
numbers.
Write a program that reads in an integer greater than or equal to 2 and finds all of its prime
factors.
This problem is a little more difficult than the others and may require longer time to
understand its logic.
Solution
!
!
!
!
!
!
!
PROGRAM Factorize
IMPLICIT NONE
INTEGER
INTEGER
INTEGER
:: Input
:: Divisor
:: Count
WRITE(*,*)
READ(*,*)
Count = 0
DO
! here, we try to remove all factors of 2
IF (MOD(Input,2) /= 0 .OR. Input == 1) EXIT
Count = Count + 1
! increase count
WRITE(*,*) 'Factor # ', Count, ': ', 2
Input = Input / 2
! remove this factor from Input
END DO
Divisor = 3
! now we only worry about odd factors
DO
! 3, 5, 7, .... will be tried
IF (Divisor > Input) EXIT
! if a factor is too large, exit and done
DO
! try this factor repeatedly
IF (MOD(Input,Divisor) /= 0 .OR. Input == 1) EXIT
Count = Count + 1
WRITE(*,*) 'Factor # ', Count, ': ', Divisor
Input = Input / Divisor
! remove this factor from Input
END DO
Divisor = Divisor + 2
! move to next odd number
END DO
END PROGRAM
Factorize
#
#
#
#
1:
2:
3:
4:
2
2
5
5
Factor
Factor
Factor
Factor
#
#
#
#
1:
2:
3:
4:
2
2
2
2
If the input is 53, since it is a prime number, the output has only one factor: 53 itself.
Factor # 1: 53
If the input value is 586390350, the output consists of 10 factors 2, 3, 5, 5, 7, 7, 13, 17,
19 and 19.
Factor # 1: 2
Factor # 2: 3
Factor
Factor
Factor
Factor
Factor
Factor
Factor
Factor
#
#
#
#
#
#
#
#
3: 5
4: 5
5: 7
6: 7
7: 13
8: 17
9: 19
10: 19
Discussion
How to remove a factor from a given number? I believe you have learned it in high
school. Let the given number be n and we know x is a factor of n. Then, we just keep
dividing n by x until the quotient is 1 or x cannot evenly divide n.
For example, 3 is a factor of 72. The first division yields a quotient 24=72/3. The second
division yields a quotient 8=24/3. Thus, the original number 72 has two factors of 3.
If n and x are both 53, then the first division yields a quotient 1=53/53. Since the quotient
is 1, no more division is necessary and 53 has a factor 53!
So, how to convert the above idea to a program? Let us use Input and Divisor for n and
x, respectively. The following DO-loop will do the job:
DO
IF (MOD(Input,Divisor) /= 0 .OR. Input == 1) EXIT
... since MOD(Input,Divisor)=0 here, Divisor is a factor...
Input = Input / Divisor
END DO
In the above, if Divisor cannot evenly divide Input or Input is 1, we exit the loop. The
former condition states that Divisor is not a factor of Input, while the latter means Input
is 1 and does not have any other factor.
If both conditions are .FALSE., then Divisor can evenly divide Input and Input is not 1.
Therefore, Input is a factor of Divisor. To remove it, just perform a division and this is
the meaning of Input = Input / Divisor.
Since 2 is the only even prime number, we'd better remove all factors of 2 before starting
any other work. Therefore, letting Divisor to 2 in the above code will remove all factor of
2:
DO
IF (MOD(Input,2) /= 0 .OR. Input == 1) EXIT
... since MOD(Input,2)=0 here, 2 is a factor...
Input = Input / Divisor
END DO
After exiting this loop, we are sure the new value of Input will have no factors of 2.
Then, we can try all odd numbers to see some of them could be factors.
Why the factors found are prime numbers? A good question, indeed.
Divisor = 3
DO
IF (Divisor > Input) EXIT
...remove all factors of Divisor...
Divisor = Divisor + 2
END DO
It is not difficult to answer, however. If Input does have a composite factor (a composite
number is the product of several prime numbers), say x = a*b, where a is a prime
number. Then, before the program can test if x is a factor, a has been tested since a < x,
and the factor a is removed. Consequently, only a possible factor b remains. In other
words, composite number x is never tested and the program will not report any composite
factors.