how to use nested for loops, with ranges (includes string index) to clean up a long multiplication code line

Question:

I wrote some code to solve project Eulers #8. I have seen a few other solutions to this problem on line, but I do not understand how they create the nested loop, or it is a different method.

Problem OVERVIEW: Find the thirteen adjacent digits in the 1000-digit number(string with no delimitator) that have the greatest product. What is the value of this product?

I got it to work, but I could not find a way to simplify my product of 13 numbers from a string of numbers.

Here is what I had in mind:

num_string = '73167176531330624919225119674426574742355349...'
length = 13
products_of_13 = []
largest_product_of_13 =0

for i in range(0,(len(num_string) - 12)):
    product_13=0
    for j in range (i,i+12):
        product_13 *= int(num_string[j])
    products_of_13.append(product_13)

for num in products_of_13:
    if num > largest_product_of_13:
       # print(product_13)
        largest_product_of_13 = num

print(largest_product_of_13)

This only resulted in an answer of 0 every time. I added

print(int(num_string[j]))

into the above code to check that I am getting the correct numbers, but it looks as though a zero is being put between each iteration of the loop. Making everything zero.

QUESTION: How do I make a loop to calculate the product_13 , to avoid typing out each string index and turning it in to an integer?

I tried so many things that did not work I used the below code that is horrible to read:

num_string='''7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450'''


products_of_13 = []
largest_product_of_13 =0

for i in range(0,(len(num_string) - 12)):
    product_13= int(num_string[i])*int(num_string[i+1])*int(num_string[i+2])*int(num_string[i+3])*int(num_string[i+4])*int(num_string[i+5])*int(num_string[i+6])*int(num_string[i+7])*int(num_string[i+8])*int(num_string[i+9])*int(num_string[i+10])*int(num_string[i+11])*int(num_string[i+12])
    products_of_13.append(product_13)

for num in products_of_13:
    if num > largest_product_of_13:
       # print(product_13)
        largest_product_of_13 = num

print(largest_product_of_13)

UPTDATE: solution is 23514624000

Asked By: user20303754

||

Answers:

You need to assign product_13 = 1 instead of 0. This is because any number multiplied by 0 will give a result of 0 only.

Answered By: Tarun Kumar Sao

I tried this question on my laptop and I came up with this solution

In your solution, the product seems to be static that may create problem.

Also a suggestion, to multiple number in the string you can use
reduce(lambda x,y: x*y, map(int, your_string))

from functools import reduce
a = """73167176531330624919225119674426574742355349194934
96983520312774506326239578318016984801869478851843
85861560789112949495459501737958331952853208805511
12540698747158523863050715693290963295227443043557
66896648950445244523161731856403098711121722383113
62229893423380308135336276614282806444486645238749
30358907296290491560440772390713810515859307960866
70172427121883998797908792274921901699720888093776
65727333001053367881220235421809751254540594752243
52584907711670556013604839586446706324415722155397
53697817977846174064955149290862569321978468622482
83972241375657056057490261407972968652414535100474
82166370484403199890008895243450658541227588666881
16427171479924442928230863465674813919123162824586
17866458359124566529476545682848912883142607690042
24219022671055626321111109370544217506941658960408
07198403850962455444362981230987879927244284909188
84580156166097919133875499200524063689912560717606
05886116467109405077541002256983155200055935729725
71636269561882670428252483600823257530420752963450"""
a = a.replace("n", "") # convert multi line string to single string
i = 0 # start from index 0
adj = 13 # how many adjacent characters to take
max_ = -1 # store max value as -1 from start
res = ""  # store the result as string in this

while i<(len(a)-adj): # loop from i=0 till length of string - adjacent characters
  start = a[i:i+adj]  # take the substring from given index to next 13 characters
  prod = reduce(lambda x,y: x*y, map(int, start)) # this will multiply all integers in substring and get the product of those
  max_ = max(max_, prod) # check if the product obtained above is more than max if yes than store the res
  if max_ == prod:
    res = start
  i += 1
res
# output : 5576689664895

this solution can be further optimised by taking the window of 13 string and calculating the product for first 13 numbers, then divide the number by 1st number, and multiply it by the 13th number.
this way we don’t need to multiply the whole 13 numbers again and again.

Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.