Determining Prime Numbers in C
To ascertain whether a given integer is prime, let's start by outlining the steps involved:
Now, let's translate this algorithm into C code:
int IsPrime(unsigned int number) { if (number <= 1) return 0; // Handle special cases unsigned int i; for (i = 2; i*i <= number; i++) { if (number % i == 0) return 0; // Number has divisors } return 1; // No divisors found, number is prime }
This function follows the outlined steps:
While this method is not optimized for performance, it provides a clear and comprehensible solution for determining prime numbers in C.
The above is the detailed content of How to Determine if a Number is Prime in C?. For more information, please follow other related articles on the PHP Chinese website!