diff options
Diffstat (limited to 'AlgoDesignAndTechniqueEdxPython/sources/fiblastdigit.py')
-rw-r--r-- | AlgoDesignAndTechniqueEdxPython/sources/fiblastdigit.py | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/AlgoDesignAndTechniqueEdxPython/sources/fiblastdigit.py b/AlgoDesignAndTechniqueEdxPython/sources/fiblastdigit.py new file mode 100644 index 0000000..cca9cd9 --- /dev/null +++ b/AlgoDesignAndTechniqueEdxPython/sources/fiblastdigit.py @@ -0,0 +1,29 @@ +# Uses python3 +import sys +def getFibLastDigitNaive(n): + if n <= 1: + return n + + previous = 0 + current = 1 + + for _ in range(n - 1): + previous, current = current, previous + current + + return current % 10 + + +def getFibLastDigitOptimized(n): + if (n <= 1): + return n + fibLst = [None] * (n+1) + fibLst[0] = 0 + fibLst[1] = 1 + for i in range(2, n+1): + fibLst[i] = (fibLst[i-1] + fibLst[i-2]) % 10 + return fibLst[n] + +if __name__ == '__main__': + input = sys.stdin.read() + n = int(input) + print(getFibLastDigitOptimized(n)) |