# Solving Twin Primes with a Sieve Algorithm in Fortran

• Comp Sci
• ƒ(x) → ∞
In summary, the program is finding prime numbers that differ by two, however it is having difficulty finding prime twins. The programmer is trying to find an algorithm to calculate twin primes.
ƒ(x) → ∞

## 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!

-

## 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:
What's your algorithm for finding twin primes?

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.

## 1. What are twin primes?

Twin primes are a pair of prime numbers that are only two numbers apart from each other. For example, 41 and 43 are twin primes, as they are both prime numbers and only have a difference of 2.

## 2. What is a sieve algorithm?

A sieve algorithm is a method for finding prime numbers. It involves creating a list of numbers and eliminating multiples of known primes to narrow down the list to only prime numbers. In this case, the sieve algorithm is used to find twin primes.

## 3. What is Fortran?

Fortran is a computer programming language that is commonly used for scientific and engineering applications. It was one of the first high-level programming languages and is known for its efficient handling of mathematical calculations.

## 4. How does the sieve algorithm work for finding twin primes?

The sieve algorithm for finding twin primes involves creating a list of numbers starting from 3, as 2 is the only even prime number. Then, we mark off all multiples of 3, 5, 7, and so on until we reach the square root of the largest number in the list. Any numbers that are not marked off after this process are considered twin primes.

## 5. Why use Fortran for solving twin primes with a sieve algorithm?

Fortran is a powerful programming language that is well-suited for mathematical calculations. It is also known for its efficiency and speed, making it a great choice for solving problems involving large numbers, such as finding twin primes with a sieve algorithm.

• Engineering and Comp Sci Homework Help
Replies
2
Views
5K
• Engineering and Comp Sci Homework Help
Replies
7
Views
2K
• Engineering and Comp Sci Homework Help
Replies
32
Views
3K
• Engineering and Comp Sci Homework Help
Replies
33
Views
3K
• Engineering and Comp Sci Homework Help
Replies
8
Views
2K
• Programming and Computer Science
Replies
28
Views
1K
• Engineering and Comp Sci Homework Help
Replies
80
Views
8K
• Engineering and Comp Sci Homework Help
Replies
4
Views
2K
• Engineering and Comp Sci Homework Help
Replies
8
Views
2K
• Engineering and Comp Sci Homework Help
Replies
7
Views
1K