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 - #include <iostream>
-
using namespace std;
-
int main ()
-
{
-
int prime, range=1000;
-
int n;
-
for ( prime = 2; prime<=range; prime++ )
-
{
-
for ( n=2; n < prime; n++ )
-
-
-
if (prime%n==0 )
-
break;
-
if ( n==prime )
-
cout << prime << endl;
-
}
-
return 0;
-
}
-
14 23969
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 -
#include <iostream>
-
using namespace std;
-
int main ()
-
{
-
int prime, range=1000;
-
int n;
-
for ( prime = 2; prime<=range; prime++ )
-
{
-
for ( n=2; n < prime; n++ )
-
-
-
if (prime%n==0 )
-
break;
-
if ( n==prime )
-
cout << prime << endl;
-
}
-
return 0;
-
}
-
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.
how can you get it to take one prime and then look at its next in the list?
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)?
makes sense! thanks....only problem is i dont think i am allowed to use arrays yet
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: -
#include "math.h"
-
.......
-
for ( n=2, n<= int(sqrt(prime))+1, n++)
-
......
-
-
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.
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 :) - #include <iostream>
-
using namespace std;
-
int main ()
-
{
-
int prime, range=1000;
-
int n,tmp=0,count=0;
-
for ( prime = 2; prime<=range; prime++ )
-
{
-
tmp=prime;
-
for ( n=2; n < prime; n++ )
-
-
-
if (prime%n==0 )
-
break;
-
if ( prime=tmp+2 )
-
count=count+1;
-
}
-
cout << "Twin Primes = " << count << endl;
-
return 0;
-
}
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 :) - #include <iostream>
-
using namespace std;
-
int main ()
-
{
-
int prime, range=1000;
-
int n,tmp=0,count=0;
-
for ( prime = 2; prime<=range; prime++ )
-
{
-
tmp=prime;
-
for ( n=2; n < prime; n++ )
-
-
-
if (prime%n==0 )
-
break;
-
if ( prime=tmp+2 )
-
count=count+1;
-
}
-
cout << "Twin Primes = " << count << endl;
-
return 0;
-
}
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).
-
#include <iostream>
-
using namespace std;
-
int main ()
-
{
-
int prime, range=1000;
-
int n,tmp=0,count=0;
-
for ( prime = 2; prime<=range; prime++ )
-
{
-
tmp=prime;
-
for ( n=2; n < prime; n++ ) {
-
if (prime%n==0 ) {
-
if ( prime=tmp+2 ) {
-
// I believe this is where you know one is a twin prime
-
// so cout prime and tmp (or whatever)
-
// and set tmp equal to prime for the next pass
-
}
-
}
-
}
-
}
-
cout << "Twin Primes = " << count << endl;
-
return 0;
-
}
-
I believe that filling those lines in will give you what you need.
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 - #include <iostream>
-
using namespace std;
-
int main ()
-
{
-
int prime, range=1000;
-
int n;
-
for ( prime = 2; prime<=range; prime++ )
-
{
-
for ( n=2; n < prime; n++ )
-
-
-
if (prime%n==0 )
-
break;
-
if ( n==prime )
-
cout << prime << endl;
-
}
-
return 0;
-
}
-
Hello friend... here i have the solution... you can see... -
/* Find Twin Primes between 1 and 1000 */
-
-
#include<iostream.h>
-
-
int main()
-
{
-
int i,j,n,flag=0,prime_number=0,temp;
-
cout<<"Enter the number up to which you want to search the twin prime numbers"<<endl;
-
cin>>n;
-
for(i=3; i<=n; i++)
-
{
-
for(j=2; j<i; j++)
-
{
-
if((i%j)==0)
-
{
-
flag=1;
-
}
-
}
-
if(flag==0)
-
{
-
if(prime_number==0)
-
{
-
prime_number=i;
-
}
-
else if(i-prime_number==2)
-
{
-
cout<<"Twin prime number : "<<"("<< prime_number<<"," << i<<")"<<endl<<endl;
-
prime_number=i;
-
}
-
else
-
{
-
prime_number=i;
-
}
-
}
-
flag=0;
-
-
}
-
return 0;
-
}
thanks for your help!
Ohhh........ Got it.. it's ok...
no man your code helped too........much more concise then my jumbled mess
no man your code helped too........much more concise then my jumbled mess
Ok... it's good then....
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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 *...
|
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
{
|
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...
|
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...
|
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...
|
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
|
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...
|
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.
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
| |