Write a function to check if a number is prime. with explanation
Write a function to check if a number is prime. with explanation
Ravi Vishwakarma is a dedicated Software Developer with a passion for crafting efficient and innovative solutions. With a keen eye for detail and years of experience, he excels in developing robust software systems that meet client needs. His expertise spans across multiple programming languages and technologies, making him a valuable asset in any software development project.
Anubhav Kumar
02-Sep-2025Prime Number Reminder:
2, 3, 5, 7, 11, 13are prime.4, 6, 8, 9, 10are not prime because they can be divided by numbers other than 1 and themselves.Python Program
Explanation
Base Case:
0,1, and negative numbers are not prime.Check divisibility:
√ninstead ofn-1.nhas a factor larger than√n, then the corresponding smaller factor would already have been found.Check modulus:
ndivides evenly byi, then it’s not prime.Return True if no divisors found:
Output :