| | |
Practise Problem, Pair of Prime Numbers
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jan 2008
Posts: 3,829
Reputation:
Solved Threads: 501
•
•
•
•
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. •
•
Join Date: Aug 2008
Posts: 23
Reputation:
Solved Threads: 0
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 .
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.
Present:Intermediate
Future:Pro
The Future is near, as Daniweb members are so helpful and talented.
•
•
Join Date: Aug 2008
Posts: 23
Reputation:
Solved Threads: 0
When i compiled the above code in Visual C++, I got the following error 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
C++ Syntax (Toggle Plain Text)
error C2668: 'sqrt' : ambiguous call to overloaded function
A lot of questions
Past:Beginner
Present:Intermediate
Future:Pro
The Future is near, as Daniweb members are so helpful and talented.
Present:Intermediate
Future:Pro
The Future is near, as Daniweb members are so helpful and talented.
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.
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.
•
•
Join Date: Jan 2008
Posts: 3,829
Reputation:
Solved Threads: 501
•
•
•
•
cmath different in visual c++ and dev c++ ? Sorry, I know i ask a lot of questions but i'm Curious.
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++.
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <cmath> using namespace std; int main () { int a = 5; cout << sqrt (a) << endl; cin.get (); return 0; }
If you typecast
a to a double in line 10 like this: C++ Syntax (Toggle Plain Text)
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
> 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.
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.
![]() |
Other Threads in the C++ Forum
- Previous Thread: building Mysql on win XP Microsoft Visual Studio 9.0
- Next Thread: C vs C++ for a small application
| Thread Tools | Search this Thread |
api array arrays based beginner binary c++ c/c++ calculator char class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive return simple string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets




Thanks Lerner for helping. 

