5 ms·
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 co
by blauditore 10mo ago
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.
- layer8 10mo agoYou could save stack space by transforming it into a loop. It’s still only O(n)!
- majkinetor 10mo agoBut then, even numbers will have the worst possible performance.
- IncreasePosts 10mo agoGood 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.
- rdiddly 10mo agoYeeessss! Microservices!
- chihuahua 10mo agoYour 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?
- colejohnson66 10mo agoCould try 1 per IPv4 address? There’s just barely enough!
- hybridtupel 10mo agoWhy not run both and use the result retrieved the fastest.
- blauditore 10mo agoThat sounds kinda stupid, it would completely destroy the original space savings. Unless the sharding makes it fit within compiler limits.
- bobbylarrybobby 10mo agoYou 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.
- deleted 10mo ago[deleted]