TI-BASIC (TI-84+), 74 Bytes
Ans→A:{2,3→A:For(P,5,2A,2:If min(remainder(P,seq(X,X,3,1+√(P),2:P→∟A(1+dim(∟A:End:abs(A-∟A:∟A(1+sum(not(cumSum(Ans=min(Ans
Hexdump:
(Token hex-values found here.)
72 04 41 3E 08 32 2B 33 04 41 3E D3 50 2B 35 2B | Ans→A:{2,3→A:For(P,5,
32 41 2B 32 3E CE 1A EF 32 50 2B 23 58 2B 58 2B | 2A,2:If min(remainder(P,seq(X,X,
33 2B 31 70 BC 50 11 2B 32 3E 50 04 EB 41 10 31 | 3,1+√(P),2:P→∟A(1
70 B5 EB 41 3E D4 EE B2 41 71 EB 41 3E EB 41 10 | +dim(∟A:End:abs(A-∟A:∟A(
31 70 B6 B8 BB 29 72 6A 1A 72 | 1+sum(not(cumSum(Ans=min(Ans
Input is an integer in Ans
. Output is printed implicitly.
Note: due to limitations on how large lists can be, \$A\$ is limited to \$0 \le A \le 3953\$
Explanation:
Ans→A ;store the input in a variable called "A"
{2,3→A ;store the list {2 3} into a list called "A"
For(P,5,2A,2 ;loop from 5 to two times the input using a step
; of 2
seq(X,X,3,1+√(P),2 ;generate a list of integers from 3 to 1 + the
; square root of the loop counter using a step of 2
; (e.g. P=21, list={3 5})
remainder(P, ;then take the loop counter modulo each element
; (e.g. P=21, list={0 1})
If min( ;if all of the resulting elements are non-zero,
; then
P→∟A(1+dim(∟A ;add the loop counter's value to the list "A", the
; list of primes
End
abs(A-∟A ;subtract the input from the primes list and take
; the absolute value of each element (distance to
; each prime)
1+sum(not(cumSum(Ans=min(Ans ;find the first index of the minimum value
∟A( ;then use that index to get the value in the primes
; list at that index
;implicit output
Examples:
100:prgmCDGF1F
101
5:prgmCDGF1F
5
80:prgmCDGF1F
79