Fortran Solving Problems in Fortran Code for Prime Numbers

  • Thread starter Thread starter ƒ(x) → ∞
  • Start date Start date
  • Tags Tags
    Fortran
AI Thread Summary
The discussion centers on a code compilation issue related to a Fortran program designed to find prime numbers that differ by specific offsets (2, 4, 2). The user reports that the code fails to produce values when 'n' is greater than one. Suggestions for troubleshooting include debugging by printing the first 100 values of the array 's' to verify the alignment of 1s with prime numbers. Additionally, it is recommended to replace the GOTO statement with an EXIT statement for better control flow. The conversation also highlights the potential for simplifying array initialization by using implicit array definitions instead of traditional loops.
ƒ(x) → ∞
Messages
24
Reaction score
0
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:
	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.
 
Technology news on Phys.org
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.
 
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:
offset(:)=0
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Replies
20
Views
3K
Replies
22
Views
5K
Replies
8
Views
4K
Replies
6
Views
2K
Replies
19
Views
6K
Replies
1
Views
3K
Replies
8
Views
2K
Back
Top