- #1
eku_girl83
- 89
- 0
I need to write a program that will print the prime factorization of a number.
Here's what I have so far:
integer:: n,i
print *, "enter integer"
read *, n
do while (n>1)
i=2
if (mod(n,i)==0) then
print *, i
n = n/i
else
i = i+1
end if
end do
end program
I'm just using trial division to find the prime factorization. For some reason, the program works for powers of 2 (4,8,16,32,64, etc.) but nothing else. It will factor a number such as 20 into (2,2) but doesn't go any farther. What is wrong with my program? Why does it stop trial division at i=2?
Here's what I have so far:
integer:: n,i
print *, "enter integer"
read *, n
do while (n>1)
i=2
if (mod(n,i)==0) then
print *, i
n = n/i
else
i = i+1
end if
end do
end program
I'm just using trial division to find the prime factorization. For some reason, the program works for powers of 2 (4,8,16,32,64, etc.) but nothing else. It will factor a number such as 20 into (2,2) but doesn't go any farther. What is wrong with my program? Why does it stop trial division at i=2?