65.8k views
5 votes
Write a function powerthree that, given a non-negative number n, returns 3n ( 3^n, or "3 raised topower n") recursively, assuming 3n is something that can be represented as an integer. do not use a loop, and do not use the character '*' anywhere in your code.

User Giesburts
by
5.0k points

1 Answer

4 votes
Because the * is not to be used, you also have to create a recursive multiplication; it could look like this:

int times(int a, int b) {
if (a == 1)
return b; else return b + times(a - 1, b);
}

int powerthree(int n) {
if (n == 0)
return 1;
else return times(3, powerthree(n - 1));
}


User Santhucool
by
5.2k points