Dont know what to do with this


#1


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

i dont know what to do with this it gives error plz help


6. is_prime
#2

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

this is my code with good looking hh
plz help me with this


#3

We wouldn't give n a value and then erase it in the next line. The above line is not needed, so may be removed.

for n in range(2, x - 1):

Somewhere somebody decided that range() includes the upper bound, and to circumvent this they felt it necessary to subtract 1 from x. Bottom line? Not necessary. If x is the upper bound, n will never equal it since x will not be in the range.

print range(5)    # [0, 1, 2, 3, 4]

Truth be known, we could write,

for n in range(3, (x + 1) / 2):

A number greater than 1/2 of a number cannot be a factor. And it gets better.

from math import sqrt

for n in range(3, sqrt(x + 1)):

If there are no factors less than the square root of a number, there are none greater. We make our program super fast, especially if we filter out even numbers before running the loop. They should return an immediate False.

In the above I use x + 1 so as to cross the line and not miss any special cases that may exist in the math.


#4

please because i have been a lot unsure about using the range loop plz explain for me why the instructions
told me to make 2 to x-1 i mean is this have to be range or what? and why i have to make sqrt??


#5

Not have to, may. This is one's own option. If one cannot reason why, then just follow and stick to the instructions and make one's way along. I have my doubts as to how much you really understand. When you put yourself in the deep end, you run the risk of being without a life guard when you need one. Suggest go all the way back to the beginning and start over. All the way back.


#6

what if i completed to the end of the course what will happened ? do you me to begain all over i had an idea about finishing the course and do what i can to improve my coding skills and when i need something search for it or try to understand it what do really think about it?


#7

There is no harm in a do-over. The review will prove helpful, and indications are that it is necessary in your case. I don't think you are ready to move forward, but then that is my own opinion based on what I've seen in the forums.


#8

iam already doing that now thanks


#9

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