How do I check if a number is a prime? - Printable Version
+- SA-MP Forums Archive (
https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (
https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (
https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: How do I check if a number is a prime? (
/showthread.php?tid=263517)
How do I check if a number is a prime? -
linuxthefish - 22.06.2011
How do I check if a number is a prime? I'm trying to create a lotto system.
Thanks.
Re: How do I check if a number is a prime? - Double-O-Seven - 22.06.2011
Assume X is the number for which you want to know if it's prime.
Naive: Check for all numbers from 2 up to the square root of X if they divide X.
A number Y divides X when X % Y == 0. (X is equivalent to 0 modulo Y)
However, this is a not very efficient algorithm.
There are better ones.
Re: How do I check if a number is a prime? -
linuxthefish - 22.06.2011
Thanks, I was never good at math/s!
Re: How do I check if a number is a prime? -
linuxthefish - 29.08.2011
I've found a nice example from the PAWN pdf that i converted to work with normal pawn...
pawn Code:
#include <a_samp>
/* Print all primes below 100, using the "Sieve of Eratosthenes" */
main()
{
new max_primes = 100;
new series[max_primes] = { true, ... };
for (new i = 2; i < max_primes; ++i)
if (series[i])
{
printf("%d", i);
/* filter all multiples of this "prime" from the list */
for (new j = 2 * i; j < max_primes; j += i)
series[j] = false;
}
}