-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem28.py
More file actions
52 lines (38 loc) · 1.2 KB
/
Problem28.py
File metadata and controls
52 lines (38 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# Code by @AmirMotefaker
# projecteuler.net
# https://projecteuler.net/problem=28
# Number spiral diagonals
# Problem 28
# 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?
import time
start_time = time.time() #Time at the start of program execution
def main():
counter = 0
gap_size = 1
four_counter = 4
total = 0
for n in range(1, 1001**2 + 1):
if counter == 0:
total += n
counter = gap_size
four_counter -= 1
#print('here', n)
elif counter != 0:
#print('here2', n)
counter -= 1
if four_counter == 0:
gap_size += 2
four_counter = 4
print(total)
# count 1, miss one (gap size), count, miss,... four counter, increase gap size
main()
end_time = time.time() #Time at the end of execution
print ("Time of program execution:", (end_time - start_time)) # Time of program execution
### Answer: 669171001