From 8b04b13c8f194fdaafdd47a85a47571be234196c Mon Sep 17 00:00:00 2001 From: Yuri Tatishchev Date: Sat, 14 Feb 2026 23:58:43 -0800 Subject: [PATCH] hw1: impl multiply, power of --- hw1/BigNum.hs | 21 ++++++++++++++++++--- hw1/output | 13 +++++++++++++ 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/hw1/BigNum.hs b/hw1/BigNum.hs index d4cc234..86b1953 100644 --- a/hw1/BigNum.hs +++ b/hw1/BigNum.hs @@ -63,7 +63,7 @@ bigSubtract' (x:xs) [] b = let d = x - b else d : bigSubtract' xs [] 0 bigEq :: BigNum -> BigNum -> Bool -bigEq x y = stripLeadingZeroes x == stripLeadingZeroes y +bigEq x y = stripLeadingZeroes (reverse x) == stripLeadingZeroes (reverse y) bigDec :: BigNum -> BigNum bigDec x = bigSubtract x [1] @@ -73,10 +73,25 @@ bigDec x = bigSubtract x [1] -- If you are having trouble finding a solution, write a helper method that -- multiplies a BigNum by an Int. bigMultiply :: BigNum -> BigNum -> BigNum -bigMultiply _ _ = error "Your code here" +bigMultiply x y = reverse $ stripLeadingZeroes $ reverse $ bigMultiply' x y + +bigMultiply' :: BigNum -> BigNum -> BigNum +bigMultiply' _ [] = [] +bigMultiply' x (y:ys) = bigAdd (bigMultiplyBlock x y) (bigMultiply (0:x) ys) + +bigMultiplyBlock :: BigNum -> Block -> BigNum +bigMultiplyBlock x y = bigMultiplyBlock' x y 0 + +bigMultiplyBlock' :: BigNum -> Block -> Block -> BigNum +bigMultiplyBlock' [] _ 0 = [] +bigMultiplyBlock' [] _ c = [c] +bigMultiplyBlock' (x:xs) y c = let p = x * y + c + in (p `mod` maxblock) : bigMultiplyBlock' xs y (p `div` maxblock) bigPowerOf :: BigNum -> BigNum -> BigNum -bigPowerOf _ _ = error "Your code here" +bigPowerOf x [0] = [1] +bigPowerOf x [1] = x +bigPowerOf x y = bigMultiply x (bigPowerOf x (bigDec y)) prettyPrint :: BigNum -> String prettyPrint [] = "" diff --git a/hw1/output b/hw1/output index 42db385..d65fb42 100644 --- a/hw1/output +++ b/hw1/output @@ -10,4 +10,17 @@ Subtraction [1] [0] Multiplication +[12] +[0] +[392,296,4,12] +[518,250,645,161,37,915,479,1] +Power Of +[256] +[1] +Others 7 +63 +256 +800,000,000,000,000,000,002 +1 +59,745,672,527,794,294,248,045