find the thirteen adjacent digits in the 1000-digit number that have the greatest product. what is the value of this product? in python

Here's one way to solve the problem:

main.py
number = "73167176531330624919225119674426574742355349194934...<rest of number>"
num_digits = 13
max_product = 0

for i in range(len(number) - num_digits):
    current_product = 1
    for j in range(num_digits):
        current_product *= int(number[i+j])
    if current_product > max_product:
        max_product = current_product

print(max_product)
352 chars
13 lines

Explanation:

  • We first define the input number and the number of adjacent digits we want to find the product for.
  • We then loop through the indices of the number up to len(number) - num_digits since we want to be able to obtain the full range of num_digits from each starting index.
  • For each starting index, we calculate the product of the num_digits adjacent digits using another loop and multiplying them together.
  • We compare the current calculated product with the max_product and update it if it's greater.
  • Finally, we print out the max_product.

Note: The input number is assumed to be stored as a string.

gistlibby LogSnag