Ad
  • Custom User Avatar

    Dang this is recursion at its best... Kudos to you clever sirs.

  • Custom User Avatar

    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.

    Again, apologies!

  • Custom User Avatar

    This is actually wrong. If the input is 0 the answer would be 0. But this would spit out 9 if the input is 0.

    I suggest the numerous authors to revise this solution by adding an if statement for the special case 0.

  • Custom User Avatar

    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.?

  • Custom User Avatar

    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.