Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
Find the prime integer closest to a number
ResourceFunction["ClosestPrime"][n] returns the prime number closest to n. |
Find the closest prime to 100:
In[1]:= |
Out[1]= |
Find the closest prime to 15:
In[2]:= |
Out[2]= |
The input to ClosestPrime need not be an integer:
In[3]:= |
Out[3]= |
Symbolically specified numbers may also be used:
In[4]:= |
Out[4]= |
Negative numbers are not considered to be prime. Inputting a negative number will always return 2:
In[5]:= |
Out[5]= |
ClosestPrime is similar but not identical to NextPrime. ClosestPrime[n] returns the prime number to closest to n, considering numbers less than, equal to, or greater than n. NextPrime[n] returns the closest prime number to n, considering only numbers greater than n:
In[6]:= |
Out[6]= |
In[7]:= |
Out[7]= |
This work is licensed under a Creative Commons Attribution 4.0 International License