I am a newbie here. If I do something wrongly, please show me tolerance.
I tried to solve this problem: Problem_Picture_1 Problem_Picture_2
My first solution didn't work:
# Uses python3
import sys
# Uses python3
def get_fibonacci_last_digit(n):
fib = [0, 1]
if n <= 1:
return n
for i in range(2, n):
current = (fib[i-1] + fib[i-2]) % 10
fib.append(current)
return fib[-1]
n = int(input())
if __name__ == '__main__':
input = sys.stdin.read()
n = int(input)
print(get_fibonacci_last_digit(n))
But my second solution worked:
# Uses python3
def calc_fib(n):
fib = [0, 1]
if n <= 1:
return n
for i in range(2, n+1):
current = (fib[i-1] + fib[i-2]) % 10
fib.append(current)
return fib[-1]
n = int(input())
print(calc_fib(n))
What is the difference between these two approaches? Why didn't work my first solution? Please explain to me.
Edit: When I worked my first approach, It's still running. It couldn't return a number