Monday, 12 March 2018

C function to check whether a given number is prime or not

int prime(unsigned long long p)
{
    unsigned long long int i;
     if (p <= 1)  return 0;
    if (p <= 3)  return 1;
    if (p%2 == 0 || p%3 == 0) return 0;long int l=sqrt(p);
    for ( i=5; i<=l;i=i+6)
        if (p%i == 0 || p%(i+2)==0)
           return 0;

    return 1;
}

No comments:

Post a Comment