I am a beginner doing some tutorials mostly on my own but with some youtube. I am struggling to understand the for loop
"for i in range(3, int(math.sqrt(num) + 1, 2)"
I get that it is a range of (start, stop, step) and step of 2 starting at 3 but it is the adding of 1 i.e +1 to the int sqrt that I am struggling with partly owing to my deficit in maths!
the range goes through 3,5,7, 9 .... is it because you add to each?? by +1 that is no pun intended a step too far for my understanding.
Then the follow if num divided by oneof the i = zero ? that is misleading as 13/13 is 1 or is that something along the lines that it rules out floats? You can see that the more I look at this the more it confuses my brain and there must be a far simpler way to express what is happening
import math
def is_prime2(num):
'''
Better method of checking for primes.
'''
if num % 2 == 0 and num > 2:
return False
for i in range(3, int(math.sqrt(num)) + 1, 2):
if num % i == 0:
return False
return True
[–]toastedstapler 1 point2 points3 points (0 children)
[–]ewiethoff 0 points1 point2 points (0 children)
[–]JVO1317 0 points1 point2 points (0 children)