Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Help with simple Fortran program

  1. Mar 7, 2006 #1
    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)
    if (mod(n,i)==0) then
    print *, i
    n = n/i
    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?
  2. jcsd
  3. Mar 12, 2006 #2
    Change this:

    do while (n>1)

    I think that you need write:

    do while (n>1)
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook