To determine if a positive integer is a prime number, we can follow these steps:

1. **Check if the number is less than 2**: Prime numbers are greater than 1. If the input is 1 or less, it’s not prime.
2. **Check for divisibility up to the square root**: If any number from 2 up to the square root of the input divides it evenly, the number is not prime. This approach optimizes the check by reducing the number of iterations needed.

Here’s the Python code implementing this logic:

“`python
import math

n = int(input())
if n <= 1: print("Not Prime") else: is_prime = True for i in range(2, int(math.sqrt(n)) + 1): if n % i == 0: is_prime = False break print("Prime" if is_prime else "Not Prime") ``` **Explanation:** - **Input Handling**: The code reads an integer input. - **Initial Check**: If the number is 1 or less, it immediately returns "Not Prime". - **Prime Check Loop**: For numbers greater than 1, the loop checks divisibility from 2 up to the integer square root of the number. If any divisor is found, the number is not prime. - **Efficiency**: By checking up to the square root, the algorithm efficiently determines primality without unnecessary iterations. This code correctly handles edge cases and efficiently checks for prime numbers using optimal mathematical properties.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top
Scroll to Top