Recursion, Recursion

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
int y(int x) {

  if (x==0 || x==1) {

    return 1;

  } else {

    return y(x-1) + y(x-2);
  }

}

If this program returned 8, what does x equal to?


The answer is 5.

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

1 solution

Pranjal Jain
Apr 17, 2015

The program will return 1 1 if input is 0 0 or 1 1 , otherwise, it will return the sum of previous two values.

So, thats a fibonacci sequence with F 0 = 1 F_0=1 and F 1 = 1 F_1=1 . This gives the sequence as 1 , 1 , 2 , 3 , 5 , 8 1,1,2,3,5,8 , note that 8 8 is F 5 F_5 and not F 6 F_6 as it is starting from F 0 F_0

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...