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.

#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;
 }
 }}

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

i have to define a function that tests number from 2 to 10000 to see if they are prime.

for (i =2; i <= 500; i++)

but it only tests prime numbers until about 500 or so

That's what you tell it to do.

then it gets most of the numbers to be prime but it still includes non prime numbers for some reason. ANy ideaS?

You might try Google to see if there are any other algorithms to compare yours to.

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

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;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.