-4

The below code keeps displaying "is not prime" for a prime number and "is prime" for a number that is not prime. What am I doing wrong?

quuN = int(input("ENTER NUMBER : "))

quuM = 2

if (quuN <= 0) :

    print("ENTER NON-NEGATIVE NUMBER PLEASE")


elif (quuN % quuM == 0) :

    print(" IS PRIME " )

else : 

    print("IS NOT PRIME ")
Karin
  • 8,404
  • 25
  • 34
iivri andre
  • 51
  • 2
  • 7
  • 1
    Why do you think that this code would accurately determine whether a number is prime? – michaelrccurtis Aug 13 '16 at 00:31
  • @michaelrccurtis well I tried to represent the conditions indicating a prime in if statements. So if _quuN_ has a mod of 0 when divided by _quuM_ then it would not be a prime since primes are evenly divisible by 1 and itself. – iivri andre Aug 13 '16 at 00:40
  • You're only checking if the number is divisible by 2... this program should be called "Check if a number is odd or not" – Mateja Apr 08 '21 at 16:12

4 Answers4

0

The logic is incorrect

A prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself. A natural number greater than 1 that is not a prime number is called a composite number.

Simple python code below

def is_prime(n):
    for i in range(3, n):
        if n % i == 0:
            return False
    return True
Sudheej
  • 1,873
  • 6
  • 30
  • 57
  • 2
    s_prime(4) will be displayed as True but 4 is not a prime number. The for loop should start from 2. – warun26 Aug 13 '16 at 00:57
0

The above code is checking if a number is even or odd. If you enter a prime number, for example 17, the code checks if 17 is less than or equal to 0. Then it checks 17%2 which evalutes to 1, and is not 0. Hence the else block is executed which prints IS NOT PRIME.

If you enter an even number, it prints IS PRIME.

This code checks for Prime numbers.

def is_prime(n):
    import math
    for i in range(2, int(math.sqrt(n))+1):
        if n % i == 0:
            return False
    return True
warun26
  • 453
  • 9
  • 18
  • Why do I need the for loop which nests the if loop, if I only need to know if one number entered is a prime or not. – iivri andre Aug 13 '16 at 00:44
  • A prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself. The for loop goes over the natural numbers from 2 to till the square root of n to *check* if any of these numbers actually divide n. Check [this](http://stackoverflow.com/questions/5811151/why-do-we-check-upto-the-square-root-of-a-prime-number-to-determine-if-it-is-pri) for why we use square root. The *if* condition facilitates this checking. The % operation checks if the reminder of dividing n and i is 0. If it is 0, then i divides n and n is not a prime number. – warun26 Aug 13 '16 at 00:55
0

I assume you're a beginner with python so let me point out the logic to check primality of numbers in your code is not correct, you should read first carefully the definition of primes numbers, when you do so, try to understand this little example which shows you how to check prime numbers:

import math

def is_prime_naive(number):
    if number == 2:
       return True
    if number % 2 == 0:
        return False

    i = 3
    sqrt_number = math.sqrt(number)

    while i <= sqrt_number:
        if number % i == 0:
            return False
        i = i+2

    return True

for i in range(2,101):
    print "{0} {1} prime".format(i,"is" if is_prime_naive(i) else "is not")

Now, be aware the above code is one of the simplest but also slowest way to check whether a number is prime or not. When you become familiar enough with the concept of primes then you should check for fastest way to check primality, some examples could be the Fermat and Miller Rabin primality tests. So, good luck with primes, you'll sure have fun with them ;-)

BPL
  • 9,632
  • 9
  • 59
  • 117
0

Here's the answer without using any libraries

Note: it's only needed to check until sqrt(n), explanation here

def isPrime(n):
    if (n<=1): return False

    for i in range(2, int(n**(.5))+1):
        if (n%i==0):return False

    return True
Mateja
  • 271
  • 1
  • 11