codekofi
← All problems

Problem 88

Easy

1 · Worked examples

0 / 3

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 3 outputs correctly.

The accepted solution

1int squares(int n) {
2
3 int sum = 0;
4
5 while (n > 0) {
6
7 int d = n % 10;
8
9 sum += d * d;
10 n = n / 10;
11 }
12
13 return sum;
14}
15
16bool solution(int n) {
17
18 int slow = n;
19 int fast = n;
20
21 do {
22 slow = squares(slow);
23 fast = squares(squares(fast));
24 } while (slow != fast);
25
26 return slow == 1;
27}

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.