Advertisement
banovski

Project Euler, Problem #8, Python

Dec 24th, 2021 (edited)
885
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.92 KB | None | 0 0
  1. #! /usr/bin/env python3
  2.  
  3. # The four adjacent digits in the 1000-digit number
  4. # 731671765313306249192251196744265747423553491949349698352031277450632623957831
  5. # 801698480186947885184385861560789112949495459501737958331952853208805511125406
  6. # 987471585238630507156932909632952274430435576689664895044524452316173185640309
  7. # 871112172238311362229893423380308135336276614282806444486645238749303589072962
  8. # 904915604407723907138105158593079608667017242712188399879790879227492190169972
  9. # 088809377665727333001053367881220235421809751254540594752243525849077116705560
  10. # 136048395864467063244157221553975369781797784617406495514929086256932197846862
  11. # 248283972241375657056057490261407972968652414535100474821663704844031998900088
  12. # 952434506585412275886668811642717147992444292823086346567481391912316282458617
  13. # 866458359124566529476545682848912883142607690042242190226710556263211111093705
  14. # 442175069416589604080719840385096245544436298123098787992724428490918884580156
  15. # 166097919133875499200524063689912560717606058861164671094050775410022569831552
  16. # 0005593572972571636269561882670428252483600823257530420752963450
  17. # that have the greatest product are 9 × 9 × 8 × 9 = 5832. Find the
  18. # thirteen adjacent digits in the 1000-digitnumber that have the
  19. # greatest product. What is the value of this product?
  20.  
  21. # The input value shouldn't be formatted like this, but if it's not split into
  22. # lines, Pastebin aparently deems the code dangerous.
  23. a = 73167176531330624919225119674426574742355349194934969835203127745
  24. 063262395783180169848018694788518438586156078911294949545950173795833
  25. 195285320880551112540698747158523863050715693290963295227443043557668
  26. 966489504452445231617318564030987111217223831136222989342338030813533
  27. 627661428280644448664523874930358907296290491560440772390713810515859
  28. 307960866701724271218839987979087922749219016997208880937766572733300
  29. 105336788122023542180975125454059475224352584907711670556013604839586
  30. 446706324415722155397536978179778461740649551492908625693219784686224
  31. 828397224137565705605749026140797296865241453510047482166370484403199
  32. 890008895243450658541227588666881164271714799244429282308634656748139
  33. 191231628245861786645835912456652947654568284891288314260769004224219
  34. 022671055626321111109370544217506941658960408071984038509624554443629
  35. 812309878799272442849091888458015616609791913387549920052406368991256
  36. 071760605886116467109405077541002256983155200055935729725716362695618
  37. 82670428252483600823257530420752963450
  38.  
  39. h = e = g = 1
  40. f = i = 0
  41.  
  42. for b in range(999, 11, -1):
  43.     c = a
  44.     for d in range(b, b - 13, -1):
  45.         e = c // 10 ** d
  46.         f += e * 10 ** (12 - (b - d))
  47.         g *= e
  48.         c -= e * 10 ** d
  49.     if g > h:
  50.         h = g
  51.         i = f
  52.     j = a // 10 ** b
  53.     a -=  j * 10 ** b
  54.     g = 1
  55.     f = 0
  56.  
  57. print("The sequence is ", i, ", its product is ", h, sep="")
  58.  
  59. # The sequence is 5576689664895, its product is 23514624000
  60. # 4 function calls in 0.168 seconds
  61.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement