-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path8_largest_product_in_a_series.rb
43 lines (34 loc) · 1.43 KB
/
8_largest_product_in_a_series.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# Find the greatest product of five consecutive
# digits in the 1000-digit number.
number = "7316717653133062491922511967442657474235
53491949349698352031277450632623957831801698480186
94788518438586156078911294949545950173795833195285
32088055111254069874715852386305071569329096329522
74430435576689664895044524452316173185640309871112
17223831136222989342338030813533627661428280644448
66452387493035890729629049156044077239071381051585
93079608667017242712188399879790879227492190169972
08880937766572733300105336788122023542180975125454
05947522435258490771167055601360483958644670632441
57221553975369781797784617406495514929086256932197
84686224828397224137565705605749026140797296865241
45351004748216637048440319989000889524345065854122
75886668811642717147992444292823086346567481391912
31628245861786645835912456652947654568284891288314
26076900422421902267105562632111110937054421750694
16589604080719840385096245544436298123098787992724
42849091888458015616609791913387549920052406368991
25607176060588611646710940507754100225698315520005
59357297257163626956188267042825248360082325753042
0752963450"
numbers = number.gsub("/n",'').split("").map {|n| n.to_i}
digit_count = numbers.count
greatest_product = 0
product = 0
0.upto(digit_count - 5) do |n|
product = numbers[n] * numbers[n + 1] * numbers[n + 2] * numbers[n + 3] * numbers[n + 4]
if product > greatest_product
greatest_product = product
end
end
puts greatest_product