# Number of twin primes between an interval

Page 1 of 1

## 6 Replies - 1124 Views - Last Post: 11 February 2015 - 01:37 PMRate Topic: 2 Votes //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=370131&amp;s=1e54778f449d45ebb98f53b4c525daaf&md5check=' + ipb.vars['secure_hash'], cur_rating: 3, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

Reputation: 0
• Posts: 5
• Joined: 11-February 15

# Number of twin primes between an interval

Posted 11 February 2015 - 01:12 PM

Hi, I'm having some trouble finishing my code, it's meant to give the number of twin primes between an interval e.g. 1 to 1000000 and the answer should be 8169 but all I can get it to is 8168 if anyone could give me any help finishing up it would be much appreciated!

```#include <stdio.h>
int prime (int num) {
int div;
if (num == 2) return 1;
if (num % 2 == 0) return 0;
div = 3;
while (div*div <= num) {
if (num%div == 0)
return 0;
div += 2;
}
return 1;
}
int main(void) {
int low, high, i, count, n, m;
printf("Please enter the values for the lower and upper limits of the interval\n");
scanf("%d%d", &low, &high);
i = low;
count = 0;
while (6*i-1>=low && 6*i+1<=high) {
n = 6*i-1;
m = 6*i+1;
if (prime(n) && prime(m)) ++count;
i = i + 1;
}
printf("Number of twin primes is %d\n", count);
return 0;
}

```

Is This A Good Question/Topic? 0

## Replies To: Number of twin primes between an interval

### #2 Salem_c

• void main'ers are DOOMED

Reputation: 2150
• Posts: 4,218
• Joined: 30-May 10

## Re: Number of twin primes between an interval

Posted 11 February 2015 - 01:20 PM

http://en.wikipedia....wiki/Twin_prime
You seem to forget to count (3,5) with your algorithm.

Reputation: 0
• Posts: 5
• Joined: 11-February 15

## Re: Number of twin primes between an interval

Posted 11 February 2015 - 01:26 PM

I can see but I can't see where I can make the revision in my code to include it?

Reputation: 0
• Posts: 5
• Joined: 11-February 15

## Re: Number of twin primes between an interval

Posted 11 February 2015 - 01:31 PM

Found it thanks for the help!

### #5 Salem_c

• void main'ers are DOOMED

Reputation: 2150
• Posts: 4,218
• Joined: 30-May 10

## Re: Number of twin primes between an interval

Posted 11 February 2015 - 01:31 PM

Another waste of 10 minutes researching what has already been solved elsewhere
http://stackoverflow...s-wrong-results

### #6 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12297
• Posts: 45,395
• Joined: 27-December 08

## Re: Number of twin primes between an interval

Posted 11 February 2015 - 01:34 PM

Just an FYI- we don't delete posts. If you shouldn't be asking for help online, you shouldn't have posted here in the first place.

### #7 #define

• Duke of Err

Reputation: 1853
• Posts: 6,671
• Joined: 19-February 09

## Re: Number of twin primes between an interval

Posted 11 February 2015 - 01:37 PM

Salem_c, on 11 February 2015 - 08:31 PM, said:

Another waste of 10 minutes researching what has already been solved elsewhere
http://stackoverflow...s-wrong-results

It doesn't work when the low value is greater than three I think. The variable i is set to low but they are different quantities, low is the lowest value of the range and i is the iteration/count of the pairs.