# Recognize Recursive Function
Each of following functions solves some well-known problem, but name of the function is changed. Figure out what problems functions solve.
# Exercise 1
int foo(int x) {
if (x == 0) return 0;
if (x < 0) return foo(x+1) +1;
if (x > 0) return foo(x-1) +1;
}
reveal
int absoluteValue(int x) {
if (x == 0) return 0;
if (x < 0) return absoluteValue(x+1) +1;
if (x > 0) return absoluteValue(x-1) +1;
}
WARNING
This function is just educative. Not for serious use at all, it is the worst implementation ever.
# Exercise 2
long foo(int x) {
if (x == 0) return 1;
return x * foo(x-1);
}
reveal
long factorial(int x) {
if (x == 0) return 1;
return x * factorial(x-1);
}
TIP
This function is legit for serious use.
# Exercise 3
int foo(int num) {
if (num <= 0) return 0;
if (num == 1) return 1;
return foo(num-1) + foo(num-2);
}
reveal
int fibonacci(int num) {
if (num <= 0) return 0;
if (num == 1) return 1;
return fibonacci(num-1) + fibonacci(num-2);
}
WARNING
This function is just educative. Not for serious use at all, it is the worst implementation ever.
# Exercise 4
int foo(int num);
int bar(int num) {
if (num < 0) return bar(-num);
if (num == 0) return 0;
if (num == 1) return 1;
return foo(num-1);
}
int foo(int num) {
if (num < 0) return foo(-num);
if (num == 0) return 1;
if (num == 1) return 0;
return bar(num-1);
}
reveal
int isEven(int num);
int isOdd(int num) {
if (num < 0) return isOdd(-num);
if (num == 0) return 0;
if (num == 1) return 1;
return isEven(num-1);
}
int isEven(int num) {
if (num < 0) return isEven(-num);
if (num == 0) return 1;
if (num == 1) return 0;
return isOdd(num-1);
}
WARNING
These functions are just educative. Not for serious use at all, it is the worst implementation ever.