I need to write a program that will print the prime factorization of a number.(adsbygoogle = window.adsbygoogle || []).push({});

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?

**Physics Forums - The Fusion of Science and Community**

Dismiss Notice

Join Physics Forums Today!

The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

# Help with simple Fortran program

Loading...

Similar Threads - Help simple Fortran | Date |
---|---|

A little help with a simple Tcl/Tk window/buttons layout | Jul 7, 2016 |

C++ and pthread simple program help | Feb 25, 2015 |

Simple fortran help | Sep 17, 2012 |

Help with a very simple fortran program. | Jan 22, 2012 |

Simple do-loop in Fortran. Please help ! | Jun 14, 2010 |

**Physics Forums - The Fusion of Science and Community**