Skip to content

Instantly share code, notes, and snippets.

@kshirsagarsiddharth
Created October 16, 2020 12:34
Show Gist options
  • Select an option

  • Save kshirsagarsiddharth/7aa5b2655c6e310ee3bc26fd9789354c to your computer and use it in GitHub Desktop.

Select an option

Save kshirsagarsiddharth/7aa5b2655c6e310ee3bc26fd9789354c to your computer and use it in GitHub Desktop.
find quotient of two numbers
def find_quotient(x,y):
# keep the value of power as large as possible
result,power = 0,16
# this is (2^k)y inttially defined
# in this case it is (2^16)y
y_power = y << power
# because we are successfully dividing we stop when
# x is less than y
while x >= y:
# we want to find k such that (2^k)y <= x
# so we stop looping when y_power < x
while y_power > x:
y_power >>= 1
power -= 1
# once we find k which satisfies the condition
# we add it to result and subtract x with y_power
result += 1 << power
x -= y_power
return result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment