6/15 Practice makes perfect


#1

I don't understand anything at all.

Here is my code:

def is_prime(x):
    if x < 2: 
        return False
    else:
        for n in range(2, x-1):
            if x%n == 0:
                return False
            else:
                return True

#2

All right, just else belongs to the cycle
for n in range(2,x-1):
if x%n == 0:
return False
else:
return True


#3
def is_prime(x):
    for n in range(2,x-1):
        if x % n == 0:
            return False
        else:
            return True

im not sure what im doing wrong here? :frowning:


#4

You made a mistake in the cycle.If you want to confirm x is a prime,it need to go through the whole cycle and there is no divisors.So,the judgement "True" must be out of the cycle.Try again like this:

def is_prime(x):
    if x < 2: 
        return False
    else:
        for n in range(2, x-1):
            if x%n == 0:
                return False
        else:
            return True

#5

Thanks dude, makes sense now! :smile:


#6

I think the range have a little problem, like:
it should be range(2, x), not range(2,x-1).


#7

It is much faster to test in range(2,sqrt(x)+1), it does the half computations especially for big numbers.


#8

The simplest I have made:

def is_prime(x):
if x <= 1:
    return False
for i in range(2,x+1):
    if x%i ==0 and x!= i:
        return False
    elif x%i ==0 and x== i:
        return True

#9

My code is a bit convoluted, but it works by iterating through a while loop. the reason it stores the verdict elsewhere, is because for some reason, as it iterated, it was returning the first verdict it came across, without looking at other values of n in the range (hence also the count mechanism, which exits the while loop)

def is_prime(x):
verdict = False
if x == 2:
    verdict = True
elif x > 2:
    count = 2
    while count <= x-1:
        if x % count == 0:
            verdict = False
            count += 1
            break
        elif x % count != 0:
            verdict = True
            count += 1
else:
    verdict = False
return verdict

can anyone tell me where I can clean up the code a bit? I hate having messy code, as it's harder to follow and wastes processing power.


#10

Can somebody please help me debug this code? Thanks in advance!

def is_prime(x):
if x < 2:
return False
elif x==2:
return True
elif x > 2:
for n in range(2, x):
if x % n == 0:
return False
else:
return True


#11

why do you first write
if x<2:
return False
why cant we start with the for n in range....?


#12

That's the definition of the prime number in Wikipedia:> A prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself.So,we need to exclude numbers less than 2 at the first.


#13

Could you explain in more detail why the final "else: return True" statement has to be nested under the else statement and not the for statement? This has been somewhat mysterious to me throughout this course, and I'm not having luck finding resources online that explain it (at least in a way that's relatively basic).


#14

well...what the situation should the function return True?When the parameter is a prime number,right?So putting > else:return True under the for statement means it is associate with> if x%n == 0 .That leads to a break after x=2.It means you just go through a cycle once,not from 2 to x-1.


#15

I don't get it why should i nest the else statement in the for-loop??


#16

It's actually connected to the if loop, but it's just that it's also inside the for loop at the same time.


#17

My code is like this:-

def is_prime(x):
if x<2:
return False

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

#18

Thanks for this help.

I couldn't figure out what I was doing wrong and that was it -- I wasn't completing the cycle.


#19

Hi! thanks for your help, but why the second else statement is not even with any of the if statements?


#20

def is_prime(x):
if x<2:
return False
elif x==2:
return True
else:
for i in range(2,x):
if x%i==0:
return False
break
else:
return True

I think a break is necessary in the for loop!