Practise Problem, Pair of Prime Numbers

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Aug 2008
Posts: 23
Reputation: vidit_X is an unknown quantity at this point 
Solved Threads: 0
vidit_X vidit_X is offline Offline
Newbie Poster

Re: Practise Problem, Pair of Prime Numbers

 
0
  #11
Sep 5th, 2008
Salem gave me the hint, but I dont got it. Thanks Lerner for helping.
Past:Beginner
Present:Intermediate
Future:Pro

The Future is near, as Daniweb members are so helpful and talented.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,829
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Practise Problem, Pair of Prime Numbers

 
0
  #12
Sep 5th, 2008
Originally Posted by vidit_X View Post
Salem gave me the hint, but I dont got it. Thanks Lerner for helping.

"I don't get it" isn't helpful. You need to be more specific. What don't you get? The point is that this function is inefficient:

int prime(int a)
{
 int i;
 for(i=2;i<=sqrt(a);i++) 
  if(a%i==0)
   return 0;
  else 
   continue;	
 return 1;
}

because it is checking whether 4, 6, 8, 10, etc. are prime by testing whether a % i is equal to 0. You don't need to test 4, 6, 8, 10, etc. at all since there is no way they can be prime since they are even and not equal to 2. So test whether a is equal to 2 at the top. If it is, return true. Then test whether a is even. If it is, a is not prime, so return false. After that, just test the odd values of i as Salem and Lerner suggested.


Lerner's code:

    for(i = 3; i <= sqrt(a); i += 2)

See the red text and compare it to your highlighted red text. Yours starts at 2 and checks EVERY number, including the even numbers. Lerner's tests for 3, 5, 7, 9, etc., but not 4, 6, 8, etc. since these are definitely not prime.

Also, note that Lerner checks for evenness of a before executing the loop above. the loop above executes only if a is not even.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 23
Reputation: vidit_X is an unknown quantity at this point 
Solved Threads: 0
vidit_X vidit_X is offline Offline
Newbie Poster

Re: Practise Problem, Pair of Prime Numbers

 
0
  #13
Sep 5th, 2008
Thanks VernonDozier for your concern. Actually after the Lerner's post, i realized what was Salem pointing at. "I dont got it" at the time when Salem gave me the hint, but after lerner's post it got clear. Anywayz, Thanks VernonDozier .
Last edited by vidit_X; Sep 5th, 2008 at 11:07 pm.
Past:Beginner
Present:Intermediate
Future:Pro

The Future is near, as Daniweb members are so helpful and talented.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 23
Reputation: vidit_X is an unknown quantity at this point 
Solved Threads: 0
vidit_X vidit_X is offline Offline
Newbie Poster

Re: Practise Problem, Pair of Prime Numbers

 
0
  #14
Sep 6th, 2008
When i compiled the above code in Visual C++, I got the following error
  1. error C2668: 'sqrt' : ambiguous call to overloaded function
Though I understood the error, but the same code gave no errors when i compiled it in DEV-CPP. So my question is why does different compilers give different errors(and some dont give that too)? Which one's ISO based? Does every compiler have different header files? Previously, i used Turbo C++ v3.0 but it doesn't supports new C++ code. Which compiler replaced Turbo C++?

A lot of questions
Past:Beginner
Present:Intermediate
Future:Pro

The Future is near, as Daniweb members are so helpful and talented.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Practise Problem, Pair of Prime Numbers

 
1
  #15
Sep 6th, 2008
Yes, they all give different errors.
But all ISO compilers should spot the same kinds of errors.

Another problem is you're computing sqrt(a) EVERY time around the loop, but the answer will not change from one iteration to the next.

> Does every compiler have different header files?
There's a standard set.
If your code only uses those headers, and is a correct program, then any ISO C++ compiler will be able to compile it.
Having multiple compilers on your machine is a great way of making sure that your code (and your knowledge) is really portable.

The best of the Borland compilers is probably this one
http://www.codegear.com/downloads/free/cppbuilder
Though they've recently started re-using the "Turbo" name for some new offerings as well. I don't know much about them yet.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 23
Reputation: vidit_X is an unknown quantity at this point 
Solved Threads: 0
vidit_X vidit_X is offline Offline
Newbie Poster

Re: Practise Problem, Pair of Prime Numbers

 
0
  #16
Sep 6th, 2008
Thanks Salem for your answers. But DEV-CPP compiled the code with no error, whereas with the same code Visual C++ gave the above error.
Last edited by vidit_X; Sep 6th, 2008 at 2:19 pm.
Past:Beginner
Present:Intermediate
Future:Pro

The Future is near, as Daniweb members are so helpful and talented.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Practise Problem, Pair of Prime Numbers

 
0
  #17
Sep 6th, 2008
cmath implements sqrt() for doubles, you pass an int.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 23
Reputation: vidit_X is an unknown quantity at this point 
Solved Threads: 0
vidit_X vidit_X is offline Offline
Newbie Poster

Re: Practise Problem, Pair of Prime Numbers

 
0
  #18
Sep 6th, 2008
cmath different in visual c++ and dev c++ ? Sorry, I know i ask a lot of questions but i'm Curious.
Last edited by vidit_X; Sep 6th, 2008 at 3:09 pm.
Past:Beginner
Present:Intermediate
Future:Pro

The Future is near, as Daniweb members are so helpful and talented.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,829
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Practise Problem, Pair of Prime Numbers

 
1
  #19
Sep 6th, 2008
Originally Posted by vidit_X View Post
cmath different in visual c++ and dev c++ ? Sorry, I know i ask a lot of questions but i'm Curious.
cmath is Standard, so it'll be the same everywhere. It's not compiler-specific and is thus portable (i.e. doesn't matter which compiler you use and which Operating System you use).

Same thing happened to me with this program. Dev C++ lets me get away with it, Visual C++ doesn't. Seems like a contradiction of the above statement. Basically I think Dev C++ lets you get away with something that is not technically legal, but if you write it the way it's supposed to be written, you're GUARANTEED that it will work. However, some things that are against the rules will work some of the time on some compilers, but you still shouldn't do it. I think that's the lesson. We just got lucky that we didn't get an error on Dev C++.

  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. using namespace std;
  5.  
  6.  
  7. int main ()
  8. {
  9. int a = 5;
  10. cout << sqrt (a) << endl;
  11. cin.get ();
  12. return 0;
  13. }

If you typecast a to a double in line 10 like this:
  1. cout << sqrt ((double) a) << endl;

it'll work on either compiler. I'm a little confused as to why myself. Maybe Dev C++ automatically typecasts it for you, but Visual C++ doesn't? Best thing is to probably typecast it to a double to remove all doubt that it may fail on some compilers. Note that sqrt (int) doesn't exist, as Salem mentioned, so it probably shouldn't be used :

http://www.cplusplus.com/reference/c...math/sqrt.html
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Practise Problem, Pair of Prime Numbers

 
0
  #20
Sep 6th, 2008
> cmath different in visual c++ and dev c++ ?
The contents which ISO state they should have should be the same, from a logical viewpoint anyway. The actual characters will surely be different.

The problems come in when you use a compiler specific extension in one, which isn't in the other. I don't have the VS compiler to try it to see what it's really complaining about.

Most good compilers have an "ANSI" or "ISO" mode which turns off all the extensions. But even then, it's not a totally foolproof way of making sure your code works on many compilers.

There is a free version of the ISO draft C++ standard on the web somewhere, but I can't seem to find it at the moment. It's definitely worth getting hold of if you want to write portable code.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC