CPP
cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/*
*For a positive integer n, let f(n) be the sum of the
*squares of the digits (in base 10) of n, e.g.
* f(3) = 3^2 = 9
*Condition:
*f(n) = Perfect Square
*
*Watch the runtime!
*/
#include <chrono>
#include <iostream>
using ull = unsigned long long;
using namespace std::chrono;
constexpr ull power(ull num, ull i = 2) {
return (i < 1 ? 1 : num * power(num, i - 1));
}
constexpr ull sumSquares(ull num) {
return (num ? power(num % 10) + sumSquares(num / 10) : 0);
}
constexpr bool isSquare(long long num, ull i = 1) {
return (num > 0 ? isSquare(num - i, i + 2) : !num);
}
Enter to Rename, Shift+Enter to Preview
OUTPUT
Run