Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Starting with the number 1 and moving to the right in a clockwise direction a 5 by 5 spiral is formed as follows:
- #21 22 23 24 25
- #20 7 8 9 10
- #19 6 1 2 11
- #18 5 4 3 12
- #17 16 15 14 13
- #It can be verified that the sum of the numbers on the diagonals is 101.
- #What is the sum of the numbers on the diagonals in a 1001 by 1001 spiral formed in the same way?
- def moveR(row,col,times):
- for i in range(1,times+1):
- arr[row][col+1] = arr[row][col] + 1
- col += 1
- def moveD(row,col,times):
- for i in range(1,times+1):
- arr[row+1][col] = arr[row][col] + 1
- row += 1
- def moveL(row,col,times):
- for i in range(1,times+1):
- arr[row][col-1] = arr[row][col] + 1
- col -= 1
- def moveU(row,col,times):
- for i in range(1,times+1):
- arr[row-1][col] = arr[row][col] + 1
- row -= 1
- dim = 1001
- arr = []
- #Create an array of None elements
- for i in range(dim):
- row = []
- for j in range(dim):
- row.append(None)
- arr.append(row)
- row = dim/2
- col = dim/2
- startValue = 1
- arr[row][col] = startValue #Starting Position
- for j in range(1,dim+1):
- if row == 0 and col == 0:
- moveR(row,col,dim-1)
- col += j
- elif j%2 != 0:
- moveR(row,col,j)
- col += j
- moveD(row,col,j)
- row += j
- elif j%2 == 0:
- moveL(row,col,j)
- col -= j
- moveU(row,col,j)
- row -= j
- count = 0
- for i in range(dim):
- for j in range(dim):
- if i == j or i+j == dim-1:
- count += arr[i][j]
- print count
Advertisement
Add Comment
Please, Sign In to add comment