473,382 Members | 1,622 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,382 software developers and data experts.

Find Twin Primes between 1 and 1000

13
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 example 3 and 5)
here is code for finding a prime if it helps
Expand|Select|Wrap|Line Numbers
  1.  #include <iostream> 
  2. using namespace std;
  3. int main ()
  4. {
  5.     int prime, range=1000;
  6.     int n;
  7.     for ( prime = 2; prime<=range; prime++ )
  8.     {
  9.         for ( n=2; n < prime; n++ )
  10.  
  11.  
  12.             if (prime%n==0 )
  13.                 break;
  14.             if ( n==prime )
  15.                 cout << prime << endl;
  16.             }
  17.     return 0;
  18. }
  19.  
Feb 12 '07 #1
14 23969
sicarie
4,677 Expert Mod 4TB
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 example 3 and 5)
here is code for finding a prime if it helps
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3. int main ()
  4. {
  5.     int prime, range=1000;
  6.     int n;
  7.     for ( prime = 2; prime<=range; prime++ )
  8.     {
  9.         for ( n=2; n < prime; n++ )
  10.  
  11.  
  12.             if (prime%n==0 )
  13.                 break;
  14.             if ( n==prime )
  15.                 cout << prime << endl;
  16.             }
  17.     return 0;
  18. }
  19.  
I would put the answers of that in an array, and then iterate through the array comparing each element with its next largest to see if it is part of a twin prime set, and outputting or doing whatever you needed with those.
Feb 12 '07 #2
hey77
13
how can you get it to take one prime and then look at its next in the list?
Feb 12 '07 #3
sicarie
4,677 Expert Mod 4TB
how can you get it to take one prime and then look at its next in the list?
Instead of cout'ing the number, assign it to an array (I would recommend reading this first to get a general idea, and then this for a little more info into how memeory is handled - if you are interested), and then create two tmp variables.

Assign the tmp vars (possibly: int tmp_var1, tmp_var2;) then you can use a loop to go through and point one to one place in the index, and another to the place after. So you have two primes, and all you need to do is check if they are twin primes.

Does that make sense (ie, can you construct an algorithm (basic list of steps) from that)?
Feb 12 '07 #4
hey77
13
makes sense! thanks....only problem is i dont think i am allowed to use arrays yet
Feb 12 '07 #5
RRick
463 Expert 256MB
You can also speed up the inner loop by checking for n <= sqrt(prime). I turns out that if a number has any factors (i.e. not prime), then at least one of those factors is <= sqrt( prime).

For n in the for loop, try:
Expand|Select|Wrap|Line Numbers
  1. #include "math.h"
  2. .......
  3. for ( n=2,  n<= int(sqrt(prime))+1, n++)
  4. ......
  5.  
  6.  
Feb 12 '07 #6
sicarie
4,677 Expert Mod 4TB
makes sense! thanks....only problem is i dont think i am allowed to use arrays yet
Then you can create the tmp, and store it, and then calculate the next one. At that point, you check the tmp to the current (in the 'if (n==prime)' loop), and then output or whatever.

The tricky part will be the initialization - you start from two, so you need to set the tmp_prime1 (or whatever you call it), initially, to something that won't be seen as a twin prime to 2 (I believe 0 would qualify), so you would want to set it to -1 or something, and then set tmp_prime1 to 2 after the first iteration. Or you could set it to 2, and start the count from 3... It all depends how you want to implement it.
Feb 12 '07 #7
hey77
13
how could i set the tmp to the previous prime before it becomes the next?
for example set the tmp to 2, change prime to 3, check if they are a twin, and then repeat the process? would this work? ( maybe i should check how many twin primes there are :)
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3. int main ()
  4. {
  5.     int prime, range=1000;
  6.     int n,tmp=0,count=0;
  7.     for ( prime = 2; prime<=range; prime++ )
  8.     {
  9.         tmp=prime;
  10.         for ( n=2; n < prime; n++ )
  11.  
  12.  
  13.             if (prime%n==0 )
  14.                 break;
  15.             if ( prime=tmp+2 )
  16.                 count=count+1;
  17.             }
  18.     cout << "Twin Primes = " << count << endl;
  19.     return 0;
  20. }
Feb 12 '07 #8
sicarie
4,677 Expert Mod 4TB
how could i set the tmp to the previous prime before it becomes the next?
for example set the tmp to 2, change prime to 3, check if they are a twin, and then repeat the process? would this work? ( maybe i should check how many twin primes there are :)
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3. int main ()
  4. {
  5.     int prime, range=1000;
  6.     int n,tmp=0,count=0;
  7.     for ( prime = 2; prime<=range; prime++ )
  8.     {
  9.         tmp=prime;
  10.         for ( n=2; n < prime; n++ )
  11.  
  12.  
  13.             if (prime%n==0 )
  14.                 break;
  15.             if ( prime=tmp+2 )
  16.                 count=count+1;
  17.             }
  18.     cout << "Twin Primes = " << count << endl;
  19.     return 0;
  20. }
Personally, I would change the initial value of whatever tmp to be something odd so that it is never considered for the twin prime, that way you wouldn't really have to modify much of your code. Then, I would add, where you had the cout<< prime the if (prime=tmp+2); cout << tmp<<prime; tmp=prime;

Something like that (sorry it's kinda squished together, I'm at work - I'll try to clarify later, if that's too confusing).
Feb 12 '07 #9
sicarie
4,677 Expert Mod 4TB
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3. int main ()
  4. {
  5.     int prime, range=1000;
  6.     int n,tmp=0,count=0;
  7.     for ( prime = 2; prime<=range; prime++ )
  8.     {
  9.         tmp=prime;
  10.         for ( n=2; n < prime; n++ ) {
  11.             if (prime%n==0 ) {
  12.                  if ( prime=tmp+2 ) {
  13.                 // I believe this is where you know one is a twin prime
  14.                                 // so cout prime and tmp (or whatever)
  15.                                 // and set tmp equal to prime for the next pass
  16.                             }
  17.             }
  18.                }
  19.         }
  20.     cout << "Twin Primes = " << count << endl;
  21.     return 0;
  22. }
  23.  
I believe that filling those lines in will give you what you need.
Feb 13 '07 #10
hey77
13
thanks for your help!
Feb 13 '07 #11
Manjiri
40
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 example 3 and 5)
here is code for finding a prime if it helps
Expand|Select|Wrap|Line Numbers
  1.  #include <iostream> 
  2. using namespace std;
  3. int main ()
  4. {
  5.     int prime, range=1000;
  6.     int n;
  7.     for ( prime = 2; prime<=range; prime++ )
  8.     {
  9.         for ( n=2; n < prime; n++ )
  10.  
  11.  
  12.             if (prime%n==0 )
  13.                 break;
  14.             if ( n==prime )
  15.                 cout << prime << endl;
  16.             }
  17.     return 0;
  18. }
  19.  

Hello friend... here i have the solution... you can see...

Expand|Select|Wrap|Line Numbers
  1. /* Find Twin Primes between 1 and 1000 */
  2.  
  3. #include<iostream.h>
  4.  
  5. int main()
  6. {
  7. int i,j,n,flag=0,prime_number=0,temp;
  8. cout<<"Enter the number up to which you want to search the twin prime numbers"<<endl;
  9. cin>>n;
  10. for(i=3; i<=n; i++)
  11. {
  12. for(j=2; j<i; j++)
  13. {
  14. if((i%j)==0)
  15. {
  16. flag=1;
  17. }
  18. }
  19. if(flag==0)
  20. {
  21. if(prime_number==0)
  22. {
  23. prime_number=i;
  24. }
  25. else if(i-prime_number==2)
  26. {
  27. cout<<"Twin prime number : "<<"("<< prime_number<<"," << i<<")"<<endl<<endl;
  28. prime_number=i;
  29. }
  30. else
  31. {
  32. prime_number=i;
  33. }
  34. }
  35. flag=0;
  36.  
  37. }
  38. return 0;
  39. }
Feb 13 '07 #12
Manjiri
40
thanks for your help!


Ohhh........ Got it.. it's ok...
Feb 13 '07 #13
hey77
13
no man your code helped too........much more concise then my jumbled mess
Feb 14 '07 #14
Manjiri
40
no man your code helped too........much more concise then my jumbled mess

Ok... it's good then....
Feb 14 '07 #15

Sign in to post your reply or Sign up for a free account.

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...
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 *...
5
by: nmtoan | last post by:
Hi, I could not find any answer to this simple question of mine. Suppose I have to write a program, the main parts of it are as follows: #include <blahblah.h> struct {
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...
1
by: nizam | last post by:
Hi all, i have a simple Problem. i have written a code in asp.net for sending email. my code is working fine. but entire body content is not going only 1000 bytes are going. above 1000 bytes all...
18
by: baltimoredude1 | last post by:
Hi I was writing a simple code to generate the first 100 prime numbers. Everything looks fine to me except the output of the program. What's wrong with it? I am attaching the program as well as...
16
by: Michael M. | last post by:
How to find the longst element list of lists? I think, there should be an easier way then this: s1 = s2 = s3 = if len(s1) >= len(s2) and len(s1) >= len(s3): sx1=s1 ## s1 ist längster
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...
7
by: Umesh | last post by:
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.
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.