6. Is prime explanation


#1

can someone explain to me how exactly this for loop is working ? how does it calculate each number? n being the number going from 2 to x ? for example how would it work out 9 ? i got the right answer for this question but I'm not 100% sure on how it works

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

#3

x = 5 # I'am using x = 5 as the example
    for n in range(2,x): # loop starts at 2 and ends at 4, so it is 2,3,4 
        if x % n == 0: #It checks if x(5) is divided by 2, 3, 4 have  remainder of 0, 
            return False  # it does have a remainder, so it return False( 5/2 = 2.5, 5/3 =1.6, 5/4 =1.25)
    return True #Finally it exits the loop,  and remains True

Hope this helped:)


#4

Thanks very much. I confused myself more then anything.


#5

i still dont get it, here my code:
if x < 1:
for n in range(2,x):
if x%n == 0:
return False
return True
else:
return False