Quantcast
Channel: Implement division using only addition - Code Golf Stack Exchange
Viewing all articles
Browse latest Browse all 18

Answer by Joe Z. for Implement division using only addition

$
0
0

Python, 320703 additions

def divide(a, b):    quotient = 0    c = 0    d = 0    while add(d, b) <= a:        c = add(c, 1)        d = add(d, b)    return c

As always, a last-place reference answer. This simply adds 1 to a "quotient" and b to a "remultiplication" variable until it hits a.

Here is the debugging code:

add_used = 0def add(a, b):    global add_used    add_used += 1    return a + bfor a in range(1, 201):    for b in range(1, a+1):        print "%d / %d = %d" % (a, b, divide(a, b))print "Additions used:", add_used

Viewing all articles
Browse latest Browse all 18

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>