Jul-19-2018, 03:51 AM
Hi all,
So, this is a small thing that I know the answer to, but I'm just curious why the answer is what it is. I'm going through Codeacademy's python course, and right now it's having me create a little function that tests an inputted number to see if it's prime or not. I won't spoil the rest of the code, except to say that you have to say:
for n in range(2, x-1):
if x % n == 0:
return False
I don't understand why it's not range(2, x) instead of range(2, x-1). I mean, if you're testing 11, say, then it's going to test 11 % 2 through 11 % 10, because the stop point is up to but not including the stop point. In all cases, you're not going to have zero remainder, and given that it's also greater than 2, it's a prime number. I tested this in the exercise and it still ran without problems.
Any thoughts?
P.S.: I considering putting this under the homework sub-forum, but it's not homework, it's just something I'm doing out of curiosity.
Thanks!
So, this is a small thing that I know the answer to, but I'm just curious why the answer is what it is. I'm going through Codeacademy's python course, and right now it's having me create a little function that tests an inputted number to see if it's prime or not. I won't spoil the rest of the code, except to say that you have to say:
for n in range(2, x-1):
if x % n == 0:
return False
I don't understand why it's not range(2, x) instead of range(2, x-1). I mean, if you're testing 11, say, then it's going to test 11 % 2 through 11 % 10, because the stop point is up to but not including the stop point. In all cases, you're not going to have zero remainder, and given that it's also greater than 2, it's a prime number. I tested this in the exercise and it still ran without problems.
Any thoughts?
P.S.: I considering putting this under the homework sub-forum, but it's not homework, it's just something I'm doing out of curiosity.
Thanks!