Ad
  • Default User Avatar

    I do not find this code "Best Practices" or "Clever". There are two bugs in the solution.

    Bug 1: Deadlock
    With some input values variable v will overflow, become negative and the while loop will go in deadlock.

    Test with the max value for a 64-bit integer.
    testequal(9223372036854775807, -1);

    Bug 2: Precision loss with double conversion
    The inputs to pow function are converted to double and the output is converted back to integer. A double can't represent whole numbers bigger than 2^53.

    Test with the maximum number of squares possible for an 64-bit integer.
    testequal(9223193340756366400, 77935);