codekofi
← All problems

Problem 16

Medium40 XP

1 · Worked examples

0 / 4

Give it an input and say what you think comes back. It compiles and runs for real, so a wrong guess still shows you the answer — probe as much as you like. Only correct predictions count.

solution()
returns

2 · Which problem is it?

Locked until you have predicted 4 outputs correctly.

The accepted solution

1double helper(double x, long long e) {
2 if (e == 0) return 1.0;
3 double half = helper(x, e / 2);
4 if (e % 2 == 0) return half * half;
5 return half * half * x;
6}
7
8double solution(double x, int n) {
9 long long e = n;
10 if (e < 0) return 1.0 / helper(x, -e);
11 return helper(x, e);
12}

Names have been stripped. The signature is the only clue you get for free. Compiled as C++20 with the standard headers and using namespace std; already in scope.