## Nearest Prime Number Finder

## Answers to Questions

### How to calculate the closest prime number?

No formula can find the **nearest prime** number of another given number. dCode uses algorithms based on probabilistic primality tests for numbers higher 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, ... 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, ... 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 source code of the script Closest Prime Number online. Except explicit open source licence (indicated Creative Commons / free), any algorithm, applet, snippet, software (converter, solver, encryption / decryption, encoding / decoding, ciphering / deciphering, translator), or any function (convert, solve, decrypt, encrypt, decipher, cipher, decode, code, translate) written in any informatic langauge (PHP, Java, C#, Python, Javascript, Matlab, etc.) which dCode owns rights will not be released for free. To download the online Closest Prime Number script for offline use on PC, iPhone or Android, ask for price quote on contact page !