Lines 68
Euler28 (8)
##### Permissions
Owner: brianoneill
Group Owner: SnortSnort
Viewable by Everyone
Editable by brianoneill
Writing an article is easy - try our reStructured Text demo

# Project Euler 28: Wacky Python Solution 0

 Language Python
# 's
` 1######################################################################## 2# 3#     Project Euler #28 4# 5######################################################################## 6 7import numpy as np 8 9size = 100110i=011j=112pos=113neg=-114########################################################################15#16#     Spiral matrix is series of concentric boxes each formed by17#     counting through a series of four steps, each one more18#     diabolical than the last:19#     down the right side, left across the bottom,20#     up the left side, right across the top21#     22#     step() does each of those steps23########################################################################2425def step(iorj,sign):26      global coord,spiral,num,length,i,j27      k = 128      num+=129      spiral[coord[i],coord[j]] = num30      while k < length-1:31            coord[iorj] += sign32            num += 133            spiral[coord[i],coord[j]] = num34            k += 135      return363738spiral = np.empty([size,size])3940spiral[size/2,size/2] = 1     ### center square is 141coord = [size/2,size/2+1]     ### indices of next element to be updated42length = 343num = 144########################################################################45#46#     Form spiral matrix from inside out.47########################################################################4849while length <= size:5051      step(i,pos)             ### step down52      coord[j] -= 1           ### left one53      step(j,neg)             ### step left54      coord[i] -= 1           ### up one55      step(i,neg)             ### step up56      coord[j] += 1           ### right one57      step(j,pos)             ### step right58      coord[j] += 1           ### right one5960      length += 2616263print spiral64########################################################################65#66#     Summ up diagonal elements, account for double counting center square67########################################################################68summ = -169i = 070while i < size:71      summ += spiral[i,i]72      i += 173i = size - 174j = 075while j < size:76      summ += spiral[i,j]77      i -= 178      j += 179print summ`