Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- The four adjacent digits in the 1000-digit number
- -- 731671765313306249192251196744265747423553491949349698352031277450632623957831
- -- 801698480186947885184385861560789112949495459501737958331952853208805511125406
- -- 987471585238630507156932909632952274430435576689664895044524452316173185640309
- -- 871112172238311362229893423380308135336276614282806444486645238749303589072962
- -- 904915604407723907138105158593079608667017242712188399879790879227492190169972
- -- 088809377665727333001053367881220235421809751254540594752243525849077116705560
- -- 136048395864467063244157221553975369781797784617406495514929086256932197846862
- -- 248283972241375657056057490261407972968652414535100474821663704844031998900088
- -- 952434506585412275886668811642717147992444292823086346567481391912316282458617
- -- 866458359124566529476545682848912883142607690042242190226710556263211111093705
- -- 442175069416589604080719840385096245544436298123098787992724428490918884580156
- -- 166097919133875499200524063689912560717606058861164671094050775410022569831552
- -- 0005593572972571636269561882670428252483600823257530420752963450
- -- that have the greatest product are 9 × 9 × 8 × 9 = 5832. Find the
- -- thirteen adjacent digits in the 1000-digitnumber that have the
- -- greatest product. What is the value of this product?
- -- The input value shouldn't be formatted like this, but if it's not split into
- -- lines, Pastebin aparently deems the code dangerous.
- inputValue =
- 73167176531330624919225119674426574742355349194934969835203127745063262395783180
- 16984801869478851843858615607891129494954595017379583319528532088055111254069874
- 71585238630507156932909632952274430435576689664895044524452316173185640309871112
- 17223831136222989342338030813533627661428280644448664523874930358907296290491560
- 44077239071381051585930796086670172427121883998797908792274921901699720888093776
- 65727333001053367881220235421809751254540594752243525849077116705560136048395864
- 46706324415722155397536978179778461740649551492908625693219784686224828397224137
- 56570560574902614079729686524145351004748216637048440319989000889524345065854122
- 75886668811642717147992444292823086346567481391912316282458617866458359124566529
- 47654568284891288314260769004224219022671055626321111109370544217506941658960408
- 07198403850962455444362981230987879927244284909188845801561660979191338754992005
- 24063689912560717606058861164671094050775410022569831552000559357297257163626956
- 1882670428252483600823257530420752963450
- numberToDigits :: Integral t => t -> [t]
- numberToDigits 0 = []
- numberToDigits x = numberToDigits (div x 10) ++ [mod x 10]
- products :: Num a => [a] -> [a]
- products (x:xs) =
- if length (x : xs) >= 13
- then product (x : take 12 xs) : products xs
- else [1]
- maximumValue :: Integer
- maximumValue = maximum $ products $ numberToDigits inputValue
- productsCheck :: [Integer] -> [Integer]
- productsCheck (x:xs) =
- if product (x : take 12 xs) == maximumValue
- then x : take 12 xs
- else productsCheck xs
- listToNumber :: Num a => [a] -> a
- listToNumber xs = sum [x * 10 ^ y | (x, y) <- reverse $ zip (reverse xs) [0 ..]]
- main :: IO ()
- main =
- print (maximumValue, listToNumber $ productsCheck $ numberToDigits inputValue)
- -- (23514624000,5576689664895)
- -- real 0m0,036s
- -- user 0m0,036s
- -- sys 0m0,000s
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement