c++ - Calculate the function F(n) with recursion -


read topic not know saying: function f (n) determined on non-negative integers follows: f (0) = 1; f (1) = 1; f (2n) = f (n); f (2n + 1) = f (n) + f (n + 1) calculated f (n) recursion. , code:

#include<iostream.h> double tinh_f(int n) {     if(n == 0)    {       return 0;    }     if(n == 1)     {       return 1;     }       return (f(n+1) - f(2*n+1)); } 

this incorrect. recursive function calls itself , includes stopping condition:

#include<iostream.h> double tinh_f(int n) {     if(n == 0)    {       return 0;    }     if(n == 1)     {       return 1;     }       // note function name change      return (tinh_f(n+1) - tinh_f(2*n+1)); } 

what should function if integer passed in negative? recursion still work? or should throw exception indicate callers contract broken?


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -