Skip to content

Commit

Permalink
some work towards big multiplication
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric Wilson committed Jan 26, 2011
1 parent 4e8c3b9 commit 105e250
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions EulerMath/BigNum.hs
Expand Up @@ -12,14 +12,23 @@ type BigNat = [Int]

bigSum :: [BigNat] -> BigNat
bigSum = carry . map sum . transpose
where
carry (n:m:ms) = mod n 10 : carry ((m + quot n 10) : ms)
carry (0:[]) = []
carry (n:[]) = carry [n,0]

carry :: BigNat -> BigNat
carry (n:m:ms) = mod n 10 : carry ((m + quot n 10) : ms)
carry (0:[]) = []
carry (n:[]) = carry [n,0]

stringToBigNat :: String -> BigNat
stringToBigNat = reverse . map digitToInt

bigNatToString :: BigNat -> [Char]
bigNatToString = map intToDigit . reverse

shift :: Int -> BigNat-> BigNat
shift n = (++) $ replicate n 0

timesDigit :: (Int, Int) -> BigNat -> BigNat
timesDigit (value, place) = carry . shift place . map (*value)

withPlace :: BigNat -> [(Int, Int)]
withPlace big = zip big [0..]

0 comments on commit 105e250

Please sign in to comment.