आसान 0 हल किए गए 0% स्वीकृति

Neon Number

Check whether the digit sum of a number's square equals the number.

आप क्या सीखेंगे

  • Square a number without overflow
  • Sum the digits of the square
  • Compare the result with the input

समस्या

अनुवाद दिखाएँ

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.

सीमाएँ

0 ≤ n ≤ 10^9

उदाहरण

उदाहरण 1
इनपुट:
n = 9

अपेक्षित आउटपुट:
expected = True
इनपुट:
long long n = 9;

अपेक्षित आउटपुट:
bool expected = true;
इनपुट:
long n = 9;

अपेक्षित आउटपुट:
bool expected = true;

व्याख्या

9² = 81 and 8 + 1 = 9, so the answer is true.

उदाहरण 2
इनपुट:
n = 12

अपेक्षित आउटपुट:
expected = False
इनपुट:
long long n = 12;

अपेक्षित आउटपुट:
bool expected = false;
इनपुट:
long n = 12;

अपेक्षित आउटपुट:
bool expected = false;

व्याख्या

12² = 144 and 1 + 4 + 4 = 9, which is not 12, so the answer is false.

कैसे हल करें

पहले खुद कोशिश करें, फिर एक-एक करके संकेत खोलें।

संकेत 1

Compute n × n in a 64-bit integer.

संकेत 2

Add up the digits of the square with % 10 and / 10.

संकेत 3

The digit sum of a square below 10^18 is at most 162, so large n can never be neon.

अपेक्षित जटिलता

O(d) time for d digits, O(1) extra space

संदर्भ

यह चुनौती CodeUtility के लिए अनुकूलित की गई है। ऊपर दिए गए स्टेटमेंट और सीमाओं का पालन करें।

हर कोडर का सबसे अच्छा पास हुआ समाधान। तेज़, कम मेमोरी वाले और बेहतर रेटिंग वाले समाधान ऊपर रहते हैं।

अभी कोई पास हुआ समाधान नहीं है। इसे हल करें और पहला स्थान पाएँ।

दूसरों से सीखें

समुदाय समाधान

अभी तक कोई प्रकाशित समाधान नहीं। पहला साझा करने के लिए इस चुनौती को हल करें।