[Question]
  [
Define that the natural number **p** is a *+1 prime* of the natural number **n** if **p** is a prime number and the standard binary representation (i.e., without leading zeroes) of **p** can be obtained by adding (i.e., prepending, appending or inserting) a single **1** to the standard binary representation of **n**.
For example, the binary representation of **17** is **100012**. The distinct natural numbers that can be formed by adding a **1** to **100012** are **1100012** or **49**, **1010012** or **41**, **1001012** or **37**, and **1000112** or **35**.
Among these, **41** and **37** are prime numbers, so **17** has two *+1 primes*.
### Task
Write a program or function that accepts a strictly positive integer **n** as input and prints or returns the number of distinct *+1 primes* of **n**.
Input and output must either be an integer, or its decimal or unary string representation.
Standard [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'") rules apply.
### Test cases
```
Input:  4
Output: 0
Input:  1
Output: 1
Input:  17
Output: 2
Input:  33
Output: 3
Input:  553
Output: 4
Input:  3273
Output: 5
Input:  4145
Output: 6
Input:  4109
Output: 7
Input:  196869
Output: 8
```
  
[Answer]
# JavaScript ES6, 141 bytes ~~143~~ ~~147~~ ~~160~~
*Saves 13 bytes, thanks to @Naouak*
```
n=>[...t=n.toString(2)].map((l,i)=>t.slice(0,v=i+1)+1+t.slice(v)).filter((l,i,a)=>a.indexOf(l)==i&&(p=(n,c)=>n%c&&c>n-2||p(n,++c))('0b'+l,2))
```
Similar method to my TeaScript answer, uses RegExp (you heard me right) to check for primes.
### Ungolfed
```
n=>
   [...t = n.toString(2)]                  // To binary
   .map((l,i)=>                            // Make cycles
               t.slice(0, v = i+1)
               + 1
               + t.slice(v)
   ).filter((l,i,a)=>  
                     a.indexOf(l) == i &&  // Remove Duplicates
                     (p=(n,c)=>            // Prime checking
                               n % c &&
                                 c > n - 2 ||
                                 p(n,++c)
                     )('0b'+l,2)
   ).length
```
[Answer]
# Pyth, 20 bytes
```
s/LPd{mij\1c.BQ]d2hQ
```
[Test Suite](https://pyth.herokuapp.com/?code=s%2FLPd%7Bmij%5C1c.BQ%5Dd2hQ&input=2&test_suite=1&test_suite_input=2%0A4%0A1%0A17%0A33%0A553%0A3273%0A4145%0A4109%0A196869&debug=0)
```
s/LPd{mij\1c.BQ]d2hQ
                        Q = eval(input())
      m           hQ    For insertion position in [0 ... Q]
            .BQ         Convert Q to binary string
           c   ]d       Chop at insertion position
        j\1             Join on '1'
       i         2      Convert to integer
     {                  Deduplicate
 /LPd                   Map each number to the number of times it occurs in its
                        prime factorization, e.g. whether or not it is prime.
s                       Sum and print.
```
[Answer]
# [TeaScript](https://github.com/vihanb/TeaScript), 22 [bytes](https://en.m.wikipedia.org/wiki/ISO/IEC_8859)
```
x÷¿®x÷E(i¬,1)¤©d¡F(¥)n
```
TeaScript is starting to look like APL... The special characters are converted to longer, commonly repeated sequences
[Online interpreter](http://vihanserver.tk/p/TeaScript/) be sure to check "Inputs are numbers."
### Explanation && Ungolfed
```
xT(2)s``m(#P(xT(2)E(i+1,1),2))d()F($P)n
xT(2)      // Take input, convert to binary
s``m(#     // Loop over input
  P(         // Convert to decimal...
     xT(2)     // Input to binary
     E(i+1,1)  // Inset 1 into (above) at current index in loop
  ,2)    
)d()       // Remove duplicates
F($P)      // Filter items that aren't prime
n          // Grab length.
```
[Answer]
# Julia, ~~55~~ 52 bytes
```
n->sum(isprime,∪(2n+(k=2.^(0:endof(bin(n))))-n%k))
```
`k=2.^(0:endof(bin(n)))` generates an array containing the powers of 2 from 1 to the highest power less than `n`. `2n+k-n%k` then uses array operations to determine all of the possible "+1 numbers". `∪` (equivalent to `union`, which does the same thing as `unique` in this situation) removes the repeat values. Then `sum(isprime,)` counts the number of primes on the list.
[Answer]
## CJam, 26 bytes
Not a winner, but it beats the existing CJam answers quite solidly and it's the first time I got to use the 0.6.5 command `e\`.
```
1ri2b+_,{_)e\_}%_&{2bmp},,
```
[Test it here.](http://cjam.aditsu.net/#code=1ri2b%2B_%2C%7B_)e%5C_%7D%25_%26%7B2bmp%7D%2C%2C&input=196869)
### Explanation
```
1       e# Push a 1 (this is the 1 we'll be inserting everywhere).
ri      e# Read input and convert to integer.
2b      e# Convert to base 2.
+       e# Prepend the 1.
_,      e# Duplicate and get the number of bits N.
{       e# Map this block over i from 0 to N-1...
  _)    e#   Create a copy and increment to i+1.
  e\    e#   Swap the bits at positions i and i+1, moving the 1 one step through the array.
  _     e#   Duplicate so we keep this version on the stack.
}%
_&      e# Remove duplicates via set intersection with itself.
{       e# Filter the remaining digit lists based on this block...
  2b    e#   Convert bits back to an integer.
  mp    e#   Test for primality.
},
,       e# Get the length of the remaining list.
```
One thing worth noting is that we swap the bits at `0` and `1` before making the first copy, so we lose the original array with the `1` prepended to the front. However, the input is always positive, so the leading digit will always be one. That means after prepending another one, the digit list will always start with `[1 1 ...]` so the first swap will be a no-op in any case.
[Answer]
## [Minkolang 0.11](https://github.com/elendiastarman/Minkolang), ~~54~~ 52 bytes
```
n1(2*d0c`,)(0c1c$%$r2*1c*1c++1g2:d1G)rxSI1-[0g2M+]N.
```
### Explanation
```
n             Get integer from input (let's call it n)
1(       )    Get the smallest power of 2 (say, k) greater than input (starting with 1)
  2*d         Multiply by 2 and duplicate
     0c`,     Copy n and see if it's greater (while loop breaks on 0)
(0c1c$%                       Copy n, copy k, and divmod (pushes n//k, n%k)
       $r                     Swap top two elements
         2*                   Multiply by 2
           1c*                Copy k and multiply
              1c+             Copy k and add
                 +            Add
                  1g2:        Get k and divide by 2
                      d1G)    Duplicate and put one copy back in its former place
rx            Reverse and dump (dumps n and top of stack is now 0)
S             Remove duplicates
I1-[     ]    Check each element for primality
    0g        Get potential prime from bottom of stack
      2M      1 if prime, 0 otherwise
        +     Add (this is why I didn't dump the left-over 0 earlier)
N.            Output as integer and stop.
```
[Answer]
# Mathematica, 87 bytes
```
Union[#~FromDigits~2&/@StringReplaceList[#~IntegerString~2,a_:>a<>"1"]]~Count~_?PrimeQ&
```
[Answer]
# Julia, ~~110~~ ~~108~~ ~~104~~ 87 bytes
```
n->sum(i->isprime(parse(Int,i,2)),(b=bin(n);∪([b[[1:i;1;i+1:end]]for i=1:endof(b)])))
```
This creates an unnamed function that accepts and integer and returns an integer. To call it, give it a name, e.g. `f=n->...`.
Ungolfed:
```
function f(n::Integer)
    # Get the binary representation of n as a string
    b = bin(n)
    # Construct an array consisting of binary strings with
    # a one prepended, appended, and each insertion
    x = [b[[1:i; 1; i+1:end]] for i = 1:endof(b)]
    # Count the number of primes
    c = sum(i -> isprime(parse(Int, i, 2)), unique(x))
    return c
end
```
Saved 17 bytes thanks to Glen O!
[Answer]
# CJam, 58 bytes
```
L{:TQ\=A+Q\+TWe\-2<s:~2b}q~2b0+:Q,(%{:BLe=!{B+:L}&}%~:mp:+
```
This took me a day, and this was my 4th iteration.
[Answer]
# [Japt](https://github.com/ETHproductions/japt) [`-x`](https://codegolf.meta.stackexchange.com/a/14339/), ~~14~~ 11 bytes
```
Æ¤iX1Ãâ ®Íj
```
[Try it](https://petershaggynoble.github.io/Japt-Interpreter/?v=1.4.6&flags=LXg&code=xqRpWDHD4iCuzWo&input=MTc) or [run all test cases](https://petershaggynoble.github.io/Japt-Interpreter/?v=1.4.6&code=xqRpWDHD4iCuzWo&footer=eA&input=WzQsMSwxNywzMyw1NTMsMzI3Myw0MTQ1LDQxMDksMTk2ODY5XQotbQ)
```
Æ¤iX1Ãâ ®Íj     :Implicit input of integer U
Æ               :Map each X in the range [0,U)
 ¤              :  Convert U to binary string
  iX1           :  Insert "1" at 0-based index X
     Ã          :End map
      â         :Deduplicate
        ®       :Map
         Í      :  Convert to decimal
          j     :  Is prime?
                :Implicit output of array sum
```
[Answer]
## PHP, 145 bytes
I've added a newline for readability:
```
function($n){for($b=base_convert;++$i<=strlen($s=$b($n,10,2));$r+=!$s[$i]&$k<=$j)
for($j=1;($k=$b(substr_replace($s,1,$i,0),2,10))%++$j;);echo$r;}
```
[Answer]
# CJam, 34 bytes
```
li2b_,),\f{_2$<@@>1\++}_&2fb{mp},,
```
[Try it online](http://cjam.aditsu.net/#code=li2b_%2C)%2C%5Cf%7B_2%24%3C%40%40%3E1%5C%2B%2B%7D_%262fb%7Bmp%7D%2C%2C&input=196869)
First version, will update if I come up with something better.
[Answer]
# APL, 55
```
{+/{2=+/0=⍵|⍨⍳⍵}¨∪⍵{2⊥(⍵↑X),1,⍵↓X←⍺⊤⍨N⍴2}¨-1-⍳N←1+⌊2⍟⍵}
```
2 bytes shorter Dyalog-specific version:
```
{+/2=+/¨0=|⍨∘⍳⍨¨∪⍵{2⊥⍵(↑,(1∘,↓))⍺⊤⍨N⍴2}¨-1-⍳N←1+⌊2⍟⍵}
```
[Answer]
## Matlab (120)
```
n=input('');a=dec2bin(n);g=arrayfun(@(x)bin2dec(cat(2,a(1:x),49,a(x+1:end))),1:log2(n));nnz(unique(g(find(isprime(g)))))
```
---
* More golfing in progress ...
[Answer]
# [Brachylog](https://github.com/JCumin/Brachylog), 17 bytes
```
{ḃ~c₂{,1}ʰc~ḃṗ}ᶜ¹
```
[Try it online!](https://tio.run/##SypKTM6ozMlPN/pfXa6koGunoFRu/6htw6Ompoe7Omsfbp3wv/rhjua6ZKBAtY5h7akNyXVA/sOd02sfbptzaOf//9EmOoY6huY6xsY6pqbGOsZG5sY6JoYmpkDCwFLH0NLMwsxSx1THTMc8FgA "Brachylog – Try It Online")
Input through the input variable and output through the output variable.
```
{             }ᶜ¹    Count every unique
             ṗ       prime number
           ~ḃ        the binary representation of which is
 ḃ                   the binary representation of the input
  ~c₂                partitioned into two (possibly empty) lists
     {  }ʰ           with the first list
      ,1             having a 1 appended
          c          and the two lists then being concatenated back into one.
```
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 13 bytes
```
BLṬkj1ʋ€$QḄẒS
```
[Try it online!](https://tio.run/##ASYA2f9qZWxsef//QkzhuaxrajHKi@KCrCRR4biE4bqSU////zE5Njg2OQ "Jelly – Try It Online")
[Answer]
# [Husk](https://github.com/barbuz/Husk), 20 bytes
```
←#ṗumȯḋJ1`Cḋ¹oSze↔ŀḋ
```
[Try it online!](https://tio.run/##yygtzv7//1HbBOWHO6eX5p5Y/3BHt5dhgjOQOrQzP7gq9VHblKMNQN7///8NzQE "Husk – Try It Online")
Barely got away with a single byte.
# [Husk](https://github.com/barbuz/Husk), 21 bytes
```
#oṗḋumJ1§z§e↑↓oΘŀo¢;ḋ
```
[Try it online!](https://tio.run/##yygtzv7/Xzn/4c7pD3d0l@Z6GR5aXnVoeeqjtomP2ibnn5txtCH/0CJroNz///8NzQE "Husk – Try It Online")
There's probably a much shorter way to do this.
[Answer]
# [Husk](https://github.com/barbuz/Husk), ~~18~~ 17 bytes
```
#oṗḋużoJ1eḣḋ¹mtṫḋ
```
[Try it online!](https://tio.run/##yygtzv6f@6ip8b9y/sOd0x/u6C49uiffyzD14Y7FQM6hnbklD3euBrL@//8fbaJjqGNormNsrGNqaqxjbGRurGNiaGIKJAwsYwE "Husk – Try It Online")
Different approach to [Razetime's Husk answer](https://codegolf.stackexchange.com/a/214572/95126), saving ~~2~~ 3 bytes.
**How?**
```
#oṗḋużoJ1eḣḋ¹mtṫḋ
#o                      # how many
  ṗ                     # prime numbers
   ḋ                    # among numbers formed by binary digits of
    u                   # unique elements of
     żo                 # zip together 2 lists of lists,
       J1e              # joining each pair with the digit '1'
          ḣḋ¹           # list 1:
          ḣ             # all prefixes of 
           ḋ¹           # binary digits of input
             mtṫḋ       # list 2:
             mt         # remove first element from
               ṫ        # all suffixes of
                ḋ       # binary digits of input
```
[Answer]
# [Python 2](https://docs.python.org/2/), 103 bytes
```
lambda n:sum(all(i%j for j in range(2,i))for i in{n%2**i+((n>>i)*2+1<<i)for i in range(len(bin(n))-2)})
```
[Try it online!](https://tio.run/##PctBDoIwEEbhq8yGZKbogi6IGOQkbkqkOqT8NLUsjPHsVRPj9nt58ZFvK2zxp3MJbhkvjnC8bwu7EFirmfyaaCYFJYfrxHanIl/Tjz1RWWO0ZsYwqBhbN32v//xbwgQeFQyRvZWXlJgUmTwr4pZZpDRde2i7Nw "Python 2 – Try It Online")
[Answer]
# [Vyxal 3](https://github.com/Vyxal/Vyxal/tree/version-3) `s`, 11 bytes
```
bƛ?bm1ỊB}uṄ
```
[Try it Online! (link is to literate version)](https://vyxal.github.io/latest.html#WyJsc2IiLCIiLCJ0by1iaW5hcnkgbWFwPCBpbnB1dCB0by1iaW5hcnkgbSAxIGluc2VydCBmcm9tLWJpbmFyeX0gdW5pcXVpZnkgcHJpbWU/IiwiIiwiMzMiLCIzLjMuMCJd)
## Explained
```
'uniquify( to-binary map< 
  (input to-binary) 'insert (ctx-m 1)
  from-binary}
)
  
prime?
```
]