#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; }

# Number of twin primes between an interval

Page 1 of 1## 6 Replies - 1505 Views - Last Post: 11 February 2015 - 01:37 PM

### #1

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

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

### #2

## Re: Number of twin primes between an interval

Posted 11 February 2015 - 01:20 PM

### #3

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

### #4

## Re: Number of twin primes between an interval

Posted 11 February 2015 - 01:31 PM

Found it thanks for the help!

### #5

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

http://stackoverflow...s-wrong-results

### #6

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

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

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.

Page 1 of 1