logoalt Hacker News

blauditorelast Friday at 2:04 PM2 repliesview on HN

This could be obviously done with much less code: Just add "if"s for all even number, and at the end just return "odd" if none of the evens matched. 50% less code!

Or even simpler: If it's 0, return "even". If not, do a recursive call to n-1, if that equals "even", return "odd", otherwise return "even".

But the best way is probably to just use a library. Yes, 500MB of additional dependencies, but then it's a one-liner.


Replies

majkinetorlast Friday at 2:14 PM

But then, even numbers will have the worst possible performance.

show 2 replies
layer8last Friday at 2:10 PM

You could save stack space by transforming it into a loop. It’s still only O(n)!