Last active
December 24, 2015 16:38
-
-
Save laurilehmijoki/6829074 to your computer and use it in GitHub Desktop.
Find the maximum product of five consecutive integers.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
In Clojure: https://gist.github.com/laurilehmijoki/6829426
In Java 8: https://gist.github.com/laurilehmijoki/6840530
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Is there a ready-made partition function in the standard library?