samedi 7 février 2015

Finding double primes between two values

I've been stuck on this problem for ages now and don't seem to know how to complete this program. I am a beginner in programming so I only know C. I will admit this is for an assignment and I am not looking for the answer but I would really appreciate a little help with the problem. This is what I have:



#include <stdio.h>
#include <math.h>

void main()
{
int low, high, n, count,i;

scanf ("%d%d",&low, &high);

count=0;

n=0;

if (low<=3)
count=count+1;


while (n<low)
{

n=n+6;

}

while (n<high)
{
i=1;

while (i<sqrt(n+1))
{
i=i+2;

if ((n-1)%i==0 || (n+1)%i==0)

continue;

else
count=count+1;
}

n=n+6;

}

printf("the number of twin primes between %d and %d are %d",low, high, count);


}


Am I using while loops wrong and/or the if statements? I haven't been taught to use for loops so I can't use those. I also have to use the fact that every twin prime besides {3,5} follow the formula 6+/-1.


Thank you for helping me


Aucun commentaire:

Enregistrer un commentaire