05AB1E, 1515 14 bytes
∞+.ΔαD3mαy¿yp≠*ΔαD3mαy¿yp+
Inspired by @Giuseppe's Gaia answer, so make sure to upvote him as well!
Try it onlineTry it online or verify all test casesverify all test cases.
Explanation:
∞ # Push an infinite positive list: [1,2,3,...]
+ # Add the (implicit) input `n` to each: [n+1,n+2,n+3,...]
.Δ # Find the first `n+b` which is truthy for:
α # Take the absolute difference with the (implicit) input: [1,2,3,...]
D # Duplicate this `b`
3m # Cube it: b³
α # Take the absolute difference with the `b` we've duplicated: |b-b³|
y¿ # Get the greatest common divisor with the current `b+n`: gcd(|b-b³|,b+n)
yp≠yp # Check thatwhether `b+n` is NOT a prime number: !isPrime(b+n1 if prime; 0 if not)
*+ # MultiplyAdd them together: gcd(|b-b³|,b+n)*!isPrime+isPrime(b+n)
# (NOTE: only 1 is truthy in 05AB1E)
# (after which the result is output implicitly as result)