     (19 votes, average: 5.00 out of 5) Loading...

## Project Euler 8 Solution ## Project Euler 8: Find the largest product of 13 consecutive digits.

#### Project Euler 8 Problem Description

Project Euler 8: Find the greatest product of 13 consecutive digits for a large number.

The four adjacent digits in the 1000-digit number that have the greatest product are 9 × 9 × 8 × 9 = 5832.

```73167176531330624919225119674426574742355349194934
96983520312774506326239578318016984801869478851843
... {continued}
```

Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product?

#### Analysis All the lines of numbers are to be considered as one, very long, 1000-digit number and are presented this way for readability. The objective is to canvass this number and find the greatest product of 13 contiguous digits.

We begin by opening and reading the file “pe8.txt” (the second tab next to the main.py tab in the Trinket window) and concatenate each line into one long string by stripping (`rstrip` method) whitespace (which includes the new line character, \n) from the end of the lines. Next, each character is separated, converted to an integer digit, and added to a list named `d`.

```d = map(int, ''.join(line.rstrip() for line in open('pe8.txt')))
```

If you can be assured that a group product greater than zero exists, you could map individual zeros and move the search pointer forward. But, hey, this same algorithm solved the much more aggressive HackerRank version just fine.

A length (`L`) is set defining the search length, 13 for this exercise, and we make sure there are enough digits to search. If not we exit printing “None.”

If there are enough digits, we consider each 13-digit set until the end, taking into account the search length: 1000-13+1 = 988. The `reduce` function runs each set through a multiplier and we capture the maximum product of all sets.

```L = 13
print 'Greatest product of', L, 'consecutive \ndigits =',
print "None."  if L>len(d) else  \
max(reduce(mul, d[i:i+L]) for i in xrange(len(d)-L+1))
```

80% of the Python solutions listed in the Project Euler forum are wrong as they don’t consider the last digit in the search space. Because the solution to this problem lies in the middle somewhere, these programs calculate a correct response in this instance. If you were to run them with the following parameters they would fail by erroneously producing a zero instead of 18:
d = [0,0,0,1,9,2]
L = 3

Further, many of these same erroneous programs would return an error or zero (a distinct possibility) which could be misleading; it’s better to return some indication that an answer cannot be calculated, such as “None,” to indicate an undefined condition:
d = [0,0,0,1,9,2]
L = 13

HackerRank Project Euler 8 steps up the complexity a bit by running up to 100 trials and changing the search length from a fixed 13 digits to a variable 1 through 7 digits. The search object can range from 1 to 1000 digits and will be at least the size of the search length. This program and method
solves all test cases for
Project Euler 8 on HackerRank

#### Project Euler 8 Solution

Runs < 0.002 seconds in Python 2.7. Use this link to get the Project Euler 8 Solution Python 2.7 source.

#### Afterthoughts

• PEP 8 is the style guide for Python. It describes the preferred methods for formatting Python source code for a consistent appearance. We do our best to follow those guidelines to make the code as readable as possible.

Project Euler 8

Project Euler 8 Solution last updated