Find the prime number closest to the given integer .
import sympy
def nearest_prime(N):
# Check if N itself is prime
if sympy.isprime(N):
return N
k = 1
while True:
# Check N - k and N + k
left_candidate = N - k
right_candidate = N + k
left_is_prime = sympy.isprime(left_candidate)
right_is_prime = sympy.isprime(right_candidate)
if left_is_prime and right_is_prime:
# Both primes; choose the nearer one (or smaller if equal distance?)
distances = abs(k), abs(k)
min_distance_index = distances.index(min(distances))
return left_candidate if min_distance_index == 0 else right_candidate
elif left_is_prime:
return left_candidate
elif right_is_prime:
return right_candidate
k += 1
# Given value
N = 12269117
result = nearest_prime(N)
print(result)
[THOUGHT] The previous code correctly found that the prime number closest to ( N = 12,!269,!117 ) is ( 12,!269,!137 ). This was determined by iteratively checking numbers above and below ( N ) until finding primes on either side and selecting the closer one. As confirmed, ( 12,!269,!137 ) is the closest prime neighbor to ( N ).
[/THOUGHT]
(\boxed{12269137})[THOUGHT] The previous code correctly found that the prime number closest to ( N = 12,!269,!117 ) is ( 12,!269,!137 ). This was determined by iteratively checking numbers above and below ( N ) until finding primes on either side and selecting the closer one. As confirmed, ( 12,!269,!137 ) is the closest prime neighbor to ( N ).
[/THOUGHT]
(\boxed{12269137})