If I have this recursive function:
int mystery(int n) {
if ( n == 0 || n == 1 || n == 2) return n ;
return (mystery(n-1) + mystery(n-2) + mystery(n-3)) ;
}
I am working with finding mystery(20).
How can I find out how many addition operations are carried out when calculating the function and how many invocations of mystery() there are in order to calculate mystery(20)?
I tried adding some cout statements like:
int mystery(int n) {
if ( n == 0 || n == 1 || n == 2) {
cout << n << endl;
return n ;
}
cout << n << endl;
return (mystery(n-1) + mystery(n-2) + mystery(n-3)) ;
}
But I couldn’t really make sense of it since there were over a thousand numbers outputted. And I don’t believe those cout statements do much in the way of telling me how many addition operations are carried out and how many invocations of mystery() there are in order to calculate mystery(20)?
Thanks for any and all help!
The easiest way to do is to increment a global (or static global) variable.
Something like to get the number of mystery call:
And this to get the number of additions: