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

Homework Help: Fortran Help!

  1. Dec 21, 2009 #1
    1. The problem statement, all variables and given/known data

    I have been trying to come up with a program to calculate twin primes using a sieve algorithm in Fortran. So far I have been successful in creating one that finds primes, but I am having difficulty finding one that finds prime twins. If I knew how to do this I could find prime quadruplets!

    2. Relevant equations


    3. The attempt at a solution

    My sieve program is running perfectly, however having run the twin primes program through debugger was not as successful. It is finding what appear to be random numbers. Can anybody help?

    My intial idea was to find prime numbers that differ by two.

    program prime twins
    implicit none
    integer*1 s(100000)
    integer i, j, n


    do i=1, 100000
    s(i) = 1

    do i=2, 100000
    if (s(i).eq.1) then
    do j=2, (100000/i)

    do i=2, 100000
    if (s(i).eq.s(i+2).eq.1) then
    write(*,*,*) n,i,i+2

    Last edited: Dec 22, 2009
  2. jcsd
  3. Dec 21, 2009 #2


    Staff: Mentor

    What's your algorithm for finding twin primes?
  4. Dec 22, 2009 #3
    Added my solution.
  5. Dec 22, 2009 #4


    Staff: Mentor

    You might be having problems in your last DO loop, in your IF statement.
    I'm not sure how s(i) .EQ. s(i + 2) .EQ. 1 will actually evaluate. It would be clearer (and safer) to do this:
    Code (Text):
    if ((s(i) .EQ. 1) .AND. (s(i + 2) .EQ. 1)) then
    Also, there's no need to check each element of your array, since none of the elements with even indexes (except 2) are primes. So you can save a little processing time by starting at index 3 and skipping the even indexes.
    Code (Text):
    DO i = 3, 100000, 2
    Other things
    You misspelled ENDDO in the final DO loop.
    Your WRITE statement is incorrect. The asterisks are not placeholders for the expressions to be printed.
  6. Dec 23, 2009 #5
    Thanks Mark for the help, some of the errors seem stupid now, but I wouldn't have found them without your help.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook