Sorry, I see I didn't give a good explanation for that part! C&Ping the relevant section:
for i in range(3,candidate/2): # this strips out all the numbers bigger than 1/2.
remaindercheck = remaindercheck * candidate%i # This number is only non-zero if every remainder is non-zero.
In case a little extra explanation is needed: a for loop is a specific kind of while statement. It increments the variable i from 1 to candidate/2, and then it finishes. (Generally speaking, I've been taught that it's better to use for loops where you can; a for loop is guaranteed to end at some point, but an incorrectly written while loop can loop infinitely.)
i is the number I'm going to be dividing candidate by. The statement candidate%i returns the remainder of candidate/i; so for example, if candidate = 5 and i = 3, then candidate%i = 2, because 5/3 = 1 and 2/3.
For the statement remaindercheck = remaindercheck * candidate%i , I'm taking advantage of the fact that any number multiplied by zero is equal to zero. What this means is that if any remainder is equal to zero, then remaindercheck will be set to zero until the end of the for loop. If none of the remainders are zero, then remaindercheck will be some positive number.
For example: if candidate = 9: when i = 3: remainder of 9/3 = 0, therefore remaindercheck = 1 * 0 = 0 when i = 4: remainder of 9/4 = 1, therefore remaindercheck = 0 * 1 = 0
When the for loop ends, remaindercheck = 0 and we know this is not a prime number.
if candidate = 11: when i = 3: remainder of 11/3 = 2, therefore remaindercheck = 1 * 2 = 2 when i = 4: remainder of 11/4 = 3, therefore remaindercheck = 2 * 3 = 6 when i = 5: remainder of 11/5 = 1, therefore remaindercheck = 6 * 1 = 6
When the for loop ends, remaindercheck != 0 and we know this is a prime number.
no subject
Date: 2010-01-17 12:10 am (UTC)In case a little extra explanation is needed: a for loop is a specific kind of while statement. It increments the variable i from 1 to candidate/2, and then it finishes. (Generally speaking, I've been taught that it's better to use for loops where you can; a for loop is guaranteed to end at some point, but an incorrectly written while loop can loop infinitely.)
i is the number I'm going to be dividing candidate by. The statement candidate%i returns the remainder of candidate/i; so for example, if candidate = 5 and i = 3, then candidate%i = 2, because 5/3 = 1 and 2/3.
For the statement remaindercheck = remaindercheck * candidate%i , I'm taking advantage of the fact that any number multiplied by zero is equal to zero. What this means is that if any remainder is equal to zero, then remaindercheck will be set to zero until the end of the for loop. If none of the remainders are zero, then remaindercheck will be some positive number.
For example:
if candidate = 9:
when i = 3: remainder of 9/3 = 0, therefore remaindercheck = 1 * 0 = 0
when i = 4: remainder of 9/4 = 1, therefore remaindercheck = 0 * 1 = 0
When the for loop ends, remaindercheck = 0 and we know this is not a prime number.
if candidate = 11:
when i = 3: remainder of 11/3 = 2, therefore remaindercheck = 1 * 2 = 2
when i = 4: remainder of 11/4 = 3, therefore remaindercheck = 2 * 3 = 6
when i = 5: remainder of 11/5 = 1, therefore remaindercheck = 6 * 1 = 6
When the for loop ends, remaindercheck != 0 and we know this is a prime number.
Does that make sense?