| | |
isPrime()
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: May 2006
Posts: 25
Reputation:
Solved Threads: 1
Here's what I've come up with so far:
C++ Syntax (Toggle Plain Text)
#include <iostream> bool IsPrime(int num) { if(num == 0) return true; num = abs(num); for(int i = 2; i <= sqrt(num); i++) if(num % i == 0) return false; return true; }
Last edited by ~s.o.s~; Jul 7th, 2007 at 3:38 am. Reason: Added code tags, learn to use them.
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
>Here's what I've come up with so far
Somehow I find it hard to believe that you can write IsPrime but not the main driver to print the primes in the range [0..100). I'm guessing you were given IsPrime and told to write main, which means you've come up with nothing so far. But, the hard part is done already. All you need to do is loop from 0 to 100 and call IsPrime on the current number. If it returns true, print the number:
Somehow I find it hard to believe that you can write IsPrime but not the main driver to print the primes in the range [0..100). I'm guessing you were given IsPrime and told to write main, which means you've come up with nothing so far. But, the hard part is done already. All you need to do is loop from 0 to 100 and call IsPrime on the current number. If it returns true, print the number:
C++ Syntax (Toggle Plain Text)
for ( int i = 0; i < 100; i++ ) { if ( IsPrime ( i ) ) cout<< i <<" is prime\n"; }
I'm here to prove you wrong.
•
•
•
•
>Here's what I've come up with so far
Somehow I find it hard to believe that you can write IsPrime but not the main driver to print the primes in the range [0..100). I'm guessing you were given IsPrime and told to write main, which means you've come up with nothing so far.
[/code]
If laziness is truely the mother of invention, we have quite a group of potential inventors here!
![]() |
Similar Threads
- An isprime(n) function (Python)
- Recursive prime number f(x) (C)
- I occasionally get hijacked when browsing. (Viruses, Spyware and other Nasties)
Other Threads in the C++ Forum
- Previous Thread: compiling using g++ results to undefined reference
- Next Thread: user given array size
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion convert count data database delete desktop developer directshow dll download dynamic encryption error file forms fstream function functions game generator getline givemetehcodez google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory news node number output parameter pointer problem program programming project proxy python random read recursion recursive return string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






