Skip to content

Instantly share code, notes, and snippets.

@JontyMC
Created October 22, 2014 15:27
Show Gist options
  • Save JontyMC/504967ef83765ab52d07 to your computer and use it in GitHub Desktop.
Save JontyMC/504967ef83765ab52d07 to your computer and use it in GitHub Desktop.
Largest product in a series
digits :: Integral x => x -> [x]
digits 0 = []
digits x = digits (div x 10) ++ [mod x 10]
largestProduct :: Int -> Integer -> Integer
largestProduct digitCount number =
largestProduct' digitCount (digits number)
largestProduct' :: Int -> [Integer] -> Integer
largestProduct' digitCount list
| (length list) < digitCount = 0
| otherwise = max (product (take digitCount list)) (largestProduct' digitCount (tail list))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment