Search for a tool
Closest Prime Number

Tool to find the closest prime number of a number, the prime number nearest to a given number also called closest Prime function

Results

Closest Prime Number -

Tag(s) : Arithmetics

Share dCode and more

dCode is free and its tools are a valuable help in games, maths, geocaching, puzzles and problems to solve every day!
A suggestion ? a feedback ? a bug ? an idea ? Write to dCode!

Please, check our dCode Discord community for help requests!
NB: for encrypted messages, test our automatic cipher identifier!

Feedback and suggestions are welcome so that dCode offers the best 'Closest Prime Number' tool for free! Thank you!

# Closest Prime Number

## Nearest Prime Number Finder

 If the number is already prime Display itself Display another one

### What is the closest prime number? (Definition)

For a given number $N$, the nearest prime number is the prime number that minimizes its distance from $N$ (whose difference between itself and $N$ is minimal, such that there are no other closer primes).

### How to calculate the closest prime number?

No mathematical formula can find the nearest prime number of another given number. dCode uses algorithms based on probabilistic primality tests for numbers greater or lower than the number requested, and then checks it via deterministic algorithms.

Example: The closest prime number to 100 is 101

Example: The nearest prime number to 1000 is 997 (the next is 1009)

Example: The closest prime number to 10000 is 10007

If the next prime number and the previous prime number are at the same distance, dCode returns the next prime number.

### What are twin primes numbers?

Two prime numbers are called twins if their difference is 2 (the nearest prime number is at a distance of 2 units).

Example: 5 and 7 are twins because $7-5 = 2$

### What is the sequence of closest prime numbers to N?

The OEIS lists 2 series of numbers:

A051697: $2, 2, 2, 3, 3, 5, 5, 7, 7, 7, 11, 11, 11, 13, \dots$ which for any integer N associates its nearest prime number (the smallest in the case of a tie)

A077018: $2, 2, 2, 3, 5, 5, 7, 7, 7, 11, 11, 11, 13, 13, \dots$ which for any integer N associates its nearest prime number (the largest in case of a tie)

The 2 sequences are identical except for the number 3 which appears twice in the first sequence but only once in the second one.

## Source code

dCode retains ownership of the "Closest Prime Number" source code. Except explicit open source licence (indicated Creative Commons / free), the "Closest Prime Number" algorithm, the applet or snippet (converter, solver, encryption / decryption, encoding / decoding, ciphering / deciphering, translator), or the "Closest Prime Number" functions (calculate, convert, solve, decrypt / encrypt, decipher / cipher, decode / encode, translate) written in any informatic language (Python, Java, PHP, C#, Javascript, Matlab, etc.) and all data download, script, or API access for "Closest Prime Number" are not public, same for offline use on PC, mobile, tablet, iPhone or Android app!
Reminder : dCode is free to use.

## Cite dCode

The copy-paste of the page "Closest Prime Number" or any of its results, is allowed as long as you cite dCode!
Cite as source (bibliography):
Closest Prime Number on dCode.fr [online website], retrieved on 2023-01-29, https://www.dcode.fr/closest-prime-number

## Need Help ?

Please, check our dCode Discord community for help requests!
NB: for encrypted messages, test our automatic cipher identifier!