Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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.





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

Good point. Have two programs - one checking every even number and returning odd of not even. And then have a program checking every odd number and returning even if not. Then, a simple program to dispatch to either program randomly, so you end up in the long term with good performance for each.

Why not run both and use the result retrieved the fastest.

That sounds kinda stupid, it would completely destroy the original space savings. Unless the sharding makes it fit within compiler limits.

Yeeessss! Microservices!

Your mention of Microservices opened up my mind to additional possibilities. How about we create a microservice for each integer, then deploy 4 billion of them. Send a request to all of them simultaneously. Only one of them will respond with the answer. We still need to decide how to deploy those microservices - one per machine, or multiple per machine?

Could try 1 per IPv4 address? There’s just barely enough!

You brought up an important opportunity for optimization. If you know the distribution of your data, it may make more sense to implement it in terms of the odd numbers and leave even numbers as the fallback. It's important to profile with a realistic distribution of data to make sure you're targeting the correct parity of numbers.

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



Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: