## Posts Tagged ‘multiplication’

### Programming Praxis – Big Numbers: Addition, Subtraction, And Multiplication

May 31, 2011

In today’s Programming Praxis exercise, we’re going to add addition, subtraction and multiplication to our big number library. Let’s get started, shall we?

Addition and multiplication are both functions of the Num typeclass, so we add the two functions (the fact that a – b is equal to a + (-b) is already present in the Num typeclass, so since we already defined negate we don’t need to define the subtraction function) in our Num instance:

```instance Num BigNum where
```

For addition, we need to decide whether we’re adding or subtracting, which we do for each pair of digit groups. We could do this digit by digit, but I’m going to be lazy and do it per group. The conversion to and from Integers is not needed now, but will be required once the base is increased to prevent overflowing the Int type.

```    a@(B l1 ds1) + b@(B l2 ds2) = B (length ds * signum l) ds where
B l _ = if abs b > abs a then b else a
ds = f 0 \$ (if abs b > abs a then flip else id)
(prep \$ if signum l1 == -signum l2 then (-) else (+)) ds1 ds2
prep op (x:xs) (y:ys) = op (toInteger x) (toInteger y) : prep op xs ys
prep _  xs     ys     = map toInteger \$ xs ++ ys
f r (x:xs) = let (d,m) = divMod (r + x) base in fromIntegral m : f d xs
f r []     = if r == 0 then [] else [fromIntegral r]
```

For multiplication we use the grade school method of multiplying each digit group (again, instead of per-digit) and summing them up at the end.

```    (B l1 ds1) * (B l2 ds2) = B (signum l1 * signum l2 * sl) sds where
B sl sds = sum \$ mult ds1 ds2
mult (x:xs) (y:ys) = fromIntegral (toInteger x * toInteger y) :
map shift (mult xs (y:ys)) ++
map shift (mult [x] ys)
mult _     _  = []
shift (B l ds) = B (l + 1) (0 : ds)```

Some tests to see if everything is working properly:

```main :: IO ()
main = do print \$  12345678 +  987654321  == ( 999999999 :: BigNum)
print \$  12345678 -  987654321  == (-975308643 :: BigNum)
print \$ 987654321 -   12345678  == ( 975308643 :: BigNum)
print \$ -12345678 +  987654321  == ( 975308643 :: BigNum)
print \$ -12345678 -   87654321  == ( -99999999 :: BigNum)
print \$  12345678 *   87654321  == ( 1082152022374638 :: BigNum)
print \$  12345678 * (-87654321) == (-1082152022374638 :: BigNum)
print \$ -12345678 *   87654321  == (-1082152022374638 :: BigNum)
print \$ -12345678 * (-87654321) == ( 1082152022374638 :: BigNum)
```

### Programming Praxis – Three Binary Algorithms

January 15, 2010

In today’s Programming Praxis we have to implement binary algorithms for multiplying, dividing, and finding the greatest common divisor of two numbers. Let’s get started.

Since all our functions require the Bits typeclass, for which Haskell doesn’t do type defaulting, we use the following language pragma so we don’t have to specify types in the tests.

```{-# LANGUAGE ExtendedDefaultRules #-}
```

We need an import to do bitshifting.

```import Data.Bits
```

And because we’re going to be doing quite a bit of it, two quick convenience convenience functions for doubling and halving numbers:

```left, right :: Bits a => a -> a
left = flip shiftL 1
right = flip shiftR 1
```

Binary multiplication. Piece of cake.

```binmult :: (Bits a, Integral a) => a -> a -> a
binmult 1 b = b
binmult a b = binmult (right a) (left b) + if odd a then b else 0
```

Binary division. By using the until function we don’t have use explicit recursion to find t.

```bindiv :: (Bits a, Ord a) => a -> a -> (a, a)
bindiv n d = f (right \$ until (> n) left d) 0 n where
f t q r | t < d     = (q, r)
| t <= r    = f (right t) (left q + 1) (r - t)
| otherwise = f (right t) (left q)     r
```

Binary gcd. A lot of different conditions, but all very straightforward.

```bingcd :: (Bits a, Integral a) => a -> a -> a
bingcd a 0 = a
bingcd 0 b = b
bingcd a b | even a && even b = 2 * bingcd (right a) (right b)
| even a           = bingcd (right a) b
| even b           = bingcd a (right b)
| a > b            = bingcd (a - b) b
| otherwise        = bingcd a (b - a)
```

A quick test shows that everything is working correctly:

```main :: IO ()
main = do print \$ binmult 14 12
print \$ bindiv 837 43
print \$ bingcd 2322 654
```