3
def is_prime(num):
    lst = []
    if num > 1:
        pass
    else:
        return False
    for number in range(0, 1000000+1):
        if str(num) in str(number):
            continue
        elif str(1) in str(number):
            continue
        elif str(0) in str(number):
            continue
        lst.append(number)
    for x in lst:
        if num % num == 0 and num % 1 == 0 and not(num % x == 0):
            return True
        else:
            return False

print(is_prime(9))

I do not know what is the problem with my code and I cannot find a solution the point of the program is to check whether the number is a prime number or not(prime number is only divisible by 1 and itself). The for loop just doesn't seem to work or something

Random Davis
  • 6,662
  • 4
  • 14
  • 24
Irkl1_
  • 71
  • 2
  • 8
  • 1
    Why are you turning everything into strings? (using `str()`) – Grismar Jan 14 '21 at 23:00
  • why not `is_prime = lambda a: all(a % i for i in range(2, a))`? – Coder Jan 14 '21 at 23:01
  • 2
    Although that works, @JohnD, assigning a lambda expression to a variable is a bad practice. If you need that, use a `def`. – Grismar Jan 14 '21 at 23:02
  • 1
    any number divided by 1 leaves remainder 0 always, so you condition in for loop will remain true – sahasrara62 Jan 14 '21 at 23:03
  • why is it bad practice? in this case it is a simple utility that can be expressed in one line comments on SO nicely which `def` cannot do ;) – Coder Jan 14 '21 at 23:03
  • The second `for` loop "doesn't work" because you return on the first iteration, either `True` or `False`, but you never get to the second iteration. It's not the only reason your function doesn't work, it has several problems. – Grismar Jan 14 '21 at 23:03
  • 1
    @JohnD https://stackoverflow.com/questions/25010167/e731-do-not-assign-a-lambda-expression-use-a-def - teaching people bad habits for the convenience of short posts is kind of pointless, given what StackOverflow is for to begin with, wouldn't you agree? :) – Grismar Jan 14 '21 at 23:04
  • @JohnD it is explicitly against PEP8 style guidelines. – juanpa.arrivillaga Jan 14 '21 at 23:08
  • @JohnD Cynically, I might say "because GVR hates `lambda`", which is probably at least part of the reason it made it into PEP8 – Silvio Mayolo Jan 14 '21 at 23:11
  • ah ic what is GVR? – Coder Jan 14 '21 at 23:13
  • Please supply the expected [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). Show where the intermediate results differ from what you expected. We should be able to copy and paste a contiguous block of your code, execute that file, and reproduce your problem along with tracing output for the problem points. This lets us test our suggestions against your test data and desired output. – Prune Jan 14 '21 at 23:13
  • We expect you to perform basic diagnosis to include with your post. At the very least, print the offending values at the point of error and trace them back to their sources. "just doesn't seem to work or something" shows that you haven't made any such attempt. – Prune Jan 14 '21 at 23:14
  • 1
    @JohnD Guido van Rossum, the creator of Python. he actually wanted to remove `lambda` from Python 3. He's the main reason that `reduce` was moved to the back of `functools` – juanpa.arrivillaga Jan 14 '21 at 23:58

4 Answers4

7
def isprime(n):
    return (all([False for i in range(2,n) if n % i == 0 ]) and not n < 2)
    
print (isprime(0))
print (isprime(1))
print (isprime(2))
print (isprime(3))
print (isprime(9))
print (isprime(10))
print (isprime(13))

Output:

False
False
True
True
False
False
True

Or:

def isprime(n):

    if n < 2: return False

    for i in range(2, n):
        if n % i == 0:
            return False
    else:
        return True
Synthase
  • 5,849
  • 2
  • 12
  • 34
3

Line 16-19:

if num % num == 0 and num % 1 == 0 and not(num % x == 0):
    return True
else:
    return False

The first statement, num % num == 0 is finding the remainder when num is divided by num, which is always zero and therefore this statement is always true. num % 1 == 0 is also always true, so your code is equivalent to:

if not(num % x == 0):
    return True
else:
    return False

Which means, in the context of your program, "if the first value in lst is not a factor of num, then num is prime. Otherwise, num is not prime"

This only runs the loop one time and is backwards. Instead, you need to check if one of the numbers in lst is a factor to return False, and if all of the number is lst are not factors, then return True

for x in lst:
    if num % x == 0:
        return False
return True
Reid Moffat
  • 322
  • 1
  • 3
  • 16
2
for x in lst:
    if num % num == 0 and num % 1 == 0 and not(num % x == 0):
        return True
    else:
        return False

This loop can only ever run once, as it's guaranteed to return either true or false at the first iteration. If you find any factors, you want to return False. Otherwise, True.

for x in range(2, num):
    if num % x == 0:
        return False
return True

(Also, I'm not sure what was going on with the str shenanigans or the num % num / num % 1 conditions, both of which are always zero. But those should be unnecessary)

Silvio Mayolo
  • 62,821
  • 6
  • 74
  • 116
  • Strictly speaking you don't have to have the range stop at num-1, as python ranges don't include the last number in the range. For speed you can also have the range as ```range(2:int(math.sqrt(num)+1))``` – goalie1998 Jan 14 '21 at 23:06
  • Ah, I can never remember which languages have inclusive ranges and which ones don't. Thanks for pointing that out! – Silvio Mayolo Jan 14 '21 at 23:07
2

Except for 2 itself, you should only check divisibility by odd numbers up to the square root of the value.

Here's a short version:

def isPrime(N): 
    return all(N%p for p in range(3,int(N**0.5)+1,2)) if N>2 and N&1 else N==2
Alain T.
  • 40,517
  • 4
  • 31
  • 51