Solving Problems in Fortran Code for Prime Numbers

  • Fortran
  • Thread starter ƒ(x) → ∞
  • Start date
  • Tags
    Fortran
In summary, the conversation is about a program called "sieve" which is used to find primes that differ by 2,4,2. The code is not giving the correct values when n is greater than one and the person has tried debugging it without success. They suggest using an EXIT rather than a GOTO in the bottom do loop and using implicit definition for initializing arrays.
  • #1
ƒ(x) → ∞
25
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
  • #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.
 
  • #3
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
 

1. How do I identify prime numbers in Fortran code?

To identify prime numbers in Fortran code, you can use a simple algorithm that checks if a number is divisible by any number smaller than itself. If there are no divisors, the number is prime. You can also use the built-in function "MOD" to check for divisibility.

2. How can I optimize my Fortran code for finding prime numbers?

To optimize your Fortran code for finding prime numbers, you can use efficient algorithms such as the Sieve of Eratosthenes or the Sieve of Sundaram. These algorithms reduce the number of computations needed to identify prime numbers.

3. Can I use Fortran libraries for solving prime number problems?

Yes, there are several Fortran libraries available for solving prime number problems. Some popular ones include the Primegen library, the Prime Numbers library, and the NTL library. These libraries provide efficient algorithms and functions for working with prime numbers.

4. How can I handle errors and exceptions when solving prime number problems in Fortran?

You can use the "ERROR STOP" statement in Fortran to handle errors and exceptions in your code. This statement will stop the program and display an error message if a condition is not met. You can also use "TRY-CATCH" blocks in modern Fortran to catch and handle specific errors.

5. Is Fortran a good language for solving prime number problems?

Yes, Fortran is a good language for solving prime number problems. It is a high-performance language that is designed for scientific computing and mathematical operations, making it well-suited for working with prime numbers. Additionally, Fortran has a long history of use in the scientific community and has many efficient libraries and algorithms available for solving prime number problems.

Similar threads

  • Programming and Computer Science
Replies
20
Views
3K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
4
Views
608
  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
Replies
22
Views
4K
  • Programming and Computer Science
Replies
4
Views
7K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
19
Views
5K
  • Programming and Computer Science
Replies
22
Views
757
Back
Top