| | |
C++ prime numbers
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
My teacher is killing me, he just graduated from Yale, and he hasn't been teaching long that is very apparent. I don't know, but I think this has something to do with the complexity of his assignments for this, the first class you take for Computer Science, C++.In our first assignment on functions, he gives us this. He wants us to find any two prime numbers that add up to an even number. He wants us to print out every even number from 4 to 1000, and the two prime numbers which add up to it. for example: 4=2+2 , 10=3+7 , 12=5+7 36=13+23
We have to implement these 4 functions:
int checkPrimer(int n)
Checks if n is a prime number. Return 1 if yes, return 0 if not.
{ for(int m = 2; m*m <= n; m++)
if(n % m == 0)
return 0;
return 1;
}
int findNextPrimer(int n)
Finds next prime number > than n.
findNextPrimer(20)=23
This function must call checkPrimer.
input n
m = n + 1;
Call checkPrimer(m)
if it is prime return m.
if it is not prime m = m +1,
then call checkPrimer(m) again
int onePlusOne(int e)
Given even number e, find where e=p1+p2.
It returns p1(we can compute p2, because p2=e - p1)
This function must call checkPrimer & findNextPrimer.
input e which is > 4
assign 3 to p1
p2 = e - p1
Call checkPrimer(p2)
if yes return p1
if no then p1 = findNextPrimer(p1)
is p1 <= e, if yes go back up to the
p2 = e - p1 step
int printVerification(int e1, int e2, int width)
Prints out all e=p1+p2, for all even numbers e
between e1 & e2. Width is used to control amount
of expressions on each row. This function must call onePlusOne.
I must use printVerification to output (4, 1000, 5).
Each expression takes 30 spaces, each row has 5 expressions.
Print all twin primer pairs between 4 & 1000. (11, 13) is one pair, they equal 24 and equal number.
Any pointers would be welcome, thankyou in advance.
Last edited by Stony; Oct 25th, 2006 at 6:13 pm.
I broke it down into the 4 functions I need, just don't know how to go about it.
That took some time in and of itself.
Heres what I got so far, and it doesn't work correctly.
Without any functions at all, just the main.
I don't need any code so to speak, I need a start on how to design an algorithm on a topic(primenumbers) that is new to me. My classmates, most of them are way lost, cause most of them don't even understand the C++ part of it, much less the prime number part.
Thanks for your motivational help though, I appreciate it.
#include<iostream>
#include<iomanip>
#include<cmath>
#include<fstream>
usingnamespace std;
int main()
{
double i;
int j,aa,k;
int prime[400];
ofstream outfile;
outfile.open("output.txt");
k=0;
for(i = 2; i<= 1000; i++)
{
aa=i;
for(j = 2; j<= sqrt(i); j++)
{
int(i)%j?0 : (aa=0);
}
if (aa!=0)
{
k++;
prime[k]=i;
}
}
int kk=0;
for (int ii=1;ii<=k;ii++)
{
for (int jj=1;jj<=k;jj++ )
{
if ((prime[ii]+prime[jj])%2==0)
{
kk++;
if(kk%5==0)
{
outfile <<"("<<prime[ii]<<","<<prime[jj]<<")"<<endl;
}
else
{
outfile <<"("<<prime[ii]<<","<<prime[jj]<<")";
}
}
}
}
return 0;
}
That took some time in and of itself.
Heres what I got so far, and it doesn't work correctly.
Without any functions at all, just the main.
I don't need any code so to speak, I need a start on how to design an algorithm on a topic(primenumbers) that is new to me. My classmates, most of them are way lost, cause most of them don't even understand the C++ part of it, much less the prime number part.
Thanks for your motivational help though, I appreciate it.
#include<iostream>
#include<iomanip>
#include<cmath>
#include<fstream>
usingnamespace std;
int main()
{
double i;
int j,aa,k;
int prime[400];
ofstream outfile;
outfile.open("output.txt");
k=0;
for(i = 2; i<= 1000; i++)
{
aa=i;
for(j = 2; j<= sqrt(i); j++)
{
int(i)%j?0 : (aa=0);
}
if (aa!=0)
{
k++;
prime[k]=i;
}
}
int kk=0;
for (int ii=1;ii<=k;ii++)
{
for (int jj=1;jj<=k;jj++ )
{
if ((prime[ii]+prime[jj])%2==0)
{
kk++;
if(kk%5==0)
{
outfile <<"("<<prime[ii]<<","<<prime[jj]<<")"<<endl;
}
else
{
outfile <<"("<<prime[ii]<<","<<prime[jj]<<")";
}
}
}
}
return 0;
}
Last edited by Stony; Oct 25th, 2006 at 8:29 pm.
•
•
Join Date: Jul 2005
Posts: 60
Reputation:
Solved Threads: 4
I would personally take this advice one step further and break functions down into little segments of code to figure out. I am not gonna do your work for you but I will give you a little tip for the prime function to make the code a little more efficient. Only odd numbers can be prime. 2 is the only even prime number. If an even number other than 2 is inputted, then don't call the function to see if its prime. This avoids needless calls and needless computation.
look guys I don't need somebody to do my work for me, heres the program I already turned in listing all the prime numbers from 2 - 997.
#include<iostream>
#include<iomanip>
#include<cmath>
usingnamespace std;
int main()
{
double prime;
int count;
int remainder;
int LHu;
LHu = 0;
for(prime = 2; prime <= 1000; prime++)
{
remainder = prime;
for(count = 2; count <= sqrt(prime); count++)
{
int (prime) % count ?0: (remainder = 0);
}
if (remainder != 0)
{
//if......else to get 5 numbers on a line
LHu++;
if (LHu % 5 == 0)
{
cout << setw(6) << prime << " " << endl;
}
else
{
cout << setw(6) << prime << " ";
}
}
}
return 0;
}
All I was asking for is a little advice in how to setup my algorithm, I can do the code. I am a straight A student (3.871), I made an A in this class in the summer, just with a lot easier teacher. I have never had to learn about prime numbers. I am more interested in learning about functions, parameters, arrays, structs, classes, etc. I could care less about prime numbers, and how to find all even numbers in existence and have two prime numbers that equal them. Is prime number excellence a part of C++.
Anyways thanks for your help with my algorithm.
Can somebody just tell me the steps necessary.
Like, for example:
1. get the prime numbers
2. get even numbers
3. find two prime numbers that equal each and every even number known to man
4. print the pairs out (11,13) (3,23) etc.
5. clap your hands cause you did a good job
6. etc.
I don't think I could do this with a paper and pencil, oh well, its only worth like 4% of our grade, and with the curves he gives, don't give me any hints, its ok. After all, I've only put in 5 or 6 hours on trying to find a good algorithm. But that does sound like good advice, break down the problem into little pieces, compile often, etc. Unfortunately, my teacher already told us that on the first day of class.
#include<iostream>
#include<iomanip>
#include<cmath>
usingnamespace std;
int main()
{
double prime;
int count;
int remainder;
int LHu;
LHu = 0;
for(prime = 2; prime <= 1000; prime++)
{
remainder = prime;
for(count = 2; count <= sqrt(prime); count++)
{
int (prime) % count ?0: (remainder = 0);
}
if (remainder != 0)
{
//if......else to get 5 numbers on a line
LHu++;
if (LHu % 5 == 0)
{
cout << setw(6) << prime << " " << endl;
}
else
{
cout << setw(6) << prime << " ";
}
}
}
return 0;
}
All I was asking for is a little advice in how to setup my algorithm, I can do the code. I am a straight A student (3.871), I made an A in this class in the summer, just with a lot easier teacher. I have never had to learn about prime numbers. I am more interested in learning about functions, parameters, arrays, structs, classes, etc. I could care less about prime numbers, and how to find all even numbers in existence and have two prime numbers that equal them. Is prime number excellence a part of C++.
Anyways thanks for your help with my algorithm.
Can somebody just tell me the steps necessary.
Like, for example:
1. get the prime numbers
2. get even numbers
3. find two prime numbers that equal each and every even number known to man
4. print the pairs out (11,13) (3,23) etc.
5. clap your hands cause you did a good job
6. etc.
I don't think I could do this with a paper and pencil, oh well, its only worth like 4% of our grade, and with the curves he gives, don't give me any hints, its ok. After all, I've only put in 5 or 6 hours on trying to find a good algorithm. But that does sound like good advice, break down the problem into little pieces, compile often, etc. Unfortunately, my teacher already told us that on the first day of class.
It seems to me that your program is defined above -- each function and it's algorithm -- in your first post. You can almost convert those steps directly into code.
The only thing that's missing is your main() which processes your initial loop from 4 to 1000.
Although I'd change one aspect of your first function listed:
In C/C++, 0 is generally regarded as FALSE, 1 is TRUE.
The only thing that's missing is your main() which processes your initial loop from 4 to 1000.
Although I'd change one aspect of your first function listed:
{ for ( int m = 2; m*m <= n; m++) { if(n % m == 0) { return 0; // this is your TRUE condition } } return 1; // this is your FALSE condition }
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
yea, thats what I needed all along, the main and the plan to implement all four functions plus main. Thats why in my first post, I put down the 4 functions I needed, and the details of my assignment. All I've got from this forum, is keep your chin up, work on it in small chunks, you already have the algorithm, etc.
I look at some other threads in this same forum area, and guys have asked all kinds of simple questions, like how to count from 1 to 50, add all those #'s together etc.
So for the easy questions, forum members will help you out, but ask you guys something a little tough, and its well I don't want to do all your work for you, try a little harder, etc.
Thats real fair.
Perhaps next time I need some help, I will try to find an easy question for you guys.
Thats the end of the story.
Good day. :cheesy:
I look at some other threads in this same forum area, and guys have asked all kinds of simple questions, like how to count from 1 to 50, add all those #'s together etc.
So for the easy questions, forum members will help you out, but ask you guys something a little tough, and its well I don't want to do all your work for you, try a little harder, etc.
Thats real fair.
Perhaps next time I need some help, I will try to find an easy question for you guys.
Thats the end of the story.
Good day. :cheesy:
![]() |
Similar Threads
- (Noob) need some help w/ Prime Numbers (C++)
- Prime Numbers (C)
- prime numbers homework trouble (C++)
- help with array that determines prime numbers (C)
- prime numbers (C++)
- prime numbers (C++)
Other Threads in the C++ Forum
- Previous Thread: CPU scheduling algorithm
- Next Thread: how to i make a chart?
| Thread Tools | Search this Thread |
api array based binary bitmap c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






