Prime Inspection. 15. Again


#1



https://www.codecademy.com/en/courses/python-intermediate-en-rCQKw/0/6?curriculum_id=4f89dab3d788890003000096

Hello everybody! I have a lttle problem with this exercise. This code works, but it throws an exception like
"
Your function fails on is_prime(15). It returns True when it should return False.
"
You can see, I added an add-hoc, but it iis going on throwing this excepton. What's wrong with it? Thank you.


def isPrime(n):
    delim = 2
    if n == 15:
        return False
    while n % delim != 0:
        delim += 1
    return delim == n


#2

Are you sure that's a valid approach to determine if a number is prime? Read the definition of what a prime number is carefully.


#3

Yes, it is realy working. I've tried to run it with 15 in argument in other window, where I can use print() function, and it returned False without

if n == 15:
        return False

#4

Oh. You changed the name, your error message is calling a different function from what you posted, one that you created earlier.

(I just assumed that your code was incorrect as you decided that you needed to add a special case for 15)


#5

Thank You. I'll try to fix it, but it is really mysteryous - I've tried some other methods, but all of them throwed an exception on 15. For example, this one. Theoretically, it works faster

def IsPrime(n):
    d = 2
    while d * d <= n and n % d != 0:
        d += 1
    return d * d > n

#6

You have a second function named is_prime, that is what the submission test calls.
You created a second function isPrime (and a third) IsPrime but those are not what the submission test calls. It calls is_prime

Additionally you'll need to consider what happens for numbers less than 2


#7

At a second glance at your code, I like what I see.
It's a bit more subtle than how that is usually written but makes a lot of sense.

Subtlety is bad in code if it can be written more explicitly, but as far as problem solving goes, I really like it, it's rather elegant.


#8

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.