Write a Python function that checks if a given number is prime

Quick Overview

Write a Python function that returns whether a positive integer up to 10,000 is prime. It tests edge cases such as 1, 2 and perfect squares, an efficient divisibility check that avoids testing every smaller number, and reasoning about time complexity.

Write a Python function that checks if a given number is prime

Company: LTIMindtree

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Write a Python function that checks whether a given number `n` is prime. A prime number is a positive integer greater than 1 that is divisible only by 1 and itself. The function should return `True` if `n` is prime and `False` otherwise. ### Function Signature ```python def is_prime(n: int) -> bool: ``` ### Rules - 1 is not prime, so `is_prime(1)` returns `False`. - 2 and 3 are prime. ### Constraints - `1 <= n <= 10^4` ### Examples ```text Input: n = 5 Output: True ``` 5 is not divisible by 2, 3 or 4, so its only divisors are 1 and 5. ```text Input: n = 10 Output: False ``` 10 = 2 × 5, so it has divisors other than 1 and itself. ```text Input: n = 49 Output: False ``` 49 = 7 × 7. Its only divisor other than 1 and 49 is 7, which is exactly its square root, so a divisor search has to include the square root itself.

Overview: Write a Python function that returns whether a positive integer up to 10,000 is prime. It tests edge cases such as 1, 2 and perfect squares, an efficient divisibility check that avoids testing every smaller number, and reasoning about time complexity.

LTIMindtree logo
LTIMindtree
Nov 25, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Write a Python function that checks whether a given number n is prime. A prime number is a positive integer greater than 1 that is divisible only by 1 and itself.

The function should return True if n is prime and False otherwise.

Function Signature

def is_prime(n: int) -> bool:

Rules

  • 1 is not prime, so is_prime(1) returns False .
  • 2 and 3 are prime.

Constraints

  • 1 <= n <= 10^4

Examples

Input: n = 5
Output: True

5 is not divisible by 2, 3 or 4, so its only divisors are 1 and 5.

Input: n = 10
Output: False

10 = 2 × 5, so it has divisors other than 1 and itself.

Input: n = 49
Output: False

49 = 7 × 7. Its only divisor other than 1 and 49 is 7, which is exactly its square root, so a divisor search has to include the square root itself.