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

Automorphic Number

Check whether the square of a number ends with the number itself.

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

  • Count the digits of a number
  • Take the last d digits with a modulo
  • Handle 0 as a one-digit number

समस्या

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

A number is automorphic if its square ends in the same digits as the number. For example, 76² = 5776, which ends in 76, so 76 is automorphic.

Implement automorphic_number(n).

n² can be as large as 10^18, which still fits in a signed 64-bit integer. You can compare digits from the right, or compare n² modulo 10^d with n, where d is the number of digits of n.

Input

n: The number to test.

Output

Return true if the last digits of n² are exactly the digits of n, otherwise false. 0 and 1 are automorphic (0² = 0, 1² = 1).

सीमाएँ

0 ≤ n ≤ 10^9

उदाहरण

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

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

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

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

व्याख्या

25² = 625, which ends in 25, so the answer is true.

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

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

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

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

व्याख्या

7² = 49, which ends in 9, not 7, so the answer is false.

कैसे हल करें

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

संकेत 1

Find how many digits n has.

संकेत 2

The last d digits of a number are its remainder modulo 10^d.

संकेत 3

Make sure n = 0 counts as one digit, so 10^d is 10, not 1.

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

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

संदर्भ

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

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

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

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

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

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