p0008_s0.py
problem 8 · 44 lines
#!/usr/bin/env python3.14
# -*- coding: utf-8 -*-
""" Solution to Euler Problem 8: Largest Product in a Series [Level 0]. """
from __future__ import annotations
import math
from solver.runners import runner
number = ("73167176531330624919225119674426574742355349194934969835203127745063262"
"39578318016984801869478851843858615607891129494954595017379583319528532"
"08805511125406987471585238630507156932909632952274430435576689664895044"
"52445231617318564030987111217223831136222989342338030813533627661428280"
"64444866452387493035890729629049156044077239071381051585930796086670172"
"42712188399879790879227492190169972088809377665727333001053367881220235"
"42180975125454059475224352584907711670556013604839586446706324415722155"
"39753697817977846174064955149290862569321978468622482839722413756570560"
"57490261407972968652414535100474821663704844031998900088952434506585412"
"27588666881164271714799244429282308634656748139191231628245861786645835"
"91245665294765456828489128831426076900422421902267105562632111110937054"
"42175069416589604080719840385096245544436298123098787992724428490918884"
"58015616609791913387549920052406368991256071760605886116467109405077541"
"00225698315520005593572972571636269561882670428252483600823257530420752"
"963450")
@runner.main
def solve(*args: str) -> str:
"""Sliding-window brute force: largest digit-product over every length-wide window, skipping
windows that contain a zero; O(N·length) for N digits."""
length = runner.parse_int(args[0])
return str(max(
[
math.prod((int(d) for d in sequence))
for sequence in (number[i: i + length] for i in range(len(number) - length + 1))
if "0" not in sequence
]
))
if __name__ == "__main__":
raise SystemExit(solve())