Prime Number Checker in Python - Simple Function
This Snippet is coded by a user inOnline python Compiler. You can see and run this code too.
زبان: python
This code defines a function is_prime(num) that checks whether a given number is prime. It works by counting divisors: it loops from 1 to the number and increments a counter each time the number is divisible by i. If the total count equals exactly 2 (meaning only 1 and the number itself divide it), the function returns True; otherwise, it returns False.
To use it, simply call the function with your desired integer. The end of the code demonstrates the output for 5, 12, 37, 93, and 1567. Note that this method is straightforward but inefficient; for large numbers it can be slow. An optimization is to loop only up to the square root of the number.