Why do we check up to the square root of a number to determine if the number is prime?

Why Do We Check Up to the Square Root of a Number to Determine If the Number Is Prime?

When determining if a number ( n ) is prime, we only need to check for factors up to the square root of ( n ). This reduces the number of checks needed, significantly improving the efficiency of the algorithm. Here’s a detailed explanation of why this works:

Explanation

  1. Definition of a Prime Number:

    • A prime number is a number greater than 1 that has no positive divisors other than 1 and itself.
  2. Factors Come in Pairs:

    • Any non-prime number ( n ) can be factored into two factors, say ( a ) and ( b ), such that ( n = a \times b ).
    • If both ( a ) and ( b ) were greater than the square root of ( n ), then their product would be greater than ( n ): ( a \times b > \sqrt{n} \times \sqrt{n} = n ).
    • Therefore, at least one of the factors must be less than or equal to the square root of ( n ).
  3. Checking Up to the Square Root:

    • If ( n ) has any factors other than 1 and ( n ) itself, at least one of those factors must be less than or equal to ( \sqrt{n} ).
    • Thus, to check if ( n ) is prime, we only need to check for divisors from 2 up to ( \sqrt{n} ).

Practical Example

Suppose we want to determine if 29 is a prime number:

  • Calculate the square root of 29: ( \sqrt{29} \approx 5.39 ).
  • Check for factors up to 5:
    • 29 is not divisible by 2.
    • 29 is not divisible by 3.
    • 29 is not divisible by 4.
    • 29 is not divisible by 5.

Since none of these numbers divides 29 without a remainder, 29 is a prime number.

Mathematical Proof

If ( n ) is composite, it can be written as ( n = a \times b ), where ( 1 < a \leq b < n ). If both ( a ) and ( b ) are greater than ( \sqrt{n} ), their product ( a \times b ) would be greater than ( n ), which contradicts the assumption that ( n = a \times b ).

Therefore, if ( n ) is composite, at least one of ( a ) or ( b ) must be less than or equal to ( \sqrt{n} ). Hence, checking for factors up to ( \sqrt{n} ) is sufficient to determine the primality of ( n ).

Implementation in Python

Here's how you can implement the prime check in Python:

import math def is_prime(n): if n <= 1: return False if n <= 3: return True if n % 2 == 0 or n % 3 == 0: return False i = 5 while i * i <= n: if n % i == 0 or n % (i + 2) == 0: return False i += 6 return True # Example usage print(is_prime(29)) # Output: True print(is_prime(28)) # Output: False

Summary

  • Factors and Square Roots: To determine if ( n ) is prime, you only need to check divisors up to ( \sqrt{n} ) because any larger factor must be paired with a smaller factor.
  • Efficiency: This significantly reduces the number of checks required, improving the efficiency of the primality test.
  • Implementation: Simple algorithms and code can leverage this property to efficiently check for prime numbers.

Understanding this principle helps in creating more efficient algorithms for prime number detection and is fundamental in number theory and computational mathematics. For more in-depth knowledge and practical examples on programming concepts, consider exploring Grokking the Coding Interview on DesignGurus.io, which provides comprehensive courses on essential coding and interview techniques.

TAGS
Coding Interview
CONTRIBUTOR
Arslan Ahmad
Arslan Ahmad
ex-FAANG engineering manager and author or Grokking series.
-

GET YOUR FREE

Coding Questions Catalog

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
What are the skills required for a backend developer?
What is the difficulty level of Microsoft interview?
What is the basic concept of networking?
Why is problem-solving important in software engineering?
Why is Apple not hiring?
What to say first in a Zoom interview?
Related Courses
New
Grokking the AI System Design Interview course cover
Grokking the AI System Design Interview
Learn to design AI systems the way interviewers expect: classic ML products, LLM and RAG architectures, and agentic systems, all through the lens of the system design interview.
4.8
(1,192 learners)
Discounted price for Your Region

$123

Grokking the Coding Interview: Patterns for Coding Questions course cover
Grokking the Coding Interview: Patterns for Coding Questions
The 24 essential patterns behind every coding interview question. Available in Java, Python, JavaScript, C++, C#, and Go. The most comprehensive coding interview course with 543 lessons. A smarter alternative to grinding LeetCode.
4.6
Discounted price for Your Region

$197

Grokking Modern AI Fundamentals course cover
Grokking Modern AI Fundamentals
Master the fundamentals of AI today to lead the tech revolution of tomorrow.
4.1
Discounted price for Your Region

$72

Design Gurus logo
One-Stop Portal For Tech Interviews.
Copyright © 2026 Design Gurus, LLC. All rights reserved.