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.
Even using the hand method I still mentally divide each grouping. I guess you could subtract in a loop or multiply by the inverse, but those just seem like trivial workarounds. Also, you might want to give better examples than single-digit numbers if you want the "long-hand" method to show clearly. – Geobits – 2014-03-19T16:02:28.210
5@Geobits is right, everyone still uses division (mostly mentally) even when performing long division. I suggest the modifying the requirement like "division operators may only be used if the result is less than 10". All that long division serves to do is break a large division problem into a bunch of smaller division problems where each quotient is guaranteed to be in the range of 0 through 9. – Rainbolt – 2014-03-19T16:22:51.047
please explain the requirement in simple words (sorry, I am not so good at english !) – Mukul Kumar – 2014-03-19T16:36:34.990
In the old days of school, computers were expensive, so we had calculators instead. I used the fingers on my hands to type division calculations into my calculator. Is this the "hand" method you refer to? – Digital Trauma – 2014-03-19T16:39:36.783
@DigitalTrauma NOOO the one we used ti do by pen/pencil and a paper! – Mukul Kumar – 2014-03-19T16:42:22.377
@ The downvoter, please explain the reason of your down-vote! – Mukul Kumar – 2014-03-19T16:50:23.523
624 hours is too little time. It is recommended that you wait for a while (maybe a week), and update the winner every now and then. – Justin – 2014-03-19T18:19:13.943
4
The previous long division of integers question had a spec and required output which made clear what calculations had to be performed. The long division of polynomials question would be most easily implemented by long division even if that weren't specified. This one is so imprecise that you're praising answers which don't even pretend to follow what little instruction it gives.
– Peter Taylor – 2014-03-19T18:50:40.003Aww, too bad it is put on hold just as I finished my answer :) – CompuChip – 2014-03-19T20:01:31.577
Reopen! F#:
let d a b = -1+Seq.find((*)b>>(<)a)[1..a]
– Mau – 2014-03-20T12:14:25.897It should have stayed closed if you are going to Accept an answer which does not follow spec and only a short time after re-opening. What a waste. – Bill Woodger – 2014-03-21T09:39:37.033