Skip to content

Commit

Permalink
Create NearestPrime.py
Browse files Browse the repository at this point in the history
Title: 
Find the Nearest Prime Number

Description: 
This program determines the closest prime number to a given integer a. It checks both higher and lower values relative to a and compares their distances to identify the nearest prime. If two primes are equidistant, it selects the smaller one.
  • Loading branch information
Manojkumar2806 authored Oct 12, 2024
1 parent 809aecf commit 6146768
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions Python/NearestPrime.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
a=int(input("Enter the number:"))
for i in range(a+1):
prime=a+i
count=0
check=False
for j in range(1,prime+1):
if(prime%j==0):
count+=1
if count==2:
check=True
if check:
n=i
break

for i in range(a+1):
prime=a-i
count=0
check=False
for j in range(1,prime+1):
if(prime%j==0):
count+=1
if count==2:
check=True
if check:
p=i
break

if n>p:
print("Nearest prime is",a+p)
else:
print("Nearest prime is",a+n)

0 comments on commit 6146768

Please sign in to comment.