Julia
Here is an entry that not only is free from division but doesn't employ any multiplication either. It does the long division quite literally by using more string manipulation than arithmetic. It also prints out an ASCII version of what the long-division would look like on a sheet of paper (at least the way I learned it)
function divide(x,y)
if y > x
return 0, x
end
x = "$x"
q = ""
r = ""
workings = ""
for i = 1:length(x)
r = "$(r)0"
num = int(r) + int(x[i:i])
sum = 0
m = 0
while sum+y <= num
m += 1
sum += y
end
r = string(num-sum)
q = "$q$m"
ls = length(string(sum))
workings *= repeat(" ", i-ls) * "-$sum\n"
workings *= repeat(" ", i+1-ls) * repeat("-", ls) * "\n"
workings *= repeat(" ", i+1-length(r)) * r * (i >= length(x) ? "" : x[i+1:i+1]) * "\n"
end
workings *= repeat(" ", length(x)-length(r)+1) * repeat("=", length(r)) * "\n"
print(" $x : $y = $(int(q)) R $r\n$workings")
int(q), int(r)
end
Results (the (q,r)
line at the end is just Julia printing the result of the function call):
> divide(5,3) > divide(4138,17) > divide(7182,15)
5 : 3 = 1 R 2 4138 : 17 = 243 R 7 7182 : 15 = 478 R 12
-3 -0 -0
- - -
2 41 71
= -34 -60
-- --
(1,2) 73 118
-68 -105
-- ---
58 132
-51 -120
-- ---
7 12
= ==
(243,7) (478,12)
I suppose I could get rid of the remaining arithmetic by using a unary number system, repeat
and length
but that feels more like multiplying than not using arithmetic.
Don't even try dividing by zero! (Seriously, who would do long division for that?) Also don't try negative numbers.