Skip to content

Instantly share code, notes, and snippets.

@hyfather
Created November 9, 2011 04:49
Show Gist options
  • Save hyfather/1350428 to your computer and use it in GitHub Desktop.
Save hyfather/1350428 to your computer and use it in GitHub Desktop.
My solution to problem #8 on projecteuler.net
#"Find the greatest product of five consecutive digits in the 1000-digit number."
# http://projecteuler.net/problem=8
big = "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522\
74430435576689664895044524452316173185640309871112172238311362229893423380308135336276614282806444486645238749303589072962904915604407723907138105158593079608667017242712188399879790879227492190169\
97208880937766572733300105336788122023542180975125454059475224352584907711670556013604839586446706324415722155397536978179778461740649551492908625693219784686224828397224137565705605749026140797296\
86524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054\
42175069416589604080719840385096245544436298123098787992724428490918884580156166097919133875499200524063689912560717606058861164671094050775410022569831552000559357297257163626956188267042825248360\
0823257530420752963450".split(//).map(&:to_i)
def slide(f, head, *tail)
tail.empty? ? 0 : [f.([head] + tail), slide(f, *tail)].max
end
p slide(->(list){list.take(5).inject(:*)}, *big)
#=> 40824
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment