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

Answer by gnibbler for Implement division using only addition

$
0
0

Python - 0 additions

from itertools import repeat, countdef divide(a, b):    i = repeat(0, a)    try:        for j in count():            for k in repeat(0, b):                next(i)    except:        return j

This uses an iterator of length a, and consumes it in groups of b until StopIteration is raised. At this point j contains the result.


Viewing all articles
Browse latest Browse all 18

Trending Articles