add to list (identifying whether input is a prime number) - python

Trying to write a function 'prime_check(number)' to test if an argument is a prime number
The number_of_factors for a given number should only be 1 (the given number itself) and '1', which is already excluded in the while statement
Having trouble adding a variable to the list "set_of_factors"
Tried to debug in Thonny, which says that set_of_factors = none
def prime_checker(number):
set_of_factors=[]
number_of_factors=len(set_of_factors)
f=2
while number > 1 and f<=number:
if number%f == 0:
set_of_factors.insert(0,1)
f+=1
else:
f+=1
if number_of_factors==1:
print("It is a prime number")
else:
print("It is not a prime number")
n = int(input("Check this number: "))
prime_checker(number=n)

Prime numbers have two factors, one and itself. Ex. 5's factors would be (1, 5)
Also istead if ".insert" use ".append"
def prime_checker(number):
set_of_factors=[]
number_of_factors=len(set_of_factors)
f=2
while number > 1 and f<=number:
if number%f == 0:
set_of_factors.insert(0)
f+=1
else:
f+=1
if number_of_factors==2:
print("It is a prime number")
else:
print("It is not a prime number")

Related

Checking Odd Perfect Square as Prime Number in Python [duplicate]

This question already has answers here:
Prime numbers multiplied by 3 shown as prime numbers
(3 answers)
Closed 9 months ago.
I have tried this very basic code for checking prime numbers in python and it is working fine until you enter odd perfect squares like 9, 25, 121 etc. It results in saying that these are prime numbers. So what is the issue?
def isPrime(a):
nums = range(2,a)
for num in nums:
if (a % num) == 0:
return False
else:
return True
print('Enter an integer to check if it is prime')
a = input()
a = int(a)
if isPrime(a):
print('The number is prime')
else:
print('The number is not prime')
You can use %2 sign to determine.
Only 2 and 1 does not fit the criteria and thus need to have their own specific function since 2 is a prime number and 1 is not a prime number.
a = input("Input number: ")
a = int(a)
if a == 2:
print('The number is prime')
elif a ==1:
print("The number is not prime")
elif a%2 == 0:
print("The numbr is not prime")
else:
print('The number is prime')

Prime number in Python

#Write your code below this line ๐Ÿ‘‡
def prime_checker(number):
for num in range (2, number):
if num % number == 0:
print("It is not a prime number")
else:
print("It is a prime number")
#Write your code above this line ๐Ÿ‘†
#Do NOT change any of the code below๐Ÿ‘‡
n = int(input("Check this number: "))
prime_checker(number=n)
How can I print a text that number is prime or not only once?
Fix
number % num == 0 and not num % number == 0
A number isn't prime the moment you find a number that doesn't divide it, but only when you have tested all and none divides it
Use for/else construction, it goes into else if no break has been used
def prime_checker(number):
for num in range(2, number):
if number % num == 0:
print("It is not a prime number")
break
else:
print("It is a prime number")
Note that this only fixes your way to do, but that isn't the optimal way to check if a numbre is a prime one, at least, ending range at square root of number, and directly verifying division by small numbers like 2,3,5,7
There are few mistakes in your code. I have modified them.
def prime_checker(number):
for num in range(2, number):
if number % num == 0:
print('Not prime')
return
print('Prime number')
# Write your code above this line ๐Ÿ‘†
# Do NOT change any of the code below๐Ÿ‘‡
n = int(input("Check this number: "))
prime_checker(number=n)
For loop is to check if any of the number starting from 2 is a factor of number or not.
First, a slightly more efficient prime check by
going until the sqrt of the number only
going in steps of 2
import math
def is_prime(n: int) -> bool:
if n in (2, 3, 5):
return True
if n < 2 or n % 2 == 0:
return False
for i in range(3, math.ceil(math.sqrt(n)), 2):
if n % i == 0:
return False
return True
Now you can wrap that function in yours
def prime_checker(n: int):
msg = "%d is prime" if is_prime(n) else "%d is not prime"
print(msg % n)
prime_checker(11)
# 11 is prime
I think it's good to check a number is prime or not
def is_prime(n):
st = "prime" # being prime status
for i in range(2,n):
if n % i == 0: # if number is prime
st = "not prime"
break;
return st
n = int(input("enter n: "))
print (is_prime(n))
You only need to change the end of your code
your function is true except for these lines:
if num % number == 0:
print("It is not a prime number")
else:
print("It is a prime number")
you should change these to:
st = "Prime"
if number % num == 0:
st = "not prime"
return st

Why shouldn't I put the "else" same level with "if" in Python

When I put else the same level with if, the program didn't work when i called my function. (little star in jupyter notebook). But when I pulled "else" one level behind, everything was ok. The function is meant to return the number of prime numbers that exist up to and including a given number. Here is my code :
def count_primes(num):
primes = list(range (3,num,2))
digit = 3
if num < 2:
return 0
while digit <= num:
for x in range (3, digit, 2):
if digit % x == 0:
primes.remove(digit)
digit += 2
break
else:
digit += 2
primes.insert(0,2)
print (primes)
return len(primes)
It is because you do not want to get the else in the for loop. You just need to return the prime numbers once.
For example if I make the program just to check whether a number is prime or not -
if num > 1:
# check for factors
for i in range(2,num):
if (num % i) == 0:
print(num,"is not a prime number")
print(i,"times",num//i,"is",num)
break
else:
print(num,"is a prime number")if num > 1:
# check for factors
for i in range(2,num):
if (num % i) == 0:
print(num,"is not a prime number")
print(i,"times",num//i,"is",num)
break
else:
print(num,"is a prime number")
PS: I have not included if the number is one or less.
I do not want to print Number is not prime several times. Because if I put else inside the loop, it will run every time if fails.

Python - Prime check and increment

My objective of the below code is
check if entered number is a prime
if not print the next biggest prime
def primetest (num):
for c in range (2, num):
if num % c == 0:
repeattest (num) #not prime? increment number
else :
print (num,"is a prime number")
break
def repeattest (num): # check prime if not increment number by 1
for z in range (2, num):
num = num+1
primetest (num)
if num % z == 0:
num = num+1
else:
print ("Next Prime:", num+1)
break
num = int (input ("enter a number:")) # main code:
for y in range (2, num):
if num % y == 0:
repeattest (num)
else:
print (num,"is a prime number")
break
I think the logic is fine, but not sure why im not getting any output.
Time comlexity of your code is O(N) when it find a number which is prime or not.
There is no pointing on dividing from 2 to len(num)-1. It is enough to loop from 2 to sqrt of the given number. Therefore time complexity reduce to O(n) to O(log(n)).
import math
num = int (input ("enter a number:"))
def primeTest(num):
isPrime = 0
for i in range(2,int(math.sqrt(num)+1)):
if num%i == 0:
isPrime = isPrime + 1
break
if isPrime == 0:
print(num, "is a prime number")
else:
num = num + 1
repeatTest(num)
def repeatTest (num):
isPrime = 0
for i in range(2,int(math.sqrt(num))):
if num%i == 0:
isPrime = isPrime + 1
break
if isPrime == 0:
print("Next Prime: ", num)
else:
num = num + 1
repeatTest(num)
primeTest(num)
The logic which you are using to find if a number if prime seems wrong .
Taking a integer like 9 prints "9 is a prime number" .
And also you are checking for next prime numbers from 2 to Num .
Num being the input , you cant get a number greater than that .
It exits from the loop without even getting in , therefore not printing anything when you are searching for next prime .
You need to change the logic .
write a separate function for checking prime and end the loop when you find the next prime number instead of stopping at num .

Python prime function problems

I am very new to python; I only started two days ago. After looking at some designs online, i tried to use this function to determine if a number is prime:
def isprime(n):
if n == 1:
print("1 is not a prime.")
if n == 2:
print("2 is a prime number.")
for x in range(2, int(n)):
if n%x == 0:
print(n, "is a composite number.")
break
else:
print(n, "is a prime number.")
break
isprime(int(input("Enter a number.")))
The isprime(int(input("Enter a number."))) is so I can run the file as an .py.
However, while it runs without any errors, it returns false positives. For example, it states that 45 is a prime number. What did i do wrong?
You're aborting iteration of your for x in range loop after the first iteration, because both branches of your if-statement contain a break.
Remove the else-branch and use a for...else construct:
def isprime(n):
if n == 1:
print("1 is not a prime.")
if n == 2:
print("2 is a prime number.")
for x in range(2, int(n)):
if n%x == 0:
print(n, "is a composite number.")
break
else:
print(n, "is a prime number.")
The problem is in the logic of your code. Indentation plays an important role in Python. The code works good if it is changed like this:
def isprime(n):
if n == 1:
print("1 is not a prime.")
if n == 2:
print("2 is a prime number.")
prime = True
for x in range(2, int(n)):
if n%x == 0:
print(n, "is a composite number.")
prime = False
break
if prime:
print(n, "is a prime number.")

Categories