0

I am trying to write a program that tells you whether a number is prime or not. Here it is. As you can see when you run the chkPrime function everything returns true. I for the life of me can't figure it out. Any ideas?

total=0

#Check if number is whole
def chkWhole(x):

    if(x%1 == 0):

        return True

    else:

        return False




#Check to see if the number divides evenly with all primes
def chkModtwo(n):
    a=n%2
    if chkWhole(a)==True:
        return True
    else:
        return False
def chkModthree(n):
    a=n%3
    if chkWhole(a)==True:
        return True
    else:
        return False
def chkModfive(n):
    a=n%5
    if chkWhole(a)==True:
        return True
    else:
        return False
def chkModseven(n):
    a=n%7
    if chkWhole(a)==True:
        return True
    else:
        return False


#Check if the number is a prime using other two functions

def chkPrime(n):
    if n== 1 or 2 or 3 or 5 or 7:
        return True

    if chkModtwo(n)==False and chkModthree(n)==False and chkModfive(n)==False and chkModseven(n)==False:
        return True
    else:
        return False

#while True:
#yourinput=raw_input("Enter to check if it is a prime")
#
#
#    youranswer=chkPrime(yourinput)
#
#    if youranswer==True:
#        print("Yes, it is a prime")
#    else:
#        print("No, this number is not prime")
TheCodeArtist
  • 21,479
  • 4
  • 69
  • 130
user1074202
  • 139
  • 1
  • 4
  • 14

4 Answers4

5
if n== 1 or 2 or 3 or 5 or 7:

should be

if n == 1 or n == 2 or n == 3 or n == 5 or n == 7:

or

if n in (1, 2, 3, 5, 7):
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
1

How does your function respond when it tries to determine if 143 is prime or composite?

Here's how I wrote this function at my blog.

def isPrime(n):
    if n % 2 == 0:
        return n == 2
    d = 3
    while d * d <= n:
        if n % d == 0:
            return False
        d += 2
    return True
user448810
  • 17,381
  • 4
  • 34
  • 59
1

this will be fast and concise for numbers up to 10^14 assuming you are not going for maximum efficiency. it takes about 15 seconds to iterate through each number in the range 10**7. so you can do this execution in about 10-15 seconds for numbers of this size. of course, you can change the print statements to whatever you need them to be

Try:

import math
def isPrime(x):
     prime = True
     if x%2 == 0:
          Prime =False
     else:
          for i in range(2,math.sqrt(x)):
               if a == i:
                    pass
               else:
                    a%i == 0:
                    Prime = False
                    break
     return Prime

if isPrime(x):
    print('prime')
else:
    print('Not prime') 
-2
def prime():
    a=int(raw_input("enter the no : "))
    if a == 1:
         print "the no is not prime"
    elif a == 2:
         print "the no is prime"
    else:
        for i in range(2,a):
            if a%i == 0:
            return "the no is not prime"
            else:
             return "the no is prime"

prime()
Robert
  • 5,278
  • 43
  • 65
  • 115