This is an old comment, but just in case someone else is puzzled reading it... The % operator in C and C-based languages like C++, Java and C#, is not a true "modulo operator". It's a remainder from integer division that truncates toward 0, so that nonzero values of x % y will have the same sign as x.
Many other languages use a remainder from floor division, where nonzero values of x % y have the same sign as y. So n%10 always gives the least positive remainder from dividing n / 10. In this case, that makes:
This problem is one of the rare cases (in my experience) where C-style remainders simplify code. Usually there's no difference, but when there is I find that I have to adjust for the difference more often in C than in Python or Ruby.
Oh snap... My bad. Just re-tested it on Java and it seems fine.
However, writing the equivalent function in Python seems to spit out 9 for input = 0. That is strange and makes me wonder how different languages implement modulo operations. Very trippy.
Sorry to the numerous warriors who were alarmed by my ignorance! I only cared to quickly test it in Python.
I agree. I was coding up my solution to this problem using functions I have written before, but they were written for int so I had to change/refactor my functions to be compatible to long.
Do you know any way to make functions taking numeric inputs generic for int, long, short, BigInteger, etc.?
Hmmm. Hard to say. Did some microbenchmark tests, and sometimes the bitwise operation is faster, and sometimes the old fashioned exponential computation is faster. And I simulated very many trials with a wide range of powers, so I doubt there is any benefit to using the bitwise operation.
Other than being fun-spritied, I wouldn't really dub this a very readable code, since not many people know the bitwise operator in Python.
This is an old comment, but just in case someone else is puzzled reading it... The % operator in C and C-based languages like C++, Java and C#, is not a true "modulo operator". It's a remainder from integer division that truncates toward 0, so that nonzero values of x % y will have the same sign as x.
Many other languages use a remainder from floor division, where nonzero values of x % y have the same sign as y. So n%10 always gives the least positive remainder from dividing n / 10. In this case, that makes:
This problem is one of the rare cases (in my experience) where C-style remainders simplify code. Usually there's no difference, but when there is I find that I have to adjust for the difference more often in C than in Python or Ruby.
Consider:
Dang this is recursion at its best... Kudos to you clever sirs.
oh, that is weird let me know if you figure why that is
Oh snap... My bad. Just re-tested it on
Java
and it seems fine.However, writing the equivalent function in
Python
seems to spit out9
forinput = 0
. That is strange and makes me wonder how different languages implement modulo operations. Very trippy.Sorry to the numerous warriors who were alarmed by my ignorance! I only cared to quickly test it in
Python
.Again, apologies!
What are you talking about? I just tested with 0 and it passes just fine.
This is actually wrong. If the input is
0
the answer would be0
. But this would spit out9
if the input is0
.I suggest the numerous authors to revise this solution by adding an
if
statement for the special case0
.I agree. I was coding up my solution to this problem using functions I have written before, but they were written for
int
so I had to change/refactor my functions to be compatible tolong
.Do you know any way to make functions taking numeric inputs generic for
int
,long
,short
,BigInteger
, etc.?Hmmm. Hard to say. Did some microbenchmark tests, and sometimes the bitwise operation is faster, and sometimes the old fashioned exponential computation is faster. And I simulated very many trials with a wide range of powers, so I doubt there is any benefit to using the bitwise operation.
Other than being fun-spritied, I wouldn't really dub this a very readable code, since not many people know the bitwise operator in Python.