Skip to content

Instantly share code, notes, and snippets.

@bryant
Last active December 26, 2015 23:39
Show Gist options
  • Save bryant/7231662 to your computer and use it in GitHub Desktop.
Save bryant/7231662 to your computer and use it in GitHub Desktop.
binary exponentiation. haskell.
import System.Environment (getArgs)
myPow x y
| y < 0 = error "neg."
| y == 0 = 1
| otherwise = pow' y x
where
pow' kth cur
| kth == 1 = cur
| odd kth = cur * pow' (kth `quot` 2) (cur * cur)
| otherwise = pow' (kth `quot` 2) (cur * cur)
main = do
ints <- fmap (map read) $ getArgs :: IO [Int]
print $ (head ints) `myPow` (last ints)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment