Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Fibonacci Spiral's End by Magu
from itertools import cycle
from functools import reduce, lru_cache
def fibo_spiral_end(elem: int) -> list[int]:
l = [Fib(_) for _ in range(1, elem + 1)]
x = reduce(lambda a, b: a + b, [i * j for i, j in zip(cycle([1, 1, -1, -1]), l)], 0)
y = reduce(lambda a, b: a + b, [k * l for k, l in zip(cycle([-1, 1, 1, -1]), l)], 0)
return [x, y]
@lru_cache()
def Fib(n):
if n < 2: return n
return Fib(n - 1) + Fib(n - 2)
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. 19, 2023
Comments: