Don’t use recursion. Each call will need to allocate a new stack frame which leads to being slower than an iterative approach in pretty much any language.
Comment on I wish
affiliate@lemmy.world 1 year ago
amateurs
def is_even(n: int): if n ==0: return True elif n < 0: return is_even(-n) else: return not is_even(n-1)
Karyoplasma@discuss.tchncs.de 1 year ago
recapitated@lemmy.world 1 year ago
TCO baby
257m@sh.itjust.works 1 year ago
Since when did Python have tail call optimization?
affiliate@lemmy.world 1 year ago
here’s a constant time solution:
spoiler
i can’t imagine how long it’ll take to run, my computer took over 3 minutes to compute one value when the upper bound was replaced with 2^30^. but hey, at least it’s O(1).
Acters@lemmy.world 1 year ago
Nice, how about bitwise & operator?