Answers for "prime no. program in python"

7

determine if number is prime python

# Time Efficient Primality Check in Python

def primeCheck(n):
    # 0, 1, even numbers greater than 2 are NOT PRIME
    if n==1 or n==0 or (n % 2 == 0 and n > 2):
        return "Not prime"
    else:
        # Not prime if divisable by another number less
        # or equal to the square root of itself.
        # n**(1/2) returns square root of n
        for i in range(3, int(n**(1/2))+1, 2):
            if n%i == 0:
                return "Not prime"
        return "Prime"
Posted by: Guest on September-17-2020
0

primes in python

from math import sqrt
for i in range(2, int(sqrt(num)) + 1):
    if num % i == 0:
        print("Not Prime")
        break
    print("Prime")

# Note: Use this if your num is big (ex. 10000 or bigger) for efficiency
# The result is still the same if the num is smaller
Posted by: Guest on August-13-2021

Python Answers by Framework

Browse Popular Code Answers by Language