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

Problems In Fortran

  1. Jan 19, 2010 #1
    Everytime I try to compile the followng code, it fails to give me the values when n is greater than one, I have tried and failed to correct that mistake, can you help?

    Code (Text):
        program sieve
        implicit none
        integer*1 s(1000000), offset (10), sequence
        integer i, j, n

    c   INITIALISATIONS:
        n=0
        sequence=4

        do i=1, 10
        offset(i)=0
        enddo

        offset(1)=2
        offset(2)=4
        offset(3)=2

        do i=1, 1000000
        s(i) = 1
        enddo

        do i=2, 1000000
          if (s(i).eq.1) then
            do j=2, (1000000/i)
               s(i*j)=0
            enddo
          endif
        enddo

        do i=2, 1000000

        if (s(i).eq.1) then
          do j=1,sequence-1  
            if (s(i+offset(j)).ne.1)  goto 10
          enddo
          n=n+1
          write(*,*) n, i,i+2,i+6,i+8
        endif
    10  continue
        enddo


        end
     
    It is looking for primes that differ by 2,4,2 like {5,7,11,13}.

    Thank you.
     
  2. jcsd
  3. Jan 22, 2010 #2
    Your code looks about right but if it doesn't work....

    ... have you tried debugging it? For example, just try printing the first 100 values in 's' and see if the 1s line up with the primes.
     
  4. Jan 22, 2010 #3

    minger

    User Avatar
    Science Advisor

    Try using an EXIT rather than a GOTO in that bottom do loop. Without going line-by-line it's hard to tell.

    Another tip, rather than using do loops do initialize things, you can implicitly define an entire array such as:
    Code (Text):

    offset(:)=0
     
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Problems In Fortran
  1. Fortran problem (Replies: 1)

  2. FORTRAN READ problems (Replies: 5)

Loading...