---
title: "Check if a number is Prime"  
description: "Check if a number is Prime"  
author: "ICSM Computer"  
published: 2025-09-08  
updated: 2025-09-09  
canonical: https://www.mindstick.com/forum/161906/check-if-a-number-is-prime  
category: "python"  
tags: ["python-3.4", "Python 3"]  
reading_time: 1 minute  

---

# Check if a number is Prime

**[Check if](https://www.mindstick.com/forum/12878/how-to-check-if-an-asp-dot-net-file-upload-control-has-a-file-in-jquery) a number is Prime with example and code.**

## Replies

### Reply by Anubhav Sharma

```python
def is_prime(n):
    # 1. Prime numbers are greater than 1
    if n < 2:
        return False

    # 2. Loop from 2 up to √n (square root of n)
    #    If a number has a divisor larger than its square root,
    #    the matching smaller divisor would already be found.
    for i in range(2, int(n**0.5) + 1):
        # 3. If n is divisible by i, then it's not a prime
        if n % i == 0:
            return False

    # 4. If no divisors were found, n is prime
    return True

# Test the function
print(is_prime(17))  # True → 17 is a prime number
print(is_prime(18))  # False → 18 is divisible by 2, 3, 6, 9
```

## Answer:

```python
17 → True
18 → False
```

## Explanation:

- Prime means divisible only by 1 and itself.
- We only [check](https://yourviews.mindstick.com/story/2248/never-forget-to-check-these-specifications-before-buying-a-mobile-phone) up to `sqrt(n)` for efficiency.


---

Original Source: https://www.mindstick.com/forum/161906/check-if-a-number-is-prime

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
