19
9
How to determine if a number is odd or even without mod -or- bitwise operations?
This challenge is grossly inefficient, but challenges your ability to think outside the box for a creative solution.
EDIT:
Please create a function. Also, while regex is a fun response, the function should accept any valid number.
BACKGROUND:
This question stems from my earliest programming days. The homework for our first day of class was to write a simple program that printed 'odd' or 'even'. Being the brat I was, I didn't read the book we had for the class where it simply showed us how to use %
to determine that. I spent about a half hour pacing back in forth in my room trying to think of a way to do this and had remembered from the lecture that numbers can lose and gain precision as they are cast from one primitive type to another. Therefore, if you took the number, divided it by two and then multiplied it back didn't equal the original number, then you would know that the number was odd.
I was stunned the next day, while our instructor was evaluating our programs, that he thought that it was the most original, if inefficient, way of solving the problem.
This has been around for quite a long time, but there doesn't seem to be a winning condition of any kind, which to my mind means there is no game here. – dmckee --- ex-moderator kitten – 2013-03-16T19:22:50.770
3Should we create a function or a program? How should IO happen if we have to do a program? Please, elaborate further. – Juan – 2011-04-28T03:10:40.160
2What objective criterion will determine the accepted answer? Code size? Something else? – PleaseStand – 2011-04-28T03:47:18.897
Is it definitely a number? Should it give false positives for a string? – William – 2011-04-30T01:22:54.717