Closest Woodall Prime
Brachylog, 28 26 bytes
:K≜+ℕ~lṗ.+₁~×[İ,J]h:2^₍J≜∧
Try it online!
This is too slow for TIO for inputs bigger than 4.
Explanation
:K The list [Input, K]
≜ Assign a value to K (0, then 1, then -1, then 2, etc.)
+ℕ Input + K >= 0
~l . Output is a number of length (Input + K)
ṗ. Output must be a prime number
+₁ Output + 1...
~×[İ,J] ... = İ × J, İ being an integer...
h:2^₍J ... and J = 2^İ
≜∧ Check that it is possible to find a value for İ that satisfies
all of those constraints. If not, go back to the beginning
and try another value for K through backtracking.
We use a common trick in Brachylog for "find the closest X to Y": labeling a completely free variable (which we do when we use ≜
on the list [Input, K]
will label it with this pattern: 0, 1, -1, 2, -2, 3, ...
. Therefore, when backtracking if the value Input + K
doesn't work as the number of digits, the next choice we will try will be the next possible closest choice.