1

I have been trying to make a prime number checker in python and it doesn't seem to work. Can anyone tell me what is the problem with my code? Thank you!

num=int(input())
if num==1:
    print("Neither")
if num==2:
  print("Prime")
for i in range(2,num):
  if num!=1 and num!2:
    if num%i==0:
      print("Composite")
      break
    if num%i!=0:
      print("Prime")
      break
shark
  • 11
  • 1
  • 1
    Does this answer your question? [Python Prime number checker](https://stackoverflow.com/questions/18833759/python-prime-number-checker) – mhhabib Apr 21 '21 at 04:27
  • yup ..you can use above comments to get your answer ..Also you may want to check few more condition like negative numbers, floats etc – Deepak Apr 21 '21 at 04:40

5 Answers5

1

It's best to make it a function but this will do with little modification to you code.

num=int(input("enter"))
is_prime = True
if num==1:
    print("Neither")
if num==2:
  print("Prime")
else:
    for i in range(2,num):
        if num%i==0:
            is_prime = False
      
if num!=1 and num!=2:
    if is_prime:
        print("prime")
    else:
        print("composite")
kidus solomon
  • 23
  • 1
  • 7
0

I made small changes to your code ..check if it gives you desired output

num=int(input())
if num==1:
    print("Neither")
if num==2:
    print("Prime")
for i in range(2,num):
    if num!=1 and num!=2:
        if num%i==0:
            print("Composite")
            break
    if num%i!=0:
        print("Prime")
        break

Also you may not want to write a custom function ...instead you can use sympy library....

You need to pip install sympy first

pip install sympy

import sympy
print(sympy.isprime(5.5))  
Deepak
  • 470
  • 1
  • 3
  • 15
0

First off, I would make this as a function so it's easier to call and test.

Second issue is when checking num%i!=0. You need to check all of the numbers in the for loop before you can say its prime. Try something like this:

def is_prime(num):
    if num == 1:
        return "Neither"

    if num == 2:
        return "Prime"

    for i in range(2, num):
        if num % i == 0:
            return "Composite"

    # You finished the loop without hitting the
    # return statement, so it must be prime!
    return "Prime"

num = int(input())
print(is_prime(num))
flakes
  • 21,558
  • 8
  • 41
  • 88
0

Your code doesn't really work. Here is a working version.

num = int(input("Enter a number: "))  
  
if num > 1:  
   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")  
         
else:  
   print(num,"is not a prime number")  
  • Your code works well, thank you! I would like to ask if there is any way to simplify it. – shark Apr 21 '21 at 04:37
  • @shark To be honest, I doubt it. I've tried to make it follow the simplest 'code flowchart', so simplifying it would be a matter of trying to find whether it's a prime in a completely different way. – Sergei Kiselev Apr 21 '21 at 04:41
0

Most of the other answers have given an explanation for how to correct your code. However, since we're on the topic of primes, I wanted to give my alternative solution.

Instead of checking all the numbers from 2 to n, you only have to check from 2 to the square root of n (n**0.5). This is because any factor of n greater than n**0.5 must be paired with a factor less than n**0.5.

def prime(n):
    n = int(n)
    assert n > 0, "n must be a positive integer."
    assert n != 1, "1 is neither prime nor composite."
    for i in range(2, int(n**0.5)+1):
        if n % i == 0:
            return False
    return True

I just tested this by getting every prime number between 1 and 100:

>>> primes = [i for i in range(2, 100) if prime(i)]
>>> primes
[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]
Jacob Lee
  • 4,405
  • 2
  • 16
  • 37
  • It works well, thanks! Also, I am a newbie and don't understand the keyword assert. Can you explain it please? – shark Apr 21 '21 at 05:04
  • `assert` statements are pretty simple: The `assert` keyword, a condition, and (optionally) a comma then some value to display. If the condition evaluates to `True`, nothing happens. If the condition evaluates to `False`, an `AssertionError` exception is raised and the value is displayed. In my example, if `n` is not greater than zero, a exception will be raised, saying `AssertionError: n must be a positive integer.`. The same goes for the second assertion. If `n` is 1, then: `AssertionError: 1 is neither prime nor composite.`. – Jacob Lee Apr 21 '21 at 05:43