Đề bài
A neon number is a number whose square has a digit sum equal to the number itself. For example, 9² = 81 and 8 + 1 = 9, so 9 is a neon number.
Implement neon_number(n).
n² can reach 10^18, which fits in a signed 64-bit integer.
Input
n: The number to test.
Output
Return true if the sum of the decimal digits of n² equals n, otherwise false.
Ràng buộc
0 ≤ n ≤ 10^9
Ví dụ
Đầu vào:
n = 9
Kết quả mong đợi:
expected = True
Đầu vào:
long long n = 9;
Kết quả mong đợi:
bool expected = true;
Đầu vào:
long n = 9;
Kết quả mong đợi:
bool expected = true;
Giải thích
9² = 81 and 8 + 1 = 9, so the answer is true.
Đầu vào:
n = 12
Kết quả mong đợi:
expected = False
Đầu vào:
long long n = 12;
Kết quả mong đợi:
bool expected = false;
Đầu vào:
long n = 12;
Kết quả mong đợi:
bool expected = false;
Giải thích
12² = 144 and 1 + 4 + 4 = 9, which is not 12, so the answer is false.
Hướng tiếp cận
Hãy tự thử trước, sau đó mở từng gợi ý một.
Gợi ý 1
Compute n × n in a 64-bit integer.
Gợi ý 2
Add up the digits of the square with % 10 and / 10.
Gợi ý 3
The digit sum of a square below 10^18 is at most 162, so large n can never be neon.
Độ phức tạp mong đợi
O(d) time for d digits, O(1) extra space
Tham khảo
Thử thách này đã được điều chỉnh cho CodeUtility. Hãy làm theo đề bài và các ràng buộc ở trên.