Euler 0028
The Problem:
If you were to build a square starting with 1 in the middle then going right, down, left 2, up 2, right 2, and repeat, all the corner numbers would be diagonals.
On the Project Euler website there is a nice graphic that I'm not putting here.
What is the firstsum Fibonacciof numberthe withnumbers 1000on digits?the diagonals in a 1001 by 1001 spiral formed in the same way?
Considerations and Approach:
This problem is somewhatbait. trivialWe don't have to generate a 2d structure. Instead we can observe the pattern of skipping.
1.3.5.7.9...13...17...21...25.....30
We start with Pythonone sinceand itadd can1. storeEvery afour 1000times digitwe numberadd, withwe relativeadd ease,2 to the skipping distance, and we don'tdeclare needour tosquare run2 a super efficient Fibonacci algorithm or store many numbers at all.larger!
The Code:
fib_1max_square = 1001
spiral = 1
fib_2square_size = 2
digits_to_findtotal = 1000
#Start at index of 4 because we already have 1,1,2
running_count = 4
#keep going until the next fib digit length is greater than or equal to the digit to find.1
while len(str(fib_1square_size + fib_2))1 < digits_to_find:
fib = fib_1max_square:
+for fib_2i fib_1in =range(4):
fib_2
fib_2 = fib
running_countspiral += 1square_size
total += spiral
square_size += 2
print(running_count)total)