-
Notifications
You must be signed in to change notification settings - Fork 8
Lecture "Introduction to Computational Thinking", exercise 2 #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Input= 7 n=7 a=1 b=1 c=2; Output= 13 |
The function for calculating the nt h Fibonacci number takes as input an integer “n”.
output: 13 (for code explanation: https://www.geeksforgeeks.org/program-for-nth-fibonacci-number/) |
def F(n):
Output: 13 |
Add an equal on line 2 ("if n is less than or equal to 0 [...]"). |
The function for calculating the n-th Fibonacci number takes as input an integer “n”. def F(n): F(7) = 13 F(7) = F(6) + F(5) = 8 + 5 = 13 |
a = 1; Input = 7 n= 7 a= 1 b= 1 c = 2 { c = a + b; } So the output is 13. |
Input= 7 If (n <=0) а=1 a=b If n=7 a=1 b=1 c=2; |
The function for calculating the nth Fibonacci number takes as input an integer “n”. If “n” is less than or equal to 0, then 0 is returned as result. Otherwise, if “n” is equal to 1, then 1 is returned. input=7 If n=7 a=1 b=1 c=2; |
A return statement in a while loop is equal to a break statement. Hence, as soon as n <= 3 the return function is trigged and the while loop stops. Based on this, the while loop stops as soon as n was reduced to 3 by n--. The loop runs a total of 5 times. During the last run, it returned c = 13. n = 7 —> a = 1, b = 1 —> c = a + b = 2 —> a = b = 1, b = c = 2 —> n = 7 - 1 = 6 —> c = a + b = 3 —> a = b = 2, b = c = 3 —> n = 6 - 1 = 5 —> c = a + b = 5 —> a = b = 3, b = c = 5 —> n = 6 - 1 = 4 —> c = a + b = 8 —> a = b = 5, b = c = 8 —> n = 6 - 1 = 3 —> c = a + b = 13 —> c = 13 |
n=7 a=1 b=1 c=2 |
So, F(3) in Fibonacci's sequence is the key to find the output for any input number! |
What is the result of applying the latest natural language definition of the Fibonacci function in Section "Natural languages vs. programming languages" using “7” as input?
The text was updated successfully, but these errors were encountered: