First n twin primes

U

Umesh

Write a program for this purpose.
Note: If the difference of two consecutive primes is 2, they are
called twin primes. e.g. 3 & 5, 11 & 13 etc.
 
F

Flash Gordon

Umesh wrote, On 28/04/07 11:14:
Write a program for this purpose.

No thanks. If you want a program then write it. Post your own code here
and you may get some help.
 
U

Umesh

/*I can program it if user gives an upper limit. If user wants to
generate first n twin primes, I'm failing to do that. Slight
modifications are required. Thnaks.*/

// TWIN PRIMES BELOW A RANGE

#include <stdio.h>
#include <math.h>
int main(void)
{
long r,i,j,k=1,a[1000],num=0;
printf("Enter Upper Limit: ");
scanf("%ld",&r);
for(i=3;i<=r;++i)
{
int flag=0;
for(j=2;j<=sqrt(i)+1;++j)
if(i%j==0) {flag=1;break;}
if(flag==0) {a[k]=i;k++;}
}
printf( "\nTwin Primes are: \n");
for(int l=1;l<=k;l++)
if((a[l]-a[l-1])==2)
{num++; printf("%ld. %ld, %ld\n",num,a[l-1],a[l]);}
return 0;
}
 
A

Army1987

Umesh said:
/*I can program it if user gives an upper limit. If user wants to
generate first n twin primes, I'm failing to do that. Slight
modifications are required. Thnaks.*/

I'd use more whitespace (and, compared with most programmers, I'm
one who tends to spare it).
// TWIN PRIMES BELOW A RANGE
Only legal in C99. Does your compiler fully support it?
#include <stdio.h>
#include <math.h>
int main(void)
{
long r,i,j,k=1,a[1000],num=0;
How 'bout some more descriptive names?
printf("Enter Upper Limit: ");
fflush(stdin);
Otherwise, text sent to stdout might not be shown until the next
newline character.
scanf("%ld",&r);
What if I write "x" and hit return?
Or, what if I enter a value larger than the 1000th prime?
for(i=3;i<=r;++i)
{
int flag=0;
for(j=2;j<=sqrt(i)+1;++j)
Or j*j < i, so you won't need math.h.
if(i%j==0) {flag=1;break;}
if(flag==0) {a[k]=i;k++;}
a[0] is being left empty. Is this purposeful?
(FYI, arrays are numbered from 0 to (here) 999.)
}
printf( "\nTwin Primes are: \n");
for(int l=1;l<=k;l++)
if((a[l]-a[l-1])==2)
{num++; printf("%ld. %ld, %ld\n",num,a[l-1],a[l]);}
return 0;
}
 
A

Army1987

Umesh said:
/*I can program it if user gives an upper limit. If user wants to
generate first n twin primes, I'm failing to do that. Slight
modifications are required. Thnaks.*/

// TWIN PRIMES BELOW A RANGE

#include <stdio.h>
#include <math.h>
int main(void)
{
long r,i,j,k=1,a[1000],num=0;
printf("Enter Upper Limit: ");
scanf("%ld",&r);
for(i=3;i<=r;++i)
{
int flag=0;
for(j=2;j<=sqrt(i)+1;++j)
Are you sure you want to use *all* values of j from 2 to sqrt(i)+1?

If you want to check wheter n is prime, remember that all primes >2
are odd, so you might want to do:

int is_prime(unsigned n)
{
int flag;
switch (n) {
case 0:
case 1:
flag = 0;
break;
case 2:
flag = 1;
break;
default:
if (n % 2 == 0)
flag = 0;
else {
int i;
flag = 1;
for (i = 3; i*i <= n; i += 2)
if(n % i == 0) {
flag = 0;
break;
}
}
}
return flag;
}
But all primes >4 are either 6i - 1 or 6i + 1, so try:
int is_prime(unsigned n)
{
int flag;
switch (n) {
case 0:
case 1:
case 4:
flag = 0;
break;
case 2:
case 3:
flag = 1;
break;
default:
if (n % 6 != 1 && n % 6 != 5)
flag = 0;
else {
int i1, i2;
flag = 1;
for (i1 = 5, i2 = 7; i1*i1 <= n; i1 += 6, i2 += 6)
if(n % i1 == 0 || n % i2 == 0) {
flag = 0;
break;
}
}
}
return flag;
}
This way you'll need about 1/3 as many divisions.
If you are building an array of all the first N primes, you might
speed it up even more by only trying to divide candidates by
*primes* below sqrt(candidate), using the part of the array you've
already built. The implementation of this is left as an exercise.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top