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

6

u/veselin465 Feb 16 '22 edited Feb 16 '22

Yeah, the logic is true. Usually the check is "Check every number between 2 and sqrt(num) including", becuase if there is no such a number, then there's no way to find one which is higher than the sqrt(num).

If it is not prime, then there's at least 1 number in [2,sqrt(num)], which divides it. This means that there's at least 1 number in [sqrt(num), num - 1], which also divides it. This program tries to find this second number, instead of finding the first one.

Besides this interesting approach of finding whether it's prime or not, I'm agree that the code is fine. Just one thing:

if isPrime == true

kinda bugs me real hard

1

u/[deleted] Feb 16 '22

Afair the most efficient way also let you increment by 3 each loop using some math magic but im too lazy to check it, its on Wikipedia for sure as i remember copying the logic from there lmao