Answers for "python prime check"

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

python prime check

def isPrime(n):
  if n<2:		#1, 0 and all negative numbers are not prime
    return False
  elif n==2:	#2 is prime but cannot be calculated with the formula below becuase of the range function
    return True
  else:
    for i in range(2, n):
      if (n % i) == 0:	#if you can precisely divide a number by another number, it is not prime
        return False
    return True			#if the progam dont return False and arrives here, it means it has checked all the numebrs smaller than n and nono of them divides n. So n is prime
Posted by: Guest on March-26-2021
1

python prime factors

# There is no quick way to calculate the prime factors of a number.
# In fact, prime factorization is so famously hard that it's what puts the "asymmetric" in asymmetric RSA encryption.
# That being said, it can be sped up a little bit by using divisibility rules, like checking if the sum of the digits is divisible by 3.

def factors(num):
        ps = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149] # Primes from https://primes.utm.edu/lists/small/10000.txt. Primes can also be generated by iterating through numbers and checking for factors, or by using a probabilistic test like Rabin-Miller.
        pdict = {}
        for p in ps:
                if p <= num:
                        while (num / p).is_integer():
                                if str(p) in pdict:
                                        pdict[str(p)] += 1
                                else:
                                        pdict[str(p)] = 1
                                num /= p
                if num == 1: break
        return pdict

# Returns a dictionary in the form {"base": "exponent"}
Posted by: Guest on July-05-2020
2

prime checker in python

#make the function
#to do this all hte vairibles go in side the function

def CheckIfPrime ():
    a1 = input("which number do you want to check")
    a = int(a1)#you need the checking number as an int not an str
    b = 2 #the number to check againts
    c = ("yes")
    while b < a:#run the loop
        if a%b == 0:#check if the division has a remainder
            c = ("no")#set the answer
        b = b+1
    print(c)#print the output
CheckIfPrime ()#call the function
Posted by: Guest on August-30-2020
0

python prime checker

# Program to check if a number is prime or not

num = 29

# To take input from the user
#num = int(input("Enter a number: "))

# define a flag variable
flag = False

# prime numbers are greater than 1
if num > 1:
    # check for factors
    for i in range(2, num):
        if (num % i) == 0:
            # if factor is found, set flag to True
            flag = True
            # break out of loop
            break

# check if flag is True
if flag:
    print(num, "is not a prime number")
else:
    print(num, "is a prime number")
Posted by: Guest on April-27-2021

Python Answers by Framework

Browse Popular Code Answers by Language