473,320 Members | 1,950 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

First n twin primes

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.

Apr 28 '07 #1
7 2484
Umesh said:
Write a program for this purpose.
Why?

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Apr 28 '07 #2
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.
--
Flash Gordon
Apr 28 '07 #3
/*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;
}

Apr 28 '07 #4
"Umesh" <fr****************@gmail.comha scritto nel messaggio
news:11**********************@h2g2000hsg.googlegro ups.com...
/*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;
}

Apr 28 '07 #5

"Army1987" <pl********@for.itha scritto nel messaggio
news:f0**********@tdi.cu.mi.it...
fflush(stdin);
I meant fflush(stdout); sorry.
Apr 28 '07 #6

"Army1987" <pl********@for.itha scritto nel messaggio
news:f0**********@tdi.cu.mi.it...
> int flag=0;
for(j=2;j<=sqrt(i)+1;++j)
Or j*j < i, so you won't need math.h.
Actually, j*j <= i (as I'd written it, squares of primes would seem
primes).
>if(i%j==0) {flag=1;break;}

Apr 28 '07 #7

"Umesh" <fr****************@gmail.comha scritto nel messaggio
news:11**********************@h2g2000hsg.googlegro ups.com...
/*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.
Apr 28 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: AdrianK | last post by:
I'm having a lotta problems installing Crypt::RSA on Linux Linux gogol 2.4.17smt-mono using perl5.005_03. Main problem at present is that all the tests fail with Crypt::Primes When I run a trace...
0
by: Morten Gulbrandsen | last post by:
Dear mysql users, in the tutorial of the documentation I found an interesting example of a complicated non trivial sql query: URL:...
12
by: Alan J. Flavell | last post by:
OK, today's email brought a comment from a reader, pointing out something that I'd long since noticed myself but hadn't done anything about it. On my pages, I've got a first-letter style on...
32
by: someone else | last post by:
hi all I'm a newbie to this group. my apologies if I break any rules. I've wrote a simple program to find the first 1,000,000 primes, and to find all primes within any range (up to 200 *...
104
by: fieldfallow | last post by:
Hello all, Is there a function in the standard C library which returns a prime number which is also pseudo-random? Assuming there isn't, as it appears from the docs that I have, is there a...
9
by: Henrootje | last post by:
Hello all, I have a problem that is alike others I have seen but I am not able to apply the solutions I saw using Trim, Left, Right and so on. That is why I ask here. What is the case? I...
15
by: robert maas, see http://tinyurl.com/uh3t | last post by:
Here's the source: #include <stdio.h> #include <errno.h> main () { char* str = "9999999999"; long long int llin; char* endptr; /* Set by strtoll */ int nch; errno = 0; llin = strtoll(str,...
14
by: hey77 | last post by:
how can i calculate all the twin primes, and then add up how many there are? a prime is any number than can only be divised by 1 and itself, and a twin prime is two primes seperated by 2 ( for...
5
by: Carramba | last post by:
theorem states that: Integer n is prime if and only if (x +1)^n ≡ x^n +1 (mod n) in Z. so I testing it, but values doesn't match ... and I don't se why.. I guess :) it's some thing wrong in...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.