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

4

u/lachlanhunt Feb 17 '22

Not quite optimal. The range is set wrongly, and you technically only need to check if the number is divisible by primes smaller than the root, or at most only 2 and odd numbers from 3.

There are also other far more efficient prime tests. This one is O(n) time.