comp-think / 2019-2020

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. 2019/2020).
Other
12 stars 3 forks source link

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

Open essepuntato opened 4 years ago

essepuntato commented 4 years ago

What is the result of applying the second natural language definition of the Fibonacci function in Section "Natural languages vs programming languages" using “7” as input?

NoonShin commented 4 years ago

It will be 13, resulting from recursively adding the two previous numbers in the sequence. (For natural numbers 1 to 7, the result will respectively be: 1, 1, 2, 3, 5, 8, 13)

arcangelo7 commented 4 years ago

I agree with Nooshin's answer, but I didn't understand why we need to decrease n in each cycle rather than increase it

NoonShin commented 4 years ago

@arcangelo7 I believe that in this solution, n is acting as a sort of counter. We know that we want the 7th number in the sequence, and we know that the first two numbers in the sequence are 1 and 1. So we start counting down from 7, adding the two previous numbers of the sequence in each iteration.

The process is as follows: When n = 7, we are calculating 1 + 1, which is the 3rd number in the sequence. We decrement n (n = 6), and we calculate the 4th number (1 + 2). If we continue like this, when n reaches 3, we have the 7th number in the sequence, so we return the amount we have calculated in the final step (5 + 8).

aschimmenti commented 4 years ago

If I need to find the value of the function when the input is n=7, I need to apply the function to the results of the function F=F(7-1)+F(7-2). Going backward, since we know F=F1 and F=F0 are equal to 1 and 0 respectively, we can find F2=1 and all the following values, until F7 which is equal to F7=F6+F5 which again is equal to F7=8+5.

arimuti commented 4 years ago

I tried to solve this exercise using Python:

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

fib(7)

13

arcangelo7 commented 4 years ago

@arcangelo7 I believe that in this solution, n is acting as a sort of counter. We know that we want the 7th number in the sequence, and we know that the first two numbers in the sequence are 1 and 1. So we start counting down from 7, adding the two previous numbers of the sequence in each iteration.

The process is as follows: When n = 7, we are calculating 1 + 1, which is the 3rd number in the sequence. We decrement n (n = 6), and we calculate the 4th number (1 + 2). If we continue like this, when n reaches 3, we have the 7th number in the sequence, so we return the amount we have calculated in the final step (5 + 8).

@NoonShin, I've been thinking about it for a long time, but I finally understood. This is really counter-intuitive, but brilliant and beautiful. Thanks!

elisasilvad commented 4 years ago

I rewrite in mathematical language the definition given in natural language:

The function for calculating the nth Fibonacci number takes as input an integer “n”. F=F(n) If “n” is less than or equal to 0, then 0 is returned as a result. F=F(n<=0) = 0 Otherwise, if “n” is equal to 1, then 1 is returned. F=F(n=1) = 1 Otherwise, return the sum of the same function with “n-1” as input and still the same function with “n-2” as input. F=F(n-1)+F(n-2)

If the input is 7, F=F(7) and n=7 F=F(7-1)+F(7-2)=F(6)+F(5) I need to find F6 and F5 F=F(6)=F(6-1)+F(6-2) = F=F(5)+F(4) F=F(5)=F(5-1)+F(5-2) = F=F(4)+F(3) I need to find F(4) and F(3) F=F(4)=F(4-1)+F(4-2)=F(3)+F(2) F=F(3)=F(3-1)+F(3-2)=F(2)+F(1) I know that F(1)=1 I need to find F(2) F=F(2)=F(2-1)+F(2-2)=F(1)+F(0)

I know that F(1)=1 and F(0)=0 So, I can rewrite it: F=F(2)=F(1)+F(0)=1+0=1 F(2)=1 F=F(3)=F(2)+F(1)=1+1=2 F(3)=2 F=F(4)=F(3)+F(2)=2+1=3 F(4)=3 F=F(5)=F(4)+F(3)=3+2=5 F(5)=5 F=F(6)=F(5)+F(4)=5+3=8 And then, finally, F(7) F=F(7)=F(6)+F(5)=8+5=13

FrancescoFernicola commented 4 years ago

I went through the exact same process as @elisasilvad

ariannamorettj commented 4 years ago

I agree with the previous answers. Anyway, since my reminiscences of maths are clearly not sufficient to complete the exercise with either an appropriate language or method, I just tried to put down schematically my mental process:

Input: 7

essepuntato commented 4 years ago

Hi @ariannamorettj

The process you are following works, but it seems to be related more to the first definition in natural language of the Fibonacci algorithm instead of the second definition. I see similar issues also in @NoonShin approach, as also reprised by @arcangelo7.

However, the fact you have focussed on the first definition in natural language instead of the second one could be due to my mistake in the text of the exercise I've provided, and that have corrected a few days ago mentioning explicitly the second natural language definition. Apologies for any misunderstanding.