| | |
prime numbers homework trouble
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Sep 2004
Posts: 5
Reputation:
Solved Threads: 0
i have to define a function that tests number from 2 to 10000 to see if they are prime.
heres what i have so far.
but it only tests prime numbers until about 500 or so then it gets most of the numbers to be prime but it still includes non prime numbers for some reason. ANy ideaS?
thanks
heres what i have so far.
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <iomanip> using namespace std; bool find_prime(int); int main () { int i, counter =0; bool test; for (i =2; i <= 500; i++) { test = find_prime(i); if ( test == true ) { counter++; cout << setw(6) << i << "\t"; } } cout << "\nThe total number of the prime numbers between 2 and " << i << " is " << counter << endl; return 0; } bool find_prime (int m) { int temp, j; for ( j = 2; j < m - 1 ; j++ ) { temp = m % j; if (temp == 0) { return false; break; } else { return true; break; } }}
thanks
•
•
•
•
Originally Posted by bluegoo06
i have to define a function that tests number from 2 to 10000 to see if they are prime.but it only tests prime numbers until about 500 or sofor (i =2; i <= 500; i++)
•
•
•
•
Originally Posted by bluegoo06
then it gets most of the numbers to be prime but it still includes non prime numbers for some reason. ANy ideaS?
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
•
•
Join Date: Feb 2005
Posts: 2
Reputation:
Solved Threads: 0
Hi,
1) The "break" statement after the return does not have any effect
2) I think the logic of the program is incorrect and that may be the reason why the program returns non primes. Can u try the following code. I havent checked it but I think it should work
rgds
Sam
1) The "break" statement after the return does not have any effect
2) I think the logic of the program is incorrect and that may be the reason why the program returns non primes. Can u try the following code. I havent checked it but I think it should work
rgds
Sam
C++ Syntax (Toggle Plain Text)
bool find_prime (int m) { int temp, j; for ( j = 2; j < m - 1 ; j++ ) { temp = m % j; if (temp == 0) { return false; } } return true; }
![]() |
Similar Threads
- finding prime numbers that add up to even number? (C)
- Prime Numbers (Python)
- Help to find prime numbers without using an array (Java)
- Finding Prime numbers without using Boolean (C++)
- Prime Numbers (C)
- prime numbers (C++)
- prime numbers (C++)
Other Threads in the C++ Forum
- Previous Thread: c++ very new at
- Next Thread: Inheritance & Derived Classes
| Thread Tools | Search this Thread |
api array based beginner bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion count database delete deploy desktop developer directshow dll download dynamic email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number output parameter pointer problem program programming project python random read recursion recursive return sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






