I wrote
def recursive_function_cache(func):
cache = dict()
def wrapper(*args, **kwargs):
parameters = (tuple.__repr__(args), dict.__repr__(kwargs))
if parameters not in cache:
cache.update({parameters : func(*args, **kwargs)})
return cache.__getitem__(parameters) -sharp
return wrapper
def Fibonacci_sequence_06 (n: int) -> int: -sharpnnFibonacci
assert isinstance(n, int), "n is an error of non-integer type."
@recursive_function_cache
-sharp@profile
def Calculate_Fibonacci_sequence (n: int) -> int:
""
if n>=3:
if (n & 1) == 0: -sharpn
one_half_fib_n = Calculate_Fibonacci_sequence(n >> 1)
one_half_fib_n_minus_one = Calculate_Fibonacci_sequence((n >> 1) - 1)
return ((one_half_fib_n_minus_one << 1) + one_half_fib_n) * one_half_fib_n
else: -sharpn
return Calculate_Fibonacci_sequence(n >> 1) ** 2 + Calculate_Fibonacci_sequence((n >> 1) + 1) ** 2
elif n in (1, 2):
return 1
elif n==0:
return 0
if n>=0:
return Calculate_Fibonacci_sequence(n)
else:
return None
Fibonacci_sequence_06(10000000)
it takes 3 seconds to calculate the 10 millionth item, which is 100 times longer than the matrix method.
I think the time complexity is log (n). Check that
has been called recursively for 59 times, but there is so much time difference between it and the measured time of matrix method.
where is the high complexity hidden