A305445 Minimum number of bit inversions to convert n into a prime.
0, 0, 1, 0, 1, 0, 2, 1, 1, 0, 1, 0, 2, 1, 1, 0, 1, 0, 2, 1, 1, 0, 2, 1, 2, 1, 1, 0, 1, 0, 2, 1, 1, 1, 1, 0, 2, 1, 1, 0, 1, 0, 2, 1, 1, 0, 2, 1, 2, 1, 1, 0, 2, 1, 2, 1, 1, 0, 1, 0, 2, 1, 2, 1, 1, 0, 2, 1, 1, 0, 1, 0, 2, 1, 2, 1, 1, 0, 2, 1, 1, 0, 3, 2, 2, 1, 1, 0, 2, 1, 2, 1, 2, 1, 1, 0, 2, 1, 1, 0, 1, 0, 2, 1, 1
Offset: 2
Examples
For n = 8, the binary representation 1000 cannot be turned into a prime with only one bit inversion, but 0010, where both the first and third bits from the left are inverted, is the prime 2, so a(8) = 2. (There are other primes possible with two inversions in this case: 1011 (11 decimal) and 1101 (13 decimal).)
Links
- Robert Israel, Table of n, a(n) for n = 2..10000
- Programing Puzzles & Code Golf Stack Exchange, Toggle some bits and get a square
Programs
-
Maple
f:= proc(n) local m,d,x; if isprime(n) then return 0 fi; m:= ilog2(n); for d from 1 do for x in combinat:-choose([$0..m],d) do if isprime(Bits:-Xor(n, add(2^i,i=x))) then return d fi od od end proc: map(f, [$2..200]); # Robert Israel, Aug 20 2018
-
PARI
{a(n) = my(b, L, N, s, v); if(n < 2, , if(isprime(n), 0, b = binary(n); L = #b; for(j = 1, L, v = vector(j, Y, [1, L]); forvec(X = v, N = n + sum(k = 1, j, if(b[X[k]], s = -1, s = 1); s*2^(L - X[k])); if(isprime(N), return(j)), 2))))}
Comments