Problem 104

completed February 7, 2012

Comments

I had to try this problem like 10 times. The biggest problem was the fact that I was working with really really large numbers, but only needing the first and last 9 digits of it.

The way I solved that was to keep track of the length of the number, then only keep the first 15 digits and the last 10. It was some work to deal with lining up the first 15 digits correctly when adding them.

Code

def panlast(n):
	last = str(n)[-9:]
	for d in range(1,10):
		if str(d) not in last:
			return False
	return True

def panfirst(n):
	first = str(n)[:9]
	for d in range(1,10):
		if str(d) not in first:
			return False
	return True

F = [0,1,1]
for i in range(100):
	F.append(F[-1]+F[-2])

i = 80
F = [[int(str(F[79])[:15]),len(str(F[79])),F[79]],[int(str(F[80])[:15]),len(str(F[80])),F[80]]]
while True:
	i += 1
	nextF = [F[0][0]+F[1][0]*10**(F[1][1]-F[0][1]), 0, int(str(F[0][2])[-10:]) + int(str(F[1][2])[-10:])]
	nextF[1] = F[1][1] + (len(str(nextF[0])) - len(str(F[1][0])) - (F[1][1]-F[0][1]))
	nextF[0] = int(str(nextF[0])[:15])
	F.append(nextF)
	del F[0]
	if panfirst(F[1][0]) == True and panlast(F[1][2]) == True:
		print i
		break