Skip to main content

Euler 0029

The Problem:

IfHow youmany weredistinct toterms build a square starting with 1are in the middlesequence thengenerated goingby right,a^b down,for left 2, up 2, right 2,a and repeat,b allbeing thebounded cornerto numbers2 wouldand be100 diagonals.

On the Project Euler website there is a nice graphic that I'm not putting here.

What is the sum of the numbers on the diagonals in a 1001 by 1001 spiral formed in the same way?inclusive?

Considerations and Approach:

ThisNaively, problemthis is bait.only processing 100*100 numbers, not really much at all.  
We don'tcan have to generatecreate a 2dpython structure.set Insteadand wethen caninsert observeevery calculation, so it will remove the patternredundancy.  of skipping.  
1.3.5.7.9...13...17...21...25.....30

We start with one and add 1. Every four times we add, we add 2 to the skipping distance, and we declare our square 2 larger!

The Code:

max_square = 1001

spiral = 1
square_sizelower_limit = 2
totalupper_limit = 1100

while#create square_sizea +set
1distinct <= max_square:set()
#go through the inclusive ranges
for i in range(4)lower_limit, upper_limit + 1):
    spiralfor j in range(lower_limit, upper_limit += square_size1):
        total#do +=the spiralset square_sizeaddition +=operator 2for a^b
        distinct.add(i**j)

#print how many distinct pairs that we created
print(total)len(distinct))