944,161 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 14465
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Oct 25th, 2006
0

C++ prime numbers

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Stony is offline Offline
5 posts
since Oct 2006
Oct 25th, 2006
0

Re: C++ prime numbers

Just look at one function at a time. Write, compile, and debug your code until it works. Then go on to the next function, etc. Don't look at the whole thing at once and panic.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Oct 25th, 2006
0

Not panicking just don't know where to start

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;
}
Last edited by Stony; Oct 25th, 2006 at 8:29 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Stony is offline Offline
5 posts
since Oct 2006
Oct 25th, 2006
0

Re: C++ prime numbers

Click to Expand / Collapse  Quote originally posted by Lerner ...
Just look at one function at a time. Write, compile, and debug your code until it works. Then go on to the next function, etc. Don't look at the whole thing at once and panic.
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.
Reputation Points: 10
Solved Threads: 4
Junior Poster in Training
hinde is offline Offline
60 posts
since Jul 2005
Oct 25th, 2006
0

Re: C++ prime numbers

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Stony is offline Offline
5 posts
since Oct 2006
Oct 26th, 2006
0

Re: C++ prime numbers

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:
{ 
    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
}
In C/C++, 0 is generally regarded as FALSE, 1 is TRUE.
Moderator
Reputation Points: 3281
Solved Threads: 896
Posting Sage
WaltP is offline Offline
7,749 posts
since May 2006
Oct 26th, 2006
0

Re: C++ prime numbers

Thank you sir. I just need my main, thats right. And I need to know the steps to take in the main, its finally coming around.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Stony is offline Offline
5 posts
since Oct 2006
Oct 26th, 2006
0

Re: C++ prime numbers

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:
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Stony is offline Offline
5 posts
since Oct 2006
Aug 27th, 2010
-1
Re: C++ prime numbers
#include <iostream.h>
#include <conio.h>
void main(void)
{
clrscr();
int i,p;
for(i=1;i<=300;i=i+1)
{
for(p=2;p<i;p=p+1)
{
if(i%p==0)
break;
else
continue;
}
if(i==p)
cout<<" Prime:"<<p;
}
getch();
}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
tayyab569 is offline Offline
2 posts
since Aug 2010
Aug 27th, 2010
0
Re: C++ prime numbers
Click to Expand / Collapse  Quote originally posted by tayyab569 ...
#include <iostream.h>
#include <conio.h>
void main(void)
{
clrscr();
int i,p;
for(i=1;i<=300;i=i+1)
{
for(p=2;p<i;p=p+1)
{
if(i%p==0)
break;
else
continue;
}
if(i==p)
cout<<" Prime:"<<p;
}
getch();
}

What the #&&$*% is this?

1. void main() is a great way to start fail code
Please, for the love of any deity, use int main.

2. The conio.h is a huge, non-standard fail.
Don't use this. Just don't. It's not standard, it's not cross-platform, and it's
not even very powerful

3. READ THE RULES AND USE CODE TAGS

4. Plain text in a reply is helpful.
Perhaps you should describe what your code is for and why the OP should give a crap about it.

5. Read the original post.
Your solution simply prints all the primes less than 300. This is trivial, and the OP already knows how to determine if a number is prime. His assignment is not so difficult, but this code is anything but helpful to him.

6. Use less pointless code
This is painful:
C++ Syntax (Toggle Plain Text)
  1. for(p=2;p<i;p=p+1) // p=p+1? really? really??????
  2. {
  3. if(i%p==0)
  4. break;
  5. else // There is no point at all to using this
  6. continue; // When the if clause fails, of course the loop will continue
  7. }

perhaps:
C++ Syntax (Toggle Plain Text)
  1. for( p = 2; p < i && i % p != 0; p++ );

7. There are far better ways of doing this
Even for the brute force method can be improved by stopping your search short:

C++ Syntax (Toggle Plain Text)
  1. for( p = 2; p < i / 2 && i % p != 0; p++ );

Of course, the brute force method is a poor way of doing this. A simple google search would give you the Sieve of Eratosthenes, which is a decent method.


I'm sorry to be harsh, but, for some reason, your post really infuriated me. Please, please think before you post. Or, maybe don't post advice (if that's what this post is supposed to be) until you are actually able to provide good advice.
Reputation Points: 152
Solved Threads: 41
Posting Whiz in Training
dusktreader is offline Offline
255 posts
since Jan 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: How to print this?
Next Thread in C++ Forum Timeline: sort a STL list of structs





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC