to call the function prime, it is just to add:
prime(loop)
a simple loop condiction is:
(n/2)+1 > i
since after 1/2 of the val of n there cant be a fraction of the n number.
and the test are simple ennuf:
if (n%i == 0)
zyruz
Junior Poster in Training
60 posts since Jun 2005
Reputation Points: 10
Solved Threads: 5
To find if a number n is prime, you can just loop through all the numbers from 2 to (n - 1) and see if any of them divide evenly, using the modulus operator. So your loop ending condition could be (i < n), and you know that i divides into n if (n % i) equals 0.
This is a dumb algorithm, though. Instead, you can loop through all the numbers from 2 to the square root of n and test them. No need to go all the way up to (n - 1). Because if a number more than the square root of n divides into n, then a number less than the square root of n also works.
You might also want to try figuring out a method that doesn't use a prime testing function, which uses an array (maybe some other time).
Making the call to the function prime would be the same as making any function call, would it not? prime(loop).
Rashakil Fol
Super Senior Demiposter
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 177
Someone commented that to see if n is a prime you only have to check possible divisors < sqrt(n). In addition, you can decrease the computing time a lot by testing the divisor 2 , then 3, 5, 7..., skipping every other integer. The reason that this works is once you've determined that 2 is not a divisor you don't have to check 4, 6,8...or any even integer because if one of them were a divisor then so would 2 be a divisor. With a little ingenuity you can extend this to not testing multiples of 3 once you've determined that 3 is not a divisor, etc.
Actually, to produce a table of all the primes < 100000 (or, for that matter, of 1 million or ten million) there's a much better way called the sieve of Eritosthenes. (I think I spelled that all wrong). You can find it in many books. It's blindingly fast, using no divisions and a short code does the trick. Maybe this is what your instructor really wanted.
murschech
Junior Poster in Training
60 posts since Dec 2004
Reputation Points: 21
Solved Threads: 1
I think I read somewhere that to find out if a number is a prime, you dont have to check dividing upto n-1. Upto squareroot(n) is enough. check it out.
WolfPack
Postaholic
2,051 posts since Jun 2005
Reputation Points: 572
Solved Threads: 115
Your program doesn't make use of the efficiencies I mentioned. For one thing, you test possible factors up to valuex/2 but you only have to go up to sqrt(valuex), which is much smaller. (For example, sqrt(100) is 10, much less that 100/2.) Also you increment trial divisors by one rather than 2. Finally, once you find a divisor (and so, you know the number's not a prime), you contiinue to try more divisors!
Here's my version.
#include <iostream>
using namespace std;
bool isaprime(long);
bool isaprime(long x)
{ long n;
// is 2 a divisor?
if ( x%2 == 0)
return false;
n = 3;
while (n*n <=x)
{ if (x%n == 0)
return false;
n += 2;
}
return true;
}
int main()
{ long x;
while (1)
{ cout <<"Enter a positive integer to check for primality, 0 to exit: ";
cin >> x;
if (x == 0)
{ cout << "Thanks for the fun. Come again.\n";
return 1;
}
if (isaprime(x))
cout <<x<<" is a prime\n";
else
cout <<x<<" is not a prime\n";
}
}
<< moderator edit: fixed [code][/code] tags (forward slash) >>
murschech
Junior Poster in Training
60 posts since Dec 2004
Reputation Points: 21
Solved Threads: 1
this is a prime tester that uses the advises from post's here.
bool prime(int num)
{
// all numbers from 1 and lower aint primes.
if(num <= 1)
{
return false;
}
// 2 is a prime.
if(num == 2)
{
return true;
}
// is 2 a divisor?
if ( num%2 == 0)
{
return false;
}
for (int n=3;n*n <= num;n+=2)
{ if (num%n == 0)
return false;
}
// done testing, so return it as prime.
return true;
}
zyruz
Junior Poster in Training
60 posts since Jun 2005
Reputation Points: 10
Solved Threads: 5
If num < 4, how will it ever be equal to 4 for the switch? Might you have meant 0?
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
And the number 1 thing wrong with it is that the test function returns TRUE for non-primes and FALSE for primes!!!
Why the #define TRUE and FALSE when C++ has the perfectly good built in bool type, with values true and false ?
ps - you're responding/reopening a three year old thread, that saw a rebirth three weeks ago. Wasn't there enough discussion for you to find what you needed?
vmanes
Posting Virtuoso
1,914 posts since Aug 2007
Reputation Points: 1,268
Solved Threads: 228
THE ULTIMATE SOLUTION IS THIS
Only if you can't program... :icon_rolleyes:
WaltP
Posting Sage w/ dash of thyme
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944