Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <algorithm>
- using namespace std;
- int f(int x) {
- if(x == 5) {
- return 0;
- }
- return x + f(x + 1);
- }
- int main() {
- cout << f(1) << endl;
- return 0;
- }
- // f(1) = 1 + f(2) = 9 + 1 = 10
- // f(2) = 2 + f(3) = 7 + 2 = 9
- // f(3) = 3 + f(4) = 3 + 4 = 7
- // f(4) = 4 + f(5) = 4 + 0 = 4
- // f(5) = 0
Advertisement
Add Comment
Please, Sign In to add comment