No account? Create an account # gah

August 4th, 2001 (02:53 pm)

wanted: simple c primality test

prime?() should return 0 if composite and 1 if prime -- I'm trying to plot this:

x_(n+1)=cos(y_(n) + 2( prime?(floor(128 * (sin ((e^(x_(n)) - (e^(y_n))))-1);
y_(n+1)=sin(x_(n) + 2( prime?(floor(128 * (sin ((e^(y_(n)) - (e^(x_n))))-1);

 Posted by: the coproduct of doom () Posted at: August 5th, 2001 07:23 am (UTC) My current plan is to test 0 to 128, drop that
in an array, and then just check if there's
a zero or one in it during the main loop.

 Posted at: August 5th, 2001 10:57 am (UTC) Prime Directive

Well, since nobody else has responded, the simplest one I can think of is to set up a loop from 2 to N/2(rounded up), dividing N by the index every time. If you exhaust the loop without ever getting a perfect division, it's prime.

A little less simple (But faster) is to decrease the maximum number every time through the loop:

Test if 2 divides N evenly. If not, check and see if 3 > N/2. If so, N's prime. Otherwise,

Test if 3 divides N evenly. If not, check and see if 4 > N/3. If so, N's prime. Otherwise...

And so on, so forth. It's essentially the same as the first test, but you're not testing nearly as many numbers. (I'm pretty sure that's the complete method, but I'm a little groggy still so you might want to test it once or twice.) 