prim numbers-output

Pages: 12
Thats ok. Though personally I would still put the sqrt() call inside the Prime() function, but place it before the start of the for loop.

I messed around with the code here quite a lot, in order to avoid checking all the even numbers too. It looks a bit of a mess, I don't know whether it can be simplified further.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
bool Prime (unsigned long st)
{
    if (st<2)
        return false;
    if (st == 2)
        return true;
    if (st%2 == 0)
        return false;
    unsigned long  limit = sqrt(double(st));
    for (unsigned long  i = 3; i <= limit; i+=2)
        if (st % i == 0)
            return false;
                    
    return true;
}
Topic archived. No new replies allowed.
Pages: 12