Skip to main content

How to Check if a Number is Prime in a Batch Script

A prime number is a whole number greater than 1 that has only two divisors: 1 and itself. Determining if a number is prime is a classic logic puzzle in computer science. While batch scripting is not designed for heavy mathematical computation, it is entirely possible to write a script that can check for primality using its basic arithmetic and looping capabilities.

This guide will teach you how to implement a prime number checker in a batch script. You will learn the core logic of "trial division," how to implement this in a FOR /L loop using the modulo operator, and how to handle the special edge cases for 0, 1, 2, and 3.

note

This is a demonstration of advanced batch scripting logic. For any serious or high-performance number theory, a more powerful language like Python or PowerShell would be vastly more efficient.

Core Logic: Trial Division

The simplest and most direct way to check if a number (N) is prime is the method of trial division. The logic is as follows:

  1. Handle the edge cases: 0 and 1 are not prime. 2 and 3 are prime.
  2. For any other number N, we check for divisibility by every integer from 2 up to the square root of N.
  3. If we find any integer in that range that divides N evenly (with a remainder of 0), then N is not prime, and we can stop immediately.
  4. If we check all the way up to the square root of N and find no divisors, then N must be prime.

(We only need to check up to the square root because if N has a divisor larger than its square root, it must also have a corresponding divisor that is smaller.)

The Key Tool: The Modulo Operator (%%)

To check if a number divides evenly into another, we use the modulo operator. This operator gives the remainder of a division. In batch, this is done inside a SET /A command.

Syntax: SET /A "Remainder = Number %% Divisor"

  • If Remainder is 0, the division is even, and the number is not prime.
  • If Remainder is not 0, the division is not even.

Script: A Full Prime Number Checker

This script implements the trial division logic to check a single number.

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

SET "NumberToCheck=17"

ECHO --- Prime Number Checker ---
ECHO Checking if %NumberToCheck% is a prime number...
ECHO.

REM --- Step 1: Handle edge cases ---
IF %NumberToCheck% LSS 2 (
ECHO %NumberToCheck% is NOT a prime number.
GOTO :EOF
)
IF %NumberToCheck% EQU 2 (
ECHO 2 is a prime number.
GOTO :EOF
)
IF %NumberToCheck% EQU 3 (
ECHO 3 is a prime number.
GOTO :EOF
)

REM --- Step 2: Check for even numbers (an easy optimization) ---
SET /A "IsEven = %NumberToCheck% %% 2"
IF %IsEven% EQU 0 (
ECHO %NumberToCheck% is NOT a prime number (it is even).
GOTO :EOF
)

REM --- Step 3: The Trial Division Loop ---
REM We only need to check odd divisors from 3 up to Number/2.
SET /A "LoopEnd = %NumberToCheck% / 2"
SET "IsPrime=1"

FOR /L %%i IN (3, 2, %LoopEnd%) DO (
SET /A "Remainder = !NumberToCheck! %% %%i"

IF !Remainder! EQU 0 (
REM We found a divisor, so it's not prime.
SET "IsPrime=0"
GOTO :Result
)
)

:Result
IF %IsPrime% EQU 1 (
ECHO %NumberToCheck% is a prime number.
) ELSE (
ECHO %NumberToCheck% is NOT a prime number.
)

ENDLOCAL

How the script works:

  • Edge Cases: The script first gets rid of the simple cases: anything less than 2 is not prime, and 2 and 3 are prime.
  • Even Number Check: It then checks if the number is even. If it is (and it's not 2), it's not prime. This is a simple optimization that halves the number of checks we need to do.
  • The Loop (FOR /L):
    • FOR /L %%i IN (3, 2, %LoopEnd%): This is the main engine. It starts at 3, and increments by 2 in each step (so it only checks odd divisors: 3, 5, 7, ...), and goes up to half of the number being checked. (Checking up to N/2 is a simpler, though slightly less efficient, substitute for the square root).
  • The Modulo Check: SET /A "Remainder = !NumberToCheck! %% %%i" performs the division and checks the remainder.
  • The Flag (IsPrime): The IsPrime variable acts as a boolean flag. It starts as 1 (true). The moment we find a divisor, we set it to 0 (false) and use GOTO to exit the loop immediately, as there is no need to check further.

Common Pitfalls and How to Solve Them

  • Performance: This is the biggest issue. Batch script is an interpreted language and is extremely slow at mathematical operations. This script is fine for checking small numbers, but it will become noticeably slow for numbers in the thousands and unusably slow for very large numbers. Solution: For any serious prime number calculation, use a compiled language or a more efficient scripting language like Python.

  • 32-bit Integer Limit: The SET /A command is limited to 32-bit signed integers. The maximum number it can handle is 2,147,483,647. This script will fail for any number larger than that.

  • Delayed Expansion: Using !NumberToCheck! inside the loop is necessary if you were to change its value, but since it is constant, using %NumberToCheck% would also work here. However, !Remainder! would still need delayed expansion if you were to read it inside the same block after setting it. Using DelayedExpansion from the start is a good habit for complex loops.

Practical Example: Finding All Primes Up to 100

This script uses our prime-checking logic inside another loop to find and display all the prime numbers between 1 and 100.

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

ECHO --- Finding all prime numbers up to 100 ---
ECHO.
SET "PrimeList="

FOR /L %%N IN (2,1,100) DO (
CALL :IsPrime %%N IsPrimeResult
IF !IsPrimeResult! EQU 1 (
SET "PrimeList=!PrimeList! %%N"
)
)

ECHO The prime numbers are:
ECHO %PrimeList%
GOTO :EOF


:IsPrime <Number> <ReturnVar>
SET "Num=%1"
SET "IsPrimeFlag=1"
IF %Num% LSS 2 SET "IsPrimeFlag=0"
IF %Num% EQU 2 GOTO :ReturnPrime
IF %Num% EQU 3 GOTO :ReturnPrime
SET /A "IsEven = %Num% %% 2"
IF %IsEven% EQU 0 SET "IsPrimeFlag=0"

SET /A "Limit = %Num% / 2"
FOR /L %%i IN (3,2,%Limit%) DO (
SET /A "Rem = !Num! %% %%i"
IF !Rem! EQU 0 ( SET "IsPrimeFlag=0" & GOTO :ReturnPrime )
)

:ReturnPrime
ENDLOCAL & SET "%2=%IsPrimeFlag%"
GOTO :EOF

This demonstrates how the logic can be encapsulated in a reusable subroutine.

Conclusion

While batch scripting is not the ideal tool for mathematical computation, it is fully capable of solving logical problems like checking for prime numbers.

  • The core logic is trial division, which is implemented by looping from 2 up to the number's half-way point (or square root).
  • The SET /A command with the modulo operator (%%) is the key to checking for divisibility.
  • This method is effective for demonstrating scripting logic but is too slow for large numbers.