MCPcopy Index your code
hub / github.com/TheAlgorithms/Python / solution

Function solution

project_euler/problem_028/sol1.py:23–45  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

21
22
23def 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
48if __name__ == "__main__":

Callers 1

sol1.pyFile · 0.70

Calls 1

ceilFunction · 0.90

Tested by

no test coverage detected