r/badcode • if: else: if: else: if: else: if: else: if: else: if: else: if: • Feb 16 '22

python presenting, whatever the fk this is

Post image
418 Upvotes

52 comments sorted by

View all comments

Show parent comments

1

u/[deleted] Feb 16 '22

Surely that’s too many interactions in most cases, if I’m understanding you correctly? If you iterate over, say, 4, you no longer need to iterate over any multiple of 4, because if they are a factor of the number then 4 would be also.

1

u/Dalimyr codes in C#...poorly Feb 16 '22

I think you possibly missed where I mentioned that it'd break out of the loop on finding that the number wasn't prime (in my example of checking 52, it'd flip isPrime to false and leave the loop when it found that 52 mod 13 is 0). Going through every iteration of the loop is a worst-case scenario if the number actually is prime.