Project Euler Problem #8:

Problem #8 is the first of many to involve finding some answer among a large amount of data. In this case, we are asked to investigate a crazy 1000-digit number. Here is the statement of the problem:

Project Euler Problem 8: Largest product in a series
The four adjacent digits in the 1000-digit number that have the greatest product are 9*9*8*9 = 5832.

73167176531330624919225119674426574742355349194934
96983520312774506326239578318016984801869478851843
85861560789112949495459501737958331952853208805511
12540698747158523863050715693290963295227443043557
66896648950445244523161731856403098711121722383113
62229893423380308135336276614282806444486645238749
30358907296290491560440772390713810515859307960866
70172427121883998797908792274921901699720888093776
65727333001053367881220235421809751254540594752243
52584907711670556013604839586446706324415722155397
53697817977846174064955149290862569321978468622482
83972241375657056057490261407972968652414535100474
82166370484403199890008895243450658541227588666881
16427171479924442928230863465674813919123162824586
17866458359124566529476545682848912883142607690042
24219022671055626321111109370544217506941658960408
07198403850962455444362981230987879927244284909188
84580156166097919133875499200524063689912560717606
05886116467109405077541002256983155200055935729725
71636269561882670428252483600823257530420752963450

Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product?

In most programming languages, just loading this crazy number into a variable would result in an error. As a result, I thought it would be better to store this number in a file and then to have my script read the number from the file and store it as a string. This led me to the following solution:

Solution #1: Brute Force, File-Reading Approach

Solving this problem is not the interesting part of this problem. The interesting part of this problem is loading the 1000-digit number into a variable which I can analyze in my script. As mentioned above, the way I did this was by copy pasting the 20 50-digit lines into a file which I titled “PE8Grid.txt”. Then I read from this file in my Python 2.7 script. (Shoutouts to Guru99 for teaching me how to read files in Python)

Regardless of how the number is loaded, solving the problem is quite straightforward. I opted for iterating through the digits and from each possible starting point for the 13-adjacent digits, I would independently calculate the product of those digits. If the product was greater than a running maximum, I would reset the running maximum. Clearly this method can be optimized by simply multiplying each new digit while dividing out old digits, but I didn’t want to deal with any messy casework involving ‘0’ digits. Here is an implementation of this method in Python 2.7:

 '''
 Author: Walker Kroubalkian
 Brute Force Approach to Project Euler Problem #8
 '''
 import time
 f = open("PE8Grid.txt","r")
 if f.mode == "r":
     contents = f.readlines()
     realContents = []
     for x in contents:
         if(x[len(x)-1] == "\n"):
             realContents.append(x[0:len(x)-1])
         else:
             realContents.append(x)
 else:
     raise ValueError("Cannot read from file")
 def projectEulerProblemEight(lines, n):
     s = ""
     maxProduct = 0
     for x in lines:
         s+=x
     length = len(s)
     for i in range(length-n):
         total = 1
         for j in range(n):
             total*=int(s[i+j])
         if(total>maxProduct):
             maxProduct = total
     return maxProduct
 start = time.time()
 print projectEulerProblemEight(realContents, 13)
 print ("--- %s seconds ---" % (time.time()-start))
 '''
 Prints
 23514624000
 --- 0.00584602355957 seconds ---
 for input of n = that crazy number
 '''

While this problem may be boring compared to the typical Project Euler problem, knowing how to read from files will prove to be an invaluable skill as we attempt more problems. That’s it folks! Thanks so much for reading.

Published by Walker Kroubalkian

My name is Walker Kroubalkian. I really enjoy math, computer science, and hiking.

Leave a comment

Design a site like this with WordPress.com
Get started