+ 1

Why inner loop is running only once.

I wrote the code for fibonacci numbers. I create two loops. Outer will print number and inner will return the sum. I am confused that why the inner loop that is in function is running only 1 time. As the value of sum should update 2 times if the loop will run 2 times. But it isn't. Can anyone tell me the logic behind it?? https://code.sololearn.com/cyW5UZJrQuB6/?ref=app

20th Feb 2020, 8:41 AM
Khansa Ashraf
Khansa Ashraf - avatar
1 ответ
+ 4
If you put print (elm1,elm2) you can see that the loop in the function is not running once but everytime n changes. For n upto 2, it returns 1 but after that the loop runs for every value. for (3,3) sum = 1+1 3 --> 2 for (3,4) sum = 1+1 elem1, elem2 = elem2, sum sum = 2+1 4--> 3 ... for (3,5) ... for (3,6) ... ... for(3,9) This continues. So the loop is not executed only once but everytime n changes.
20th Feb 2020, 8:53 AM
Utkarsh Sharma
Utkarsh Sharma - avatar