Skip to content

Instantly share code, notes, and snippets.

@kylelong
Created June 3, 2019 05:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kylelong/3756dfd061f0917e8699d13afc441880 to your computer and use it in GitHub Desktop.
Save kylelong/3756dfd061f0917e8699d13afc441880 to your computer and use it in GitHub Desktop.
Cassido's interview question for week of 5/26/19 - Implement integer division
=begin
Integer dvision for a / b
@param a first number
@param b second number
@return a / b
=end
def divide(a, b)
sign = (a < 0) ^ (b < 0) ? -1 : 1
a = a.abs
b = b.abs
count = 0
while a >= b
a -= b
count += 1
end
return count * sign
end
p divide(-15, -15)
p divide(5, 10)
p divide(10, 2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment