What is a Prime Number?
Approach 1: Basic Trial Division Method.
- Check if the input number is divisible by "i" (i.e., input number % i == 0).
- If it is divisible, the input number is not a prime number. Return "Not Prime."
//C Program to check prime number #include <stdio.h> #include <stdbool.h> #include <math.h> bool is_prime_basic(int num) { // Numbers less than or equal to 1 are not prime if (num <= 1) { return false; } int sqrt_num = (int)sqrt(num); for (int i = 2; i <= sqrt_num; i++) { // Found a divisor, not a prime number if (num % i == 0) { return false; } } // No divisors found, the number is prime return true; } int main() { int number; printf("Enter a positive integer: "); scanf("%d", &number); if (is_prime_basic(number)) { printf("%d is a prime number.\n", number); } else { printf("%d is not a prime number.\n", number); } return 0; }
Enter a positive integer: 5
5 is a prime number.
Approach 2: Optimized Trial Division Method.
- If the input number is less than or equal to 1, it is not a prime number.
- If the input number is 2 or 3, it is a prime number. Return "Prime."
- For each value of "i" in the loop, it checks if the input number is divisible by "i" or "i + 2" using the modulo operator (%).
- If the input number is divisible by "i" or "i + 2," it means the number is not a prime, and the function returns "false."
//C Program to check if a number is prime or not #include <stdio.h> #include <stdbool.h> #include <math.h> bool is_prime_optimized(int num) { // Numbers less than or equal to 1 are not prime if (num <= 1) { return false; } // 2 and 3 are prime numbers if (num <= 3) { return true; } // Numbers divisible by 2 or 3 are not prime if (num % 2 == 0 || num % 3 == 0) { return false; } int sqrt_num = (int)sqrt(num); for (int i = 5; i <= sqrt_num; i += 6) { if (num % i == 0 || num % (i + 2) == 0) { return false; // Found a divisor, not a prime number } } // No divisors found, the number is prime return true; } int main() { int number; printf("Enter a positive integer: "); scanf("%d", &number); if (is_prime_optimized(number)) { printf("%d is a prime number.\n", number); } else { printf("%d is not a prime number.\n", number); } return 0; }
Enter a positive integer: 17
17 is a prime number.
Approach 3: Check Prime Number using Wilson’s Theorem.
- The factorial function multiplies all integers from 2 to "num - 1" to calculate the factorial value.
- If (num - 1)! + 1 is congruent to 0 modulo "num," then "num" satisfies Wilson's Theorem, and it is a prime number. Return true.
//C Program to check prime number using Wilson’s Theorem #include <stdio.h> // Function to calculate the factorial of a number int factorial(int num) { // Factorial of 0 and 1 is 1 if (num == 0 || num == 1) { return 1; } int result = 1; // Calculate the factorial for (int i = 2; i <= num; i++) { result *= i; } return result; } // Function to check if a number is prime using Wilson's Theorem int is_prime_wilson(int num) { // Numbers less than or equal to 1 are not prime if (num <= 1) { return 0; } // Check if (num - 1)! ≡ -1 (mod num) if ((factorial(num - 1) + 1) % num == 0) { return 1; // The number satisfies Wilson's Theorem, it is prime } else { return 0; // The number does not satisfy Wilson's Theorem, it is not prime } } int main() { int number; printf("Enter a positive integer: "); scanf("%d", &number); if (is_prime_wilson(number)) { printf("%d is a prime number.\n", number); } else { printf("%d is not a prime number.\n", number); } return 0; }
Enter a positive integer: 7
7 is a prime number.
Note: Wilson's Theorem is not practical for large numbers as the factorial operation becomes computationally expensive.(alert-passed)

