1

I'm working my way through Project Euler using python and I'm currently working on problem 8. I've encountered a problem with the first way I tried solving the problem. My initial solving method is below

num = '''7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450'''

#print(stringNumber)
seriesLen = 13
firstIndex = 0
lastIndex = seriesLen
topValue = 0
topSeries = ''


while lastIndex < len(num):
    lastIndex = firstIndex + seriesLen
    number = int(num[firstIndex:lastIndex])
    print(firstIndex, lastIndex)
    lastIndex = firstIndex + seriesLen

    if len(str(number)) != lastIndex - firstIndex:
        print(lastIndex - firstIndex)
        print(len(str(number)))
    firstIndex += 1
#    print(number)

What this code is supposed to do is go through the entire 1000 digit number given in the problem 13 digits at a time and print those 13 digits. But, for some reason it doesn't always print 13 digits and I cannot figure out why. Sometimes it takes as few as 10 digits instead of 13. I've tried this with several different strings too and it does the same thing. I've talked to my Prof about this and she is quite certain the slicing is correct so I figured I'd post this issue here.

For those who know the problem I'm talking about here I am aware that this doesn't give me the answer to the problem - it's just a MWE to demonstrate my issue.

izhang05
  • 744
  • 2
  • 11
  • 26
  • could you share your output? – mauve Dec 05 '19 at 14:58
  • 1
    You convert the 13-digits string to int, then back to int. If it starts with zeros, it will be shorter in the end. – Thierry Lathuille Dec 05 '19 at 14:58
  • Does this answer your question? [Project Euler #8 in Python](https://stackoverflow.com/questions/19285079/project-euler-8-in-python) – ishan Dec 05 '19 at 15:00
  • See [this post](https://stackoverflow.com/a/1162959/10197418) for a nice idea how to iterate over a string in steps of n characters. Can easily be adapted for Python3. – FObersteiner Dec 05 '19 at 16:07

1 Answers1

3

This happens because you convert the sliced digits to an int here:

number = int(num[firstIndex:lastIndex])

This removes any leading zeros from the string, which is why you get variable lengths sometimes. Just remove this, and your code should work as expected.

number = num[firstIndex:lastIndex]
CDJB
  • 14,043
  • 5
  • 29
  • 55