|
1 | 1 | # Instructions
|
2 | 2 |
|
3 |
| -Given a string of digits, calculate the largest product for a contiguous substring of digits of length n. |
| 3 | +Your task is to look for patterns in the long sequence of digits in the encrypted signal. |
4 | 4 |
|
5 |
| -For example, for the input `'1027839564'`, the largest product for a series of 3 digits is 270 `(9 * 5 * 6)`, and the largest product for a series of 5 digits is 7560 `(7 * 8 * 3 * 9 * 5)`. |
| 5 | +The technique you're going to use here is called the largest series product. |
6 | 6 |
|
7 |
| -Note that these series are only required to occupy *adjacent positions* in the input; the digits need not be *numerically consecutive*. |
| 7 | +Let's define a few terms, first. |
8 | 8 |
|
9 |
| -For the input `'73167176531330624919225119674426574742355349194934'`, |
10 |
| -the largest product for a series of 6 digits is 23520. |
| 9 | +- **input**: the sequence of digits that you need to analyze |
| 10 | +- **series**: a sequence of adjacent digits (those that are next to each other) that is contained within the input |
| 11 | +- **span**: how many digits long each series is |
| 12 | +- **product**: what you get when you multiply numbers together |
11 | 13 |
|
12 |
| -For a series of zero digits, you need to return the empty product (the result of multiplying no numbers), which is 1. |
| 14 | +Let's work through an example, with the input `"63915"`. |
13 | 15 |
|
14 |
| -~~~~exercism/advanced |
15 |
| -You do not need to understand why the empty product is 1 to solve this problem, |
16 |
| -but in case you are interested, here is an informal argument: if we split a list of numbers `A` into two new lists `B` and `C`, then we expect `product(A) == product(B) * product(C)` because we don't expect the order that you multiply things to matter; now if we split a list containing only the number 3 into the empty list and a list containing the number 3 then the product of the empty list has to be 1 for `product([3]) == product([]) * product([3])` to be true. |
17 |
| -
|
18 |
| -The same kind of argument justifies why the sum of no numbers is 0. |
19 |
| -~~~~ |
| 16 | +- To form a series, take adjacent digits in the original input. |
| 17 | +- If you are working with a span of `3`, there will be three possible series: |
| 18 | + - `"639"` |
| 19 | + - `"391"` |
| 20 | + - `"915"` |
| 21 | +- Then we need to calculate the product of each series: |
| 22 | + - The product of the series `"639"` is 162 (`6 × 3 × 9 = 162`) |
| 23 | + - The product of the series `"391"` is 27 (`3 × 9 × 1 = 27`) |
| 24 | + - The product of the series `"915"` is 45 (`9 × 1 × 5 = 45`) |
| 25 | +- 162 is bigger than both 27 and 45, so the largest series product of `"63915"` is from the series `"639"`. |
| 26 | + So the answer is **162**. |
0 commit comments