How do I check if a number is a prime?
#1

How do I check if a number is a prime? I'm trying to create a lotto system.

Thanks.
Reply
#2

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.
Reply
#3

Thanks, I was never good at math/s!
Reply
#4

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;
        }
}
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)