-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp008.py
42 lines (36 loc) · 1 KB
/
p008.py
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
40
41
42
#!/usr/bin/env python
"""
Largest product in a series
Problem 8
The four adjacent digits in the 1000-digit number that have the greatest
product are 9 * 9 * 8 * 9 = 5832
Find the thirteen adjacent digits in the 1000-digit number that have the
greatest product. What is the value of this product?
"""
str1000digit = ''
with open('p008_1000digit.txt') as file1000digit:
for line in file1000digit:
str1000digit += line.strip()
consecutive = []
maxadjacent = 13
largeProd = 0
i = 0
j = 0
while i < 1000:
if i+maxadjacent < 1000:
for j in range(0, maxadjacent):
if str1000digit[i+j] != "0":
consecutive.append(int(str1000digit[i+j]))
else:
consecutive = []
i += j
break
if len(consecutive) == maxadjacent:
prod = 1
for n in consecutive:
prod *= n
if prod > largeProd:
largeProd = prod
consecutive = []
i += 1
print(largeProd)