Automorphic Number

Asked atVisa
1Give yourself 5 minutes
2Answer out loud, not in your head
3Then compare with the answer below

The problem

Given a positive integer N, determine whether it is an Automorphic Number.

A number is called an Automorphic Number if the square of the number ends with the number itself.

Input: N = 76 Output: true Explanation: 76 * 76 = 5776 → ends in 76

Input: N = 25 Output: true Explanation: 25 * 25 = 625 → ends in 25

Input: N = 7

  • 1 ≤ N ≤ 10⁶

cpp

class Solution {
public:
    bool isAutomorphic(int n) {
        // Your code goes here
    }
};

java

class Solution {
    public boolean isAutomorphic(int n) {
        // Your code goes here
    }
}

python

class Solution:
    def isAutomorphic(self, n: int) -> bool:
        # Your code goes here

javascript

class Solution {
    isAutomorphic(n) {
        // Your code goes here
    }
}

csharp

class Solution {
    public bool IsAutomorphic(int n) {
        // Your code goes here
    }
}

go

func isAutomorphic(n int) bool {
Stuck? Show a way to structure it+
  1. 01Count decimal digits, with zero as one digit
  2. 02Compute modulus 10^digits
  3. 03Compare square modulo modulus with the original number
  4. 04Use a wider numeric type or string arithmetic if needed

Reference answer

Then expect these follow-ups

  • How would you test very large integers?

    Tests: constraint adaptation

  • Which residues are automorphic in base 10?

    Tests: follow-up reasoning

Free to read · better with Enzo

Practice this out loud with Enzo

Enzo runs it as a mock interview, pushes back with follow-ups, and grades you on the rubric.

Next question