Solving Twin Primes with a Sieve Algorithm in Fortran

Click For Summary

Discussion Overview

The discussion centers around the challenge of implementing a sieve algorithm in Fortran to find twin primes. Participants explore the difficulties encountered in the coding process and seek assistance in refining their approach.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • The original poster describes their current implementation of a sieve algorithm that successfully finds prime numbers but struggles to identify twin primes, which are defined as pairs of primes that differ by two.
  • Some participants inquire about the algorithm used for finding twin primes, indicating a desire for further clarification or alternative methods.
  • A participant suggests that the issue may lie in the final DO loop's IF statement, recommending a clearer logical structure using the AND operator instead of chaining equality checks.
  • Another participant points out that checking even indexes is unnecessary, suggesting that the loop should start at index 3 and skip even numbers to improve efficiency.
  • Errors in the original code are noted, including a misspelling of "ENDDO" and incorrect syntax in the WRITE statement, which could lead to runtime issues.

Areas of Agreement / Disagreement

Participants generally agree on the need for code corrections and optimizations, but there is no consensus on the overall approach to finding twin primes or the best practices for implementing the algorithm.

Contextual Notes

Limitations include potential misunderstandings of Fortran syntax and logic, as well as the original poster's uncertainty about the effectiveness of their approach to finding twin primes.

Who May Find This Useful

Readers interested in programming algorithms for prime number generation, particularly in Fortran, as well as those seeking to understand the nuances of implementing mathematical concepts in code.

ƒ(x) → ∞
Messages
24
Reaction score
0

Homework Statement



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!

Homework Equations



-

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

n=0

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

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

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

end
 
Last edited:
Physics news on Phys.org
What's your algorithm for finding twin primes?
 
Added my solution.
 
You might be having problems in your last DO loop, in your IF statement.
ƒ(x) → ∞ said:
Code:
do i=2, 100000
  if (s(i).eq.s(i+2).eq.1) then
    n=n+1
    write(*,*,*) n,i,i+2 
  endif
enddo

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:
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:
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.
 
Thanks Mark for the help, some of the errors seem stupid now, but I wouldn't have found them without your help.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
6K
  • · Replies 32 ·
2
Replies
32
Views
5K
Replies
33
Views
5K
Replies
7
Views
3K
  • · Replies 36 ·
2
Replies
36
Views
2K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 7 ·
Replies
7
Views
2K
Replies
4
Views
3K