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.
Click here to download this program.! --------------------------------------------------------------- ! This program determines all prime factors of an n integer >= 2. ! It first removes all factors of 2. Then, removes all factors ! of 3, 5, 7, and so on. All factors must be prime numbers since ! when a factor is tried all of whose non-prime factors have ! already been removed. ! --------------------------------------------------------------- PROGRAM Factorize IMPLICIT NONE INTEGER :: Input INTEGER :: Divisor INTEGER :: Count WRITE(*,*) 'This program factorizes any integer >= 2 --> ' READ(*,*) Input 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
This program factorizes any integer >= 2 --> 100 Factor # 1: 2 Factor # 2: 2 Factor # 3: 5 Factor # 4: 5
This program factorizes any integer >= 2 --> 16 Factor # 1: 2 Factor # 2: 2 Factor # 3: 2 Factor # 4: 2
This program factorizes any integer >= 2 --> 53 Factor # 1: 53
This program factorizes any integer >= 2 --> 586390350 Factor # 1: 2 Factor # 2: 3 Factor # 3: 5 Factor # 4: 5 Factor # 5: 7 Factor # 6: 7 Factor # 7: 13 Factor # 8: 17 Factor # 9: 19 Factor # 10: 19
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!
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.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
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.
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.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
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.
You might feel this is not a very efficient method since testing if 9 and 15 are factors are redundant. Yes, you are right; but, this is already a reasonable complicated program for CS110 and CS201. You could learn more efficient factorization algorithms in other computer science and/or mathematics courses, since this is an extremely important topic.