codekofi
← All problems

Problem 16

Medium

1 · Worked examples

0 / 4

Type an input and write what you think is the output . Each problem is converted to Web Assembly, so any possible input will show the corresponding output. 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
3 if (e == 0) {
4 return 1.0;
5 }
6
7 double half = helper(x, e / 2);
8
9 if (e % 2 == 0) {
10 return half * half;
11 }
12
13 return half * half * x;
14}
15
16double solution(double x, int n) {
17
18 long long e = n;
19
20 if (e < 0) {
21 return 1.0 / helper(x, -e);
22 }
23
24 return helper(x, e);
25}

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.