-2

I am trying to make a simple prime number detector base on user input. These code line down here I have input 2 and 5 and 123. Even though they are prime number but the program seems to print "not prime number" for any number you input

I have tried a lot but most of my code even didn't print anything.

def check_Prime(f):
    if(f<2):
        return False
    can=math.sqrt(f)
    for x in range(2,can):
        if(f%x==0):
            return False
        else:
            return True

if check_Prime is True:
    print("prime number")
else:
    print("not prime number")

I expect if you input a prime number then it will print("prime number") and if you didn't input prime number it will print the other one

khelwood
  • 55,782
  • 14
  • 81
  • 108
  • 2
    Your `check_Prime` returns true as soon as you find any number that is not a factor of `x`. Also you're not calling it. – khelwood Aug 16 '19 at 08:49
  • See also, for instance, https://stackoverflow.com/questions/33765792/function-to-check-if-number-x-is-prime-in-python , https://stackoverflow.com/questions/45892051/finding-prime-numbers-in-python – khelwood Aug 16 '19 at 08:50
  • 1
    Possible duplicate of [Trying to find the prime numbers using Python](https://stackoverflow.com/questions/27730410/trying-to-find-the-prime-numbers-using-python) – khelwood Aug 16 '19 at 08:51
  • you are checking if the function `check_Prime` is True, which it isnt, you might try `result = check_Prime(n)` and then on the if condition instead of `check_Prime` use `result` – IWHKYB Aug 16 '19 at 08:52
  • your function is flawed. If you pass a `3`, your `range` will fail as sqrt(3) is not an int – Unamata Sanatarai Aug 16 '19 at 08:52

4 Answers4

1

You aren't calling the function. Your line if check_Prime is True: is checking if the function itself is true. Which it always is.

You would need to actually call the function with a value like so:

if check_Prime(3) is True:

However you will then discover that this can throw

TypeError: 'float' object cannot be interpreted as an integer

When math.sqrt() returns a non-integer.

PyPingu
  • 1,697
  • 1
  • 8
  • 21
1

You don´t call the function because you check only if the function is available. Change

if check_Prime is True:
    print("prime number")
else:
    print("not prime number")

to

if check_Prime(<YourInput>) is True:
    print("prime number")
else:
    print("not prime number")

and fix your TypeError because range only work with integers. Take a look at here or here to learn how to deal with float in range.

Kampi
  • 1,798
  • 18
  • 26
0

Here is a quick function. It is slow. Should you want a speedy function, try this prime checking in python

def check_Prime(number):
    if number <= 1:
        return False

    if number == 2 or number == 3:
        return True

    if number % 2 == 0:
        return False

    limit = int(math.sqrt(number)) + 1
    for test in range(3, limit, 2):
        if number % test == 0:
            return False

    return True

if check_Prime(3) is True:
    print("prime number")
else:
    print("not prime number")
Unamata Sanatarai
  • 6,475
  • 3
  • 29
  • 51
0
import math


def checkPrime(f):
    flag = True
    if f >= 2:
        limit = int(math.sqrt(f)) + 1
        for x in range(2, limit):
            if f % x == 0:
                flag = False
    else:
        flag = False
    return flag


if checkPrime(100):
    print("prime number")
else:
    print("not prime number")
Musica M
  • 1
  • 2