Python Program to Check Prime Number
Given a positive integer, check if the number is prime or not. A prime is a natural number greater than 1 that has no positive divisors other than 1 and itself. Examples of first few prime numbers are {2, 3, 5,
Examples:
Input: n = 11 Output: true Input: n = 15 Output: false Input: n = 1 Output: false
School Method :
# A school method based Python3 # program to check if a number # is prime def isPrime(n): # Corner case if n <= 1: return False # Check from 2 to n-1 for i in range(2, n): if n % i == 0: return False; return True # Driver Program to test above function print("true") if isPrime(11) else print("false") print("true") if isPrime(14) else print("false") # This code is contributed by Smitha Dinesh Semwal |
chevron_right
filter_none
Output:
true false
Time complexity of this solution is O(n)
Optimized School Method :
# A optimized school method based # Python3 program to check # if a number is prime def isPrime(n) : # Corner cases if (n <= 1) : return False if (n <= 3) : return True # This is checked so that we can skip # middle five numbers in below loop if (n % 2 == 0 or n % 3 == 0) : return False i = 5 while(i * i <= n) : if (n % i == 0 or n % (i + 2) == 0) : return False i = i + 6 return True # Driver Program if(isPrime(11)) : print(" true") else : print(" false") if(isPrime(15)) : print(" true") else : print(" false") # This code is contributed # by Nikita Tiwari. |
chevron_right
filter_none
Output:
true false
Please refer complete article on Primality Test | Set 1 (Introduction and School Method) for more details!
Recommended Posts:
- Python Program to print digit pattern
- Python | Extract key-value of dictionary in variables
- Python program to convert time from 12 hour to 24 hour format
- Python | Animated Banner showing 'GeeksForGeeks'
- Python | Segregate list elements by Suffix
- Python Program for Selection Sort
- Python Program for Bubble Sort
- Python Program for Insertion Sort
- Python Program for Heap Sort
- Python Program for Counting Sort
- Python Program for Radix Sort
- Python Program for n-th Fibonacci number
- Python Program for Counting Sort
- Python Program for ShellSort
- Python Program for Longest Common Subsequence



