Skip to content

Instantly share code, notes, and snippets.

@laurilehmijoki
Last active December 24, 2015 16:38
Show Gist options
  • Save laurilehmijoki/6829074 to your computer and use it in GitHub Desktop.
Save laurilehmijoki/6829074 to your computer and use it in GitHub Desktop.
Find the maximum product of five consecutive integers.
import Data.Char(digitToInt)
nums = "37900490610897696126265185408732594047834333441947018503938074170641817003483791166860080189669498677558722248271653685006165703758078020538662914584106964490601037178417735301109842904952970798120105470168021976855478449620066905768943533366888238302291333721473491149055521813412305168905832929411783011983450277211542535458190375258738804563705619552777408744641552952789449531990152618001564228057277177446096431068469989305514445184509262635998279063901081322647763278370447051079759349248247518"
partition _ [] = []
partition n xs = [take n xs] ++ (partition n $ drop n xs)
partitioned = map (map digitToInt) $ partition 5 nums
maxProduct = maximum $ map (foldl1 (*)) partitioned
main = do
putStr $ show maxProduct -- will print 34992
@laurilehmijoki
Copy link
Author

Is there a ready-made partition function in the standard library?

@laurilehmijoki
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment