I am pretty new when it comes to python but I came across something really strange.
I was writing a code to prompt user for a number and print the number from Fibonacci sequence corresponding to it.
Fibonnaci sequence:0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181
If user enters 7 the number 8 should be printed. To do this I ended up with two codes, one from web and other one myself.
'''This one is from web'''
n = int(input("Please enter the number:"))
def fibo(n):
if n == 1:
return 0
if n==2:
return 1
else:
return fibo(n-1)+fibo(n-2)
print(fibo(n))
And this my code:
fibo = [0,1]
num = int(input("enter number:"))
i1 = 0
i2 = 1
for i in range(num):
#print(i1,i2)
i1 = i1+i2
fibo.append(i1)
i2 = i1 + i2
fibo.append(i2)
# print(fibo)
print(fibo[num-1])
If I run my code and enter 100 the result 218922995834555169026 is printed within seconds, literally 2 seconds max!
However if I use the code from web its about 10 minutes when the process started and it is still calculating. My questions are:
Q1)Why is the code from web taking so long?
Q2)Which type of code is used in which situations e.g. speed, debugging, game development, web development, high reliability of results etc. (Please explain) as I am confused like if this method has less speed than what is it good at?
For Tech geeks I am using Hp prodesk 600 g3 sff with intel core i3-7100, 8gb ram and intel hd 630 graphics(Just in case if my hardware is to be blamed for slow processing)
It's about more than 15 minutes now!