project-euler-100

[RADIOACTIVE] solutions to the first 100 challenges of project euler
git clone git://git.figbert.com/project-euler-100.git
Log | Files | Refs | README

problem008.py (1886B)


      1 # problemName = "Largest product in a series"
      2 # problemNum = 8
      3 # solutionBy = "FIGBERT"
      4 # language = "Python"
      5 # dateCompleted = "25/01/2020"
      6 from math import prod
      7 
      8 STRVAL = """
      9 73167176531330624919225119674426574742355349194934
     10 96983520312774506326239578318016984801869478851843
     11 85861560789112949495459501737958331952853208805511
     12 12540698747158523863050715693290963295227443043557
     13 66896648950445244523161731856403098711121722383113
     14 62229893423380308135336276614282806444486645238749
     15 30358907296290491560440772390713810515859307960866
     16 70172427121883998797908792274921901699720888093776
     17 65727333001053367881220235421809751254540594752243
     18 52584907711670556013604839586446706324415722155397
     19 53697817977846174064955149290862569321978468622482
     20 83972241375657056057490261407972968652414535100474
     21 82166370484403199890008895243450658541227588666881
     22 16427171479924442928230863465674813919123162824586
     23 17866458359124566529476545682848912883142607690042
     24 24219022671055626321111109370544217506941658960408
     25 07198403850962455444362981230987879927244284909188
     26 84580156166097919133875499200524063689912560717606
     27 05886116467109405077541002256983155200055935729725
     28 71636269561882670428252483600823257530420752963450
     29 """.strip()
     30 VAL = int("".join([line for line in STRVAL.splitlines()]))
     31 
     32 def product_of_string(string):
     33     return prod([int(i) for i in string])
     34 
     35 def find_largest_product(num):
     36     start, end, str_num = 0, 13, str(num)
     37     largest_product = 0
     38     while end <= len(str_num):
     39         if product_of_string(str_num[start:end]) > largest_product:
     40             largest_product = product_of_string(str_num[start:end])
     41         start += 1
     42         end += 1
     43     return largest_product
     44 
     45 if __name__ == "__main__":
     46     answer = find_largest_product(VAL)
     47     print((
     48         "The value of the greatest product of thirteen adjacent digits in "
     49         f"the provided 1000-digit number is {answer}"
     50     ))