Solving Problems in Fortran Code for Prime Numbers

  • Context: Fortran 
  • Thread starter Thread starter ƒ(x) → ∞
  • Start date Start date
  • Tags Tags
    Fortran
Click For Summary
SUMMARY

The forum discussion focuses on troubleshooting a Fortran program designed to find prime numbers using the Sieve of Eratosthenes algorithm. The code fails to return correct values when 'n' is greater than one. Key suggestions include debugging by printing the first 100 values of the array 's' to verify the alignment of 1s with prime numbers and replacing GOTO statements with EXIT for better control flow. Additionally, using array slicing for initialization is recommended to simplify the code.

PREREQUISITES
  • Understanding of Fortran programming, specifically version 90 or later
  • Familiarity with the Sieve of Eratosthenes algorithm for prime number generation
  • Knowledge of array manipulation and initialization in Fortran
  • Basic debugging techniques in programming
NEXT STEPS
  • Learn advanced debugging techniques in Fortran, including using print statements effectively
  • Explore array initialization methods in Fortran, particularly array slicing
  • Research control flow improvements in Fortran, focusing on EXIT vs. GOTO statements
  • Study optimization techniques for the Sieve of Eratosthenes algorithm in Fortran
USEFUL FOR

This discussion is beneficial for Fortran developers, computer science students, and anyone interested in optimizing algorithms for prime number generation.

ƒ(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
 

Similar threads

  • · Replies 20 ·
Replies
20
Views
4K
  • · Replies 22 ·
Replies
22
Views
5K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 4 ·
Replies
4
Views
8K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 19 ·
Replies
19
Views
7K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 8 ·
Replies
8
Views
2K