Hello, I think the two fragments below should do the same:
for divisor in range(2, 21): if sample % divisor != 0: break
The first fragment, I use sample divided by a number from 2 to 20, if any of them gives a remainder! = 0, then I will break and try sample += 1 (codes omitted)
if all(sample % divisor == 0 for divisor in range(2, n2+1)): return sample
In the second fragment, I will return the sample if all() returns with True , otherwise I will try sample += 1 (codes are omitted)
The second fragment was found twice as slow as the first. I donβt understand when python evaluates all (), if the same False was found in the iteration, it should immediately return False for all (), and not finish the whole iteration, right?
So why is the second fragment slower than the first?
source share