Problem 8 — Largest Product in a Series

solved Tue, 8 Oct 2019, 13:23 · level 0 · difficulty 3% projecteuler.net github

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

73167176531330624919225119674426574742355349194934
96983520312774506326239578318016984801869478851843
85861560789112949495459501737958331952853208805511
12540698747158523863050715693290963295227443043557
66896648950445244523161731856403098711121722383113
62229893423380308135336276614282806444486645238749
30358907296290491560440772390713810515859307960866
70172427121883998797908792274921901699720888093776
65727333001053367881220235421809751254540594752243
52584907711670556013604839586446706324415722155397
53697817977846174064955149290862569321978468622482
83972241375657056057490261407972968652414535100474
82166370484403199890008895243450658541227588666881
16427171479924442928230863465674813919123162824586
17866458359124566529476545682848912883142607690042
24219022671055626321111109370544217506941658960408
07198403850962455444362981230987879927244284909188
84580156166097919133875499200524063689912560717606
05886116467109405077541002256983155200055935729725
71636269561882670428252483600823257530420752963450

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


Problem Analysis

This problem asks for the maximum product of any run of length consecutive digits inside a fixed 1000-digit number. The structure is a classic sliding window problem: I slide a window of fixed width across the sequence, compute a quantity — here the product of the digits — for each position, and track the best value seen. There is no number theory or combinatorics hidden here; the difficulty is purely about doing the obvious thing correctly while watching for two pitfalls: integer overflow and the presence of zero digits.

The search space is tiny. For a string of length N and a window of width L there are exactly N - L + 1 window positions, and each costs up to L multiplications, giving O(N · L) complexity — the product of input size and window width. With N = 1000 and L = 13 that is under 13,000 operations, effectively instantaneous, so a plain brute-force search over every window is entirely sufficient. The one structural fact worth exploiting is that any window containing the digit '0' has a product of zero, which can never beat a window of non-zero digits. The other thing I must reason about up front is the magnitude of the answer: thirteen nines multiply to 9^13 = 2,541,865,828,329, which exceeds the range of a 32-bit integer, so the accumulator must be at least 64 bits wide.

Solution Approach

Sliding-Window Scan (Python and C)

Both the Python and C solutions implement the same sliding window scan: visit every window position, compute its product, and keep the maximum. The two versions differ only idiomatically.

I store the 1000-digit number as a compile-time string constant in both languages — a Python str literal and a C static const char *. In C, adjacent string literals are concatenated by the compiler at no runtime cost, so I can break the number across many lines for readability while still producing one contiguous string. There is no I/O, no parsing, and no allocation for the data itself.

For each starting index i from 0 to N - length inclusive, the algorithm does two things. First it checks whether the window contains a '0', skipping it if so. Second, for surviving windows it multiplies the length digits together and updates the running best. In C I extract each digit with the idiom number[j] - '0', which converts an ASCII character to its numeric value because the C standard guarantees the digits '0' through '9' have consecutive codes. The product accumulator and the return type are both long long to hold the worst-case 9^13 safely.

The Python version expresses the same logic in a single generator expression: each window substring number[i:i+length] is produced lazily, a guard clause if "0" not in sequence filters out zero-containing windows, math.prod over (int(d) for d in sequence) computes each product, and the outer max(...) selects the largest. Python's arbitrary-precision integers mean overflow is never a concern, so no type reasoning is needed.

In both solutions the window length is a parameter rather than a hard-coded constant — read from argv in C and passed as a positional argument in Python. This lets the development case (length = 4, expected 5832) and the main case (length = 13, expected 23514624000) run against the same untouched source.

workbench

Test cases

categoryinputanswer
dev length = 4 5832
main length = 13 23514624000

Results

solutioncategoryanswerverdict avg seconds cavg seconds py
p0008_s0 dev 5832 correct 0.000004 (105) 0.000357 (105)
p0008_s0 main 23514624000 correct 0.000006 (105) 0.000323 (105)

Files

Tags & blogs

domain Digit sum

Blogs