Find the thirteen adjacent digits in the 1000-digit number n that have the greatest product and returns it. >>> solution("13978431290823798458352374") 609638400 >>> solution("13978431295823798458352374") 2612736000 >>> solution("1397843129582379841238352374") 209018
(n: str = N)
| 58 | |
| 59 | |
| 60 | def solution(n: str = N) -> int: |
| 61 | """ |
| 62 | Find the thirteen adjacent digits in the 1000-digit number n that have |
| 63 | the greatest product and returns it. |
| 64 | |
| 65 | >>> solution("13978431290823798458352374") |
| 66 | 609638400 |
| 67 | >>> solution("13978431295823798458352374") |
| 68 | 2612736000 |
| 69 | >>> solution("1397843129582379841238352374") |
| 70 | 209018880 |
| 71 | """ |
| 72 | |
| 73 | largest_product = -sys.maxsize - 1 |
| 74 | for i in range(len(n) - 12): |
| 75 | product = 1 |
| 76 | for j in range(13): |
| 77 | product *= int(n[i + j]) |
| 78 | largest_product = max(largest_product, product) |
| 79 | return largest_product |
| 80 | |
| 81 | |
| 82 | if __name__ == "__main__": |