comp-think / 2018-2019

The GitHub repository containing all the material related to the Computational Thinking and Programming course of the Digital Humanities and Digital Knowledge degree at the University of Bologna (a.a. 2018/2019).
Other
30 stars 8 forks source link

Lecture "Introduction to Computational Thinking", exercise 2 #2

Open essepuntato opened 5 years ago

essepuntato commented 5 years ago

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?

MattiaSpadoni commented 5 years ago

Input= 7 Output= (?)

n=7 a=1 b=1 c=2; n-- n= 6 a=1 b=2 c=3; n-- n= 5 a=2 b=3 c=5; n-- n= 4 a=3 b=5 c=8; n-- n=3 (now I can return "c") a= 5 b=8 C= 13;

Output= 13

ilsamoano commented 5 years ago

The function for calculating the nt​ h 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. Otherwise, return the sum of the same function with “n-1” as input and still the same function with “n-2” as input.

def Fibo(n): 
    if n<0: 
        return 0 
    elif n==1: 
        return 1
    else: 
        return Fibo(n-1)+Fibo(n-2) 

print(Fibo(7)) 

output: 13

(for code explanation: https://www.geeksforgeeks.org/program-for-nth-fibonacci-number/)

simayguzel commented 5 years ago

def F(n): if (n == 0) : return 0 if (n == 1 or n== 2) : return 1 else: return F(n-1)+F(n-2)

print (F(7))

Output: 13

mangiafrangette commented 5 years ago

The function for calculating the nt​ h 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. Otherwise, return the sum of the same function with “n-1” as input and still the same function with “n-2” as input.

def Fibo(n): 
    if n<0: 
        return 0 
    elif n==1: 
        return 1
    else: 
        return Fibo(n-1)+Fibo(n-2) 

print(Fibo(7)) 

output: 13

(for code explanation: https://www.geeksforgeeks.org/program-for-nth-fibonacci-number/)

Add an equal on line 2 ("if n is less than or equal to 0 [...]"). if n <= 0: Also, maybe is not clear to everyone that is written in Python. Good job!

delfimpandiani commented 5 years ago

The function for calculating the n-th 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. Otherwise, return the sum of the same function with “n-1” as input and still the same function with “n-2” as input.

def F(n): if (n <= 0) : return 0 if (n == 1 or n== 2) : return 1 else: return F(n-1)+F(n-2)

F(7) = 13 The logic behind this result is the following:

F(7) = F(6) + F(5) = 8 + 5 = 13 F(6) = F(5) + F(4) = 5 + 3 = 8 F(5) = F(4) + F(3) = 3 + 2 = 5 F(4) = F(3) + F(2) = 2 + 1 = 3 F(3) = F(2) + F(1) = 1 + 1 = 2 F(2) = F(1) + F(0) = 1 + 0 = 1 F(1) = 1 F(0) = 0

tceron commented 5 years ago

a = 1;
b = 1;
while (1) { c = a + b; }
if (n <= 3) return c;
a = b;
b = c; { c = a + b; }
n--; (which means to subtract 1)

Input = 7

n= 7 a= 1 b= 1 c = 2 n-- (subtract one) n=6 (n <= 3 so return c) n= 6 a= 1 b= 2 c = 3 n-- n= 5 n= 5 a= 2 b= 3 c = 5 n-- n= 4 n= 4 a= 3 b= 5 c = 8 n= 3
n= 3 a= 5 b= 8 c = 13

{ c = a + b; } So the output is 13.

lisasiurina commented 5 years ago

Input= 7

If (n <=0) then return 0 If (n <=2) then return 1 If (n <=3) then return c

а=1 b=1 c=a+b

a=b b=c

If n=7 a=1 b=1 c=2; 
then n-- 
If n n= 6 a=1 b=2 c=3;
 then n-- 
If n= 5 a=2 b=3 c=5; 
then n-- If n= 4 a=3 b=5 c=8; 
then n-- 
If n=3 a= 5 b=8 C= 13; then return 13 Output 13

friendlynihilist commented 5 years ago

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. Otherwise, return the sum of the same function with “n-1” as input and still the same function with “n-2” as input.

input=7 a=1 b=1 c=(a+b)

If n=7 a=1 b=1 c=2; n-- (subtract 1) If n=6 a=1 b=2 c=3; n-- If n=5 a=2 b=3 c=5; n-- If n=4 a=3 b=5 c=8; n-- If n=3 a=5 b=8 c=13; then return c output=13

SeverinJB commented 5 years ago

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

Ioanna96 commented 5 years ago

n=7 a=1 b=1 c=2 n-- n=6 a=1 b=2 c=3 n-- n=5 a=2 b=3 c=5 n-- n=4 a=3 b=5 c=8 n-- n=3 a=5 b=8 c=13 if “n” is less than or equal to “3” then return “c” Output=13

dafnikitsiki commented 5 years ago

Input= 7 Output= (?)

n=7 a=1 b=1 c=2; n-- n= 6 a=1 b=2 c=3; n-- n= 5 a=2 b=3 c=5; n-- n= 4 a=3 b=5 c=8; n-- n=3 (now I can return "c") a= 5 b=8 C= 13;

Output= 13

So, F(3) in Fibonacci's sequence is the key to find the output for any input number!