Skip to content

How to Check if a Number is a Perfect Square in Batch Script

Learn how to verify if a number is a perfect square in Windows Batch using iterative root-checking or optimized Newton's method for fast, accurate validation.

A Perfect Square is a number that can be expressed as the product of an integer with itself ($n = k^2$). Examples include 1, 4, 9, 16, 25, 36, and so on. Checking if a number is a perfect square is useful for geometric calculations, data validation, and mathematical puzzles. In Batch, the simplest approach is to find the integer square root of the number and then check if squaring it gives back the original value.

In this guide, we will demonstrate how to verify perfect squares using an iterative approach.

The Strategy: Root and Verify

  1. Find the approximate integer square root of the number.
  2. Square that root.
  3. If the result equals the original number, it is a perfect square.

Implementation Script

@echo off
setlocal enabledelayedexpansion

set /p "num=Enter a number: "

:: Validate: Negative numbers cannot be perfect squares
if !num! LSS 0 (
    echo.
    echo ==========================================
    echo [NO] Negative numbers cannot be perfect squares.
    echo ==========================================
    pause
    exit /b
)

:: Find the integer square root by incrementing
set "root=0"

:find_root
set /a "square=root * root"

if !square! EQU !num! (
    echo.
    echo ==========================================
    echo [YES] !num! IS a perfect square.
    echo       Square root = !root!
    echo ==========================================
    pause
    exit /b
)

if !square! GTR !num! (
    echo.
    echo ==========================================
    echo [NO] !num! is NOT a perfect square.
    echo ==========================================
    pause
    exit /b
)

set /a "root+=1"
goto :find_root

Why Check for Perfect Squares?

  1. Grid Layouts: If a script needs to arrange items in a square grid (e.g., 16 items in a 4x4 layout), it must first verify that the total count is a perfect square.
  2. Mathematical Validation: Verifying that a computed area or dimension is geometrically valid before proceeding with a calculation.
  3. Optimization: Some algorithms can be simplified when the input is a known perfect square (e.g., binary tree dimensions).

Optimized Version (Newton's Method)

The linear search above works well for small numbers, but for larger values, using Newton's method (the Babylonian method) to approximate the root is much faster:

@echo off
setlocal enabledelayedexpansion

set /p "num=Enter a number: "

:: Validate: Negative numbers cannot be perfect squares
if !num! LSS 0 (
    echo [NO] Negative numbers cannot be perfect squares.
    pause
    exit /b
)

:: Handle 0 and 1 directly
if !num! LEQ 1 (
    echo [YES] !num! is a perfect square. Root = !num!.
    pause
    exit /b
)

:: Newton's Method for integer square root
set "guess=!num!"

for /L %%i in (1,1,30) do (
    set /a "newGuess=(guess + (num / guess)) / 2"
    if !newGuess! GEQ !guess! (
        rem Converged - stop updating
    ) else (
        set "guess=!newGuess!"
    )
)

:: Verify: Does guess^2 == num?
set /a "check=guess * guess"

if !check! EQU !num! (
    echo [YES] !num! is a perfect square. Root = !guess!.
) else (
    :: Also check guess+1 in case Newton's method undershot by one
    set /a "guessPlus=guess + 1"
    set /a "checkPlus=guessPlus * guessPlus"
    if !checkPlus! EQU !num! (
        echo [YES] !num! is a perfect square. Root = !guessPlus!.
    ) else (
        echo [NO] !num! is NOT a perfect square.
    )
)
pause

Important Considerations

Conclusion

Checking for perfect squares combines square root approximation with arithmetic verification, making it a clean exercise in iterative logic. Whether you use the simple linear scan for small numbers or Newton's Method for larger inputs, this technique adds essential mathematical validation capabilities to your Batch scripting toolkit. It ensures your scripts can make geometry-aware decisions and validate numeric properties with precision.

Navigation