6 Replies - 1124 Views - Last Post: 11 February 2015 - 01:37 PM Rate Topic: ***-- 2 Votes

#1 spaceinvader1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 2150
  • View blog
  • 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.
Was This Post Helpful? 1
  • +
  • -

#3 spaceinvader1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#4 spaceinvader1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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!
Was This Post Helpful? 0
  • +
  • -

#5 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 2150
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12297
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#7 #define  Icon User is offline

  • Duke of Err
  • member icon

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

Re: Number of twin primes between an interval

Posted 11 February 2015 - 01:37 PM

View PostSalem_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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1