Returns the sum of the numbers on the diagonals in a n by n spiral formed in the same way. >>> solution(1001) 669171001 >>> solution(500) 82959497 >>> solution(100) 651897 >>> solution(50) 79697 >>> solution(10) 537
(n: int = 1001)
| 21 | |
| 22 | |
| 23 | def solution(n: int = 1001) -> int: |
| 24 | """Returns the sum of the numbers on the diagonals in a n by n spiral |
| 25 | formed in the same way. |
| 26 | |
| 27 | >>> solution(1001) |
| 28 | 669171001 |
| 29 | >>> solution(500) |
| 30 | 82959497 |
| 31 | >>> solution(100) |
| 32 | 651897 |
| 33 | >>> solution(50) |
| 34 | 79697 |
| 35 | >>> solution(10) |
| 36 | 537 |
| 37 | """ |
| 38 | total = 1 |
| 39 | |
| 40 | for i in range(1, ceil(n / 2.0)): |
| 41 | odd = 2 * i + 1 |
| 42 | even = 2 * i |
| 43 | total = total + 4 * odd**2 - 6 * even |
| 44 | |
| 45 | return total |
| 46 | |
| 47 | |
| 48 | if __name__ == "__main__": |