Ethiopian multiplication: Difference between revisions

Content added Content deleted
Line 196: Line 196:
| even plier = ethiopicmult' (plier `div` 2) (pliand * 2) acc
| even plier = ethiopicmult' (plier `div` 2) (pliand * 2) acc
| otherwise = ethiopicmult' (plier `div` 2) (pliand * 2) (acc + pliand)</lang>
| otherwise = ethiopicmult' (plier `div` 2) (pliand * 2) (acc + pliand)</lang>

Alternately:
<lang haskell>ethiopicmult :: Integral a => a -> a -> a
ethiopicmult x y = sum [pliand | (plier, pliand) <- rowsNonZero, odd plier]
where rowsNonZero = takeWhile ((/= 0) . fst) rows
rows = zip (iterate (`div` 2) x) (iterate (* 2) y)</lang>


'''Usage example''' from the interpreter
'''Usage example''' from the interpreter