From 9d17066cda66731e27f7662498de908fd9fa2754 Mon Sep 17 00:00:00 2001 From: totaljfb Date: Mon, 1 Jan 2018 18:50:17 -0600 Subject: [PATCH] Q4 added, matrix multiplication used based on Zhijun's method --- totaljfb/Q4.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 totaljfb/Q4.py diff --git a/totaljfb/Q4.py b/totaljfb/Q4.py new file mode 100644 index 0000000..e5e960b --- /dev/null +++ b/totaljfb/Q4.py @@ -0,0 +1,37 @@ +#------------------------------------------------------------------------------- +# Name: module1 +# Purpose: +# +# Author: Jason Zhang +# +# Created: 01/01/2018 +# Copyright: (c) Jason Zhang 2018 +# Licence: +#------------------------------------------------------------------------------- + +def main(): + pass + +if __name__ == '__main__': + main() +#Q4 +#using matrix multiplication to resolve Fibonacci number calculation + +import numpy as np + +def fibonacci_number(n): + fib_matrix = np.matrix([[1,1],[1,0]])**(n-1)*np.matrix([[1],[0]]) + return fib_matrix[0,0] + +#program starts here +invalid_input = True +while invalid_input: + try: + n = int(input("Please enter an integer: ")) + print("The xth fibonacci number is :",fibonacci_number(n)) + invalid_input = False + except ValueError: + print("Please enter an integer") + + +