2009-01-18, 08:29 PM
(This post was last modified: 2009-01-19, 03:21 PM by KajitiSouls.)
Stereo Wrote:Logarithms in turn would use iteration most likely though, they're another non basic operation.
Prestoring the value would work except I think its being called a lot of times and would still have to calculate the more complicated sqrt instead of just ^2 every time.
For the purposes of the isPrime() function, calculating a pre-stored square of the number being indexed doesn't work. Pre-stored variables are a good choice only on the static limits of a loop, and assuming said loops aren't extremely short in duration.
For the function isPrime(int n):
Code:
if(preliminary tests failed) return false;
int upperLimit = sqrt(n);
for(int factor = x; factor <= upperLimit; increment factor by f(y)){
if(n % factor == 0) return false;
}
return true;Code:
if(preliminary tests failed) return false;
for(int factor = x; factor*factor <= n; increment factor by f(y)){
if(n % factor == 0) return false;
}
return true;Pre-storing the sqrt of n is noticeably faster than calculating the square of a number when used in the final algorithm I posted.
EDIT: What's the difference in Run Time and Real Time? (referring to DS's picture)
EDIT2: Thoughts confirmed xD

