Stuck on Project Euler # 13
Question:
Please help me understand why my Python code to solve the 13th Project Euler problem is incorrect. I believe that I understand the task correctly and I think my code is correct, but it is obviously not.
number = '5000 digit number - see in the problem decription at the provided link'
list1 = [number[i:i+100] for i in range(0, len(number), 100)]
temp = []
for i in range(0, len(list1)):
y = int(list1[i])
temp.append(y)
print sum(temp)
Answers:
First, the numbers are 50 digits long, not 100. Change this:
list1 = [number[i:i+100] for i in range(0,len(number),100)]
To this:
list1 = [number[i:i+50] for i in range(0,len(number),50)]
Second, you’re printing the entire sum, rather than just the first ten digits. Try:
print str(sum(temp))[:10]
Much simpler:
s = 'copied and pasted from the page'
result = sum(map(int, s.splitlines()))[:10]
Only the 11 first digits need to be summed,
somme11=sum(int(number2[i:i+11]) for i in range(100))
print(somme11)
print( 'the ten first digits are' , somme11//1000)
Because the carry can’t exceed 99.
4893024188690
the ten first digits are 4893024188
An alternative is to load the numbers into a file and then just add them up, unless I too have completely misunderstood the question.
with open("sum.nums","r") as f:
data = f.readlines()
total = 0
for i in data:
total += int(i)
print "1st 10 digits ", str(total)[:10], "of total", total
1st 10 digits 5537376230 of total 5537376230390876637302048746832985971773659831892672
It’s pretty simple
def e13():
f=open("100x50digits.txt")
summ=0
for line in f:
summ+=int(line[:11])
print(int(summ/1000))
f.close()
e13()
number = '''The 100 lines of 50 characters'''
numbers = number.splitlines() #Devide the string into a list of strings.
numbers = [int(i) for i in numbers] #Convert all elements into integers.
totalSum = str(sum(numbers)) #Add everything and convert to a string.
print(totalSum[:10]) #Print the first 10 digits of the result.
The correct answer is 5537376230, checked with P.E.
The real challenge here is to deal with the really long string
Use StringIO
to take number as a input string and iterate trough elements by converting each one to a integer value
from io import StringIO
number = StringIO(""" # Paste one-hundred 50 numbers """)
print(str(sum(map(lambda x: int(x), number)))[:10])
>>> '5537376230'
Please help me understand why my Python code to solve the 13th Project Euler problem is incorrect. I believe that I understand the task correctly and I think my code is correct, but it is obviously not.
number = '5000 digit number - see in the problem decription at the provided link'
list1 = [number[i:i+100] for i in range(0, len(number), 100)]
temp = []
for i in range(0, len(list1)):
y = int(list1[i])
temp.append(y)
print sum(temp)
First, the numbers are 50 digits long, not 100. Change this:
list1 = [number[i:i+100] for i in range(0,len(number),100)]
To this:
list1 = [number[i:i+50] for i in range(0,len(number),50)]
Second, you’re printing the entire sum, rather than just the first ten digits. Try:
print str(sum(temp))[:10]
Much simpler:
s = 'copied and pasted from the page'
result = sum(map(int, s.splitlines()))[:10]
Only the 11 first digits need to be summed,
somme11=sum(int(number2[i:i+11]) for i in range(100))
print(somme11)
print( 'the ten first digits are' , somme11//1000)
Because the carry can’t exceed 99.
4893024188690
the ten first digits are 4893024188
An alternative is to load the numbers into a file and then just add them up, unless I too have completely misunderstood the question.
with open("sum.nums","r") as f:
data = f.readlines()
total = 0
for i in data:
total += int(i)
print "1st 10 digits ", str(total)[:10], "of total", total
1st 10 digits 5537376230 of total 5537376230390876637302048746832985971773659831892672
It’s pretty simple
def e13():
f=open("100x50digits.txt")
summ=0
for line in f:
summ+=int(line[:11])
print(int(summ/1000))
f.close()
e13()
number = '''The 100 lines of 50 characters'''
numbers = number.splitlines() #Devide the string into a list of strings.
numbers = [int(i) for i in numbers] #Convert all elements into integers.
totalSum = str(sum(numbers)) #Add everything and convert to a string.
print(totalSum[:10]) #Print the first 10 digits of the result.
The correct answer is 5537376230, checked with P.E.
The real challenge here is to deal with the really long string
Use StringIO
to take number as a input string and iterate trough elements by converting each one to a integer value
from io import StringIO
number = StringIO(""" # Paste one-hundred 50 numbers """)
print(str(sum(map(lambda x: int(x), number)))[:10])
>>> '5537376230'