-1

I have developed a code to find a prime number using python. But it keeps saying the numbers are prime even tho they are not. I am new to python so my knowledge in syntax is really not as good

when i input the number 15 it would say it is a prime number. but it shouldn't be a prime number

a = 0
#num will be the number we are checking
x = int(num)
num = sqrt(float(num))
num = int(num)
primeCheck = True
for a in range(3, num, 2):
    if x == 1 or x == 2:
        primeCheck = False
        #not prime
    if x % a == 0:
        primeCheck = False
        #not prime
if primeCheck == true:
    #prime
idjaw
  • 21,992
  • 6
  • 53
  • 69
abcd123
  • 21
  • 4
  • [this](https://stackoverflow.com/questions/1801391/what-is-the-best-algorithm-for-checking-if-a-number-is-prime) and [this](https://stackoverflow.com/questions/2068372/fastest-way-to-list-all-primes-below-n?rq=1). – idjaw Jun 16 '19 at 19:39
  • There are many answers in SO about that, why don't you check those before asking again? – c0x6a Jun 16 '19 at 19:40

1 Answers1

0

You want x % a, not a % x. The larger number should almost always be on the left for the mod operation.

Draconis
  • 2,771
  • 14
  • 24