Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
generator solution in Clear category for Fibonacci Spiral's End by Sim0000
def fibo_spiral_end(elem: int) -> list[int]:
def g():
a, b = 0, 1
while True:
a, b = b, a + b
yield a, -a
a, b = b, a + b
yield a, a
a, b = b, a + b
yield -a, a
a, b = b, a + b
yield -a, -a
x = y = 0
for i, (dx, dy) in enumerate(g()):
if i >= elem: break
x, y = x + dx, y + dy
return [x, y]
"""
n a coord
0 0 [0, 0]
1 1 [1, -1] x0 + a, y0 - a +a, -a
2 1 [2, 0] x1 + a, y1 + a +a, +a
3 2 [0, 2] x2 - a, y2 + a -a, +a
4 3 [-3, -1] x3 - a, y3 - a -a, -a
5 5 [2, -6] x4 + a, y4 - a +a, -a
6 8 [10, 2] x5 + a, y5 + a +a, +a
7 13 [-3, 15] x6 - a, y6 + a -a, +a
8 21 [-24, -6] x7 - a, y7 - a -a, -a
...
"""
print("Example:")
print(fibo_spiral_end(5))
# These "asserts" are used for self-checking
assert fibo_spiral_end(0) == [0, 0]
assert fibo_spiral_end(1) == [1, -1]
assert fibo_spiral_end(2) == [2, 0]
assert fibo_spiral_end(3) == [0, 2]
assert fibo_spiral_end(4) == [-3, -1]
print("The mission is done! Click 'Check Solution' to earn rewards!")
Jan. 10, 2023
Comments: