r/shittyprogramming • u/GlueDonkey • Apr 19 '21
7 line "is even" function 🤯
I've attempted to solve the infamous "is even" problem, and I've come up with something that is both short (only 7 lines), and extremely fast (I tested with the number 99999999 and it only takes about 3 seconds)
behold:
def is_even(num):
s = "\\"
try:
eval(f"\"{s*num}\"")
return True
except SyntaxError:
return Falsehow did I make it so fast?
a few ways:
- it's written in python.
- it uses eval, one of python's fastest functions
- it uses strings, which are faster than numbers (the only number this code relies on is the input one...)
- it makes use of the little-known "\" operator, which is one of the fastest operators in python
190
Upvotes
View all comments
Show parent comment
6
u/GlueDonkey Apr 19 '21
here it is: https://replit.com/@lllllllllllllol/iseven#is_even_2#is_even_2.py
it's extremely broken and straight up doesn't work with large numbers, but at least the C makes it lightning fast