15
2
Implement a division algorithm in your favourite language which handles integer division. It need only handle positive numbers - but bonus points if it handles negative and mixed-sign division, too. Results are rounded down for fractional results.
The program may not contain the /
, \
, div
or similar operators. It must be a routine which does not use native division capabilities of the language.
You only need to handle up to 32-bit division. Using repeated subtraction is not allowed.
Input
Take two inputs on stdin separated by new lines or spaces (your choice)
740
2
Output
In this case, the output would be 370
.
The solution which is the shortest wins.
does golfscript's
\
operator (swap two elements) count as forbidden? – John Dvorak – 2013-05-01T16:07:47.193You should have waited to accept an answer. There's a shorter one now. – Pavel – 2016-12-08T04:07:23.160
What does your '' operator mean? – sergiol – 2018-06-08T23:05:37.970
2this is shortest-time but I haven't seen anyone time the code – phuclv – 2014-06-09T13:45:33.440
"Innovative solutions that do not use repeated subtraction" – so good old long division? Hardly innovative, that, though. – Joey – 2011-08-10T08:05:51.177
is
740,2
also permitted for the input? ie comma separated? – gnibbler – 2011-02-04T10:44:37.817"Results are rounded down for fractional results" - ok, so apparently the input can also result in a non-integer number... But what about the divisor being larger than the divided (say, 5 and 10) - is that permitted or not? – Aurel Bílý – 2011-02-04T12:13:40.870
@gnibber That would be fine, but make it clear in the program description. – Thomas O – 2011-02-04T12:23:44.617
@Aurel300 It is not required to have fractional output. 1/7 may produce
0.142857
... or0
. – Thomas O – 2011-02-04T12:24:36.3002is using exponentials and other math functions really allowed? they use division behind the scenes, because many solutions are doing ab⁻¹ – Ming-Tang – 2011-02-05T07:12:36.000
@SHiNKiROU - yeah I would consider that cheating too... – Aurel Bílý – 2011-02-05T10:43:59.103