-
Notifications
You must be signed in to change notification settings - Fork 0
/
14_divide_without_division.rb
66 lines (53 loc) · 1.34 KB
/
14_divide_without_division.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# Division
# Write a method to perform integer division. Divide two numbers without using the division operator.
# This solution assumes the divisor is smaller than the dividend
def division(dividend, divisor)
return 'Denominator of 0 is undefined' if divisor.zero?
return 0 if dividend.zero?
pos_dividend = dividend.abs
pos_divisor = divisor.abs
quotient = 0
if pos_dividend == pos_divisor
quotient = 1
else
while pos_dividend >= pos_divisor
pos_dividend -= pos_divisor
quotient += 1
end
end
((dividend < 0) ^ (divisor < 0)) ? - quotient : quotient
end
# evenly divisable numbers
puts division(10, 2)
puts division(10, 5)
puts division(100, 5)
puts division(32, 4)
puts division(99, 3)
puts division(80, 1)
# same number divided by itself
puts division(1, 1)
puts division(10, 10)
puts division(69, 69)
# unevenly divisable numbers with larger dividends
puts division(9, 2)
puts division(9, 5)
puts division(99, 5)
puts division(31, 4)
puts division(98, 3)
# single negative number
puts division(-10, 2)
puts division(10, -5)
puts division(-1, 1)
puts division(9, -2)
puts division(-9, 5)
# multiple negative numbers
puts division(-10, -2)
puts division(-10, -5)
puts division(-1, -1)
puts division(-9, -2)
puts division(-9, -5)
# zeros
puts division(0, 1)
puts division(0, 5)
puts division(1, 0)
puts division(5, 0)