Check Automorphic Number in PHP

Beginner
⏱️ 11 min read
📚 Updated: May 2026
🎯 2 Code Examples
Digits & modulus

What you’ll learn

  • The automorphic condition in base 10: ends with the same decimal representation as n.
  • Two equivalent PHP patterns: n² mod 10k and digit-by-digit comparison from the right.
  • Why integer 10k beats floating pow for exact suffix checks.
  • A browser live preview, edge cases, and complexity notes for interviews.

Overview

An automorphic number (in base 10) is a positive integer n whose square ends in n. Example: 25² = 625 ends in 25.

Two programs

A single-value check and a range scan.

Live preview

See , the suffix, and the verdict instantly.

Interview polish

Exact integer modulus and clean guards for n ≤ 0.

Prerequisites

You should be comfortable with loops, %, and intdiv().

  • PHP basics: variables, if, for/while, functions.
  • Digit operations with $n % 10 and intdiv($n, 10).
  • Optional contrast with Armstrong numbers.

What is an automorphic number?

Let k be the number of decimal digits of a positive integer n. Then n is automorphic when the last k decimal digits of equal n.

n² mod 10k = n.

Mathematical definition

Work in base 10. If n has exactly k ≥ 1 decimal digits, then n is automorphic when n² ≡ n (mod 10k).

Worked example: n = 76

76² = 5776, and 5776 mod 100 = 76, so 76 is automorphic.

Intuition and examples

76Automorphic
Square
76² = 5776
Last 2 digits
76
Verdict
Suffix matches n.
12Not automorphic
Square
12² = 144
Last 2 digits
44
Verdict
44 ≠ 12.
25Automorphic
Square
25² = 625
Last 2 digits
25
Verdict
Classic two-digit example.

Live preview

Enter a positive integer n. The widget uses JavaScript BigInt for exact and suffix checks.

Use integers n ≥ 1. For this preview, keep n ≤ 109.

Live result
Press “Run check” to see n², suffix, and verdict.

Algorithm

Goal: decide whether a positive integer n is automorphic in base 10.

Validate n

Reject n ≤ 0.

Count decimal digits k

Repeatedly divide by 10 using intdiv.

Form square and modulus

Compute , then build 10k with a loop.

Compare suffix

Return true if (n²) mod 10k == n.

📜 Pseudocode

Pseudocode
function digitCount(n):
    k <- 0
    t <- n
    while t > 0:
        k <- k + 1
        t <- floor(t / 10)
    return k

function isAutomorphic(n):
    if n <= 0:
        return false
    k <- digitCount(n)
    return (n*n mod 10^k) = n
1

Check a single number (suffix modulus)

Computes 10k using integer multiplication so suffix extraction stays exact.

php
<?php
function pow10int(int $k): int
{
    $p = 1;
    for ($i = 0; $i < $k; $i++) {
        $p *= 10;
    }
    return $p;
}

function isAutomorphic(int $num): bool
{
    if ($num <= 0) {
        return false;
    }

    $k = 0;
    $t = $num;
    while ($t > 0) {
        $k++;
        $t = intdiv($t, 10);
    }

    $square = $num * $num;
    $mod = pow10int($k);
    return ($square % $mod) === $num;
}

$number = 76;
echo isAutomorphic($number)
    ? $number . " is an automorphic number."
    : $number . " is not an automorphic number.";
?>
2

Automorphic numbers in a range (digit peel)

Same predicate as Example 1, implemented by comparing low digits of n and .

php
<?php
function isAutomorphicPeel(int $number): bool
{
    if ($number <= 0) {
        return false;
    }

    $square = $number * $number;
    while ($number > 0) {
        if (($number % 10) !== ($square % 10)) {
            return false;
        }
        $number = intdiv($number, 10);
        $square = intdiv($square, 10);
    }
    return true;
}

$start = 1;
$end = 50;
echo "Automorphic numbers in the range $start to $end:\\n";
for ($i = $start; $i <= $end; $i++) {
    if (isAutomorphicPeel($i)) {
        echo $i . " ";
    }
}
?>

Optimization

Precompute 10k. If many values share a digit length, reuse one modulus.

Use integer arithmetic. Keep suffix checks exact.

Choose one explanation style. Modulus and digit-peel are equivalent mathematically.

❓ FAQ

In base 10, a positive integer n is automorphic if n squared ends with the same decimal digits as n. Equivalently, if n has k digits, then n^2 mod 10^k equals n.
No. Automorphic refers to the suffix of n^2 matching n. Circular/cyclic numbers usually refer to different digit-rotation ideas.
Some definitions allow 0 because 0^2 = 0. The sample programs here return false for n <= 0 so beginner digit loops stay simple and consistent.
For exact suffix checks, integer multiplication to build 10^k is safer than floating behavior from pow.
Let k be the number of decimal digits of n. Counting digits and checking suffix are O(k), and k is Theta(log n), so one test is O(log n).
Yes, for very large n. Typical interview inputs are small enough, but very large values may exceed platform integer limits.

🔄 Input / output examples

Value of $numberTypical line printed
7676 is an automorphic number.
1212 is not an automorphic number.
55 is an automorphic number.
00 is not an automorphic number.

Edge cases and pitfalls

Input

n ≤ 0

Return false early for beginner-friendly behavior.

Floats

pow(10, k)

Prefer integer multiplication for exact suffix checks.

Overflow

Large square

Very large n can exceed integer limits.

⏱️ Time and space complexity

TaskTimeExtra space
Single testO(log n)O(1)
All n in [1, U]O(U log U)O(1)

Summary

  • Definition: ends with n.
  • Code: use modulus or digit-peel approach with integers.
  • Watch-outs: input guards, floating behavior, and very large squares.
Did you know?

In base 10, 25 is automorphic because 25² = 625 ends in 25. So is 76 (76² = 5776).

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful