Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Algorithm writing tips?
#23
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
Reply


Messages In This Thread
Algorithm writing tips? - by KajitiSouls - 2009-01-11, 10:40 PM
Algorithm writing tips? - by Stereo - 2009-01-11, 10:58 PM
Algorithm writing tips? - by KajitiSouls - 2009-01-11, 11:36 PM
Algorithm writing tips? - by Nikkey - 2009-01-12, 09:58 AM
Algorithm writing tips? - by Stereo - 2009-01-12, 11:52 AM
Algorithm writing tips? - by KajitiSouls - 2009-01-12, 03:53 PM
Algorithm writing tips? - by Nikkey - 2009-01-12, 04:19 PM
Algorithm writing tips? - by Stereo - 2009-01-12, 04:50 PM
Algorithm writing tips? - by KajitiSouls - 2009-01-12, 05:25 PM
Algorithm writing tips? - by Nikkey - 2009-01-12, 05:59 PM
Algorithm writing tips? - by GummyBear - 2009-01-12, 08:13 PM
Algorithm writing tips? - by KajitiSouls - 2009-01-12, 10:26 PM
Algorithm writing tips? - by Nikkey - 2009-01-13, 10:28 AM
Algorithm writing tips? - by GummyBear - 2009-01-13, 11:41 PM
Algorithm writing tips? - by Stereo - 2009-01-15, 10:38 AM
Algorithm writing tips? - by Nikkey - 2009-01-15, 11:02 AM
Algorithm writing tips? - by Kiley - 2009-01-16, 02:37 AM
Algorithm writing tips? - by KajitiSouls - 2009-01-18, 01:48 AM
Algorithm writing tips? - by Stereo - 2009-01-18, 04:46 PM
Algorithm writing tips? - by KajitiSouls - 2009-01-18, 05:37 PM
Algorithm writing tips? - by Nikkey - 2009-01-18, 06:26 PM
Algorithm writing tips? - by Stereo - 2009-01-18, 08:02 PM
Algorithm writing tips? - by KajitiSouls - 2009-01-18, 08:29 PM
Algorithm writing tips? - by Stereo - 2009-01-18, 10:45 PM
Algorithm writing tips? - by Nikkey - 2009-01-19, 02:10 AM

Forum Jump:


Users browsing this thread: 1 Guest(s)