[Question]
  [
A prime is weak if the closest other prime is smaller than it. If there is a tie the prime is not weak.
For example **73** is a weak prime because **71** is prime but **75** is composite.
## Task
Write some computer code that when given a prime greater than **2** as input will determine if it is a weak prime. This is a standard [decision-problem](/questions/tagged/decision-problem "show questions tagged 'decision-problem'") so you should output two unique values for each of the two cases (e.g. `weak` and `not weak`).
This is [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'") so standard rules for the tag apply.
## OEIS
Here are the first 47 weak primes:
```
3, 7, 13, 19, 23, 31, 43, 47, 61, 73, 83, 89, 103, 109, 113, 131, 139, 151, 167, 181, 193, 199, 229, 233, 241, 271, 283, 293, 313, 317, 337, 349, 353, 359, 383, 389, 401, 409, 421, 433, 443, 449, 463, 467, 491, 503, 509, 523, 547, 571, 577, 601, 619, 643, 647
```
Here is the OEIS for weak primes (should return `weak`) [OEIS A051635](http://oeis.org/A051635)
Here is the OEIS for balanced primes (should return `not weak`) [OEIS A006562](http://oeis.org/A006562)
Here is the OEIS for strong primes (should return `not weak`) [OEIS A051634](http://oeis.org/A051634) 
  
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 7 bytes
```
Æn+Æp>Ḥ
```
[Try it online!](https://tio.run/##y0rNyan8//9wW5724bYCu4c7lvz//9/Q0BgA "Jelly – Try It Online")
### Explanation
```
           See if
Æn         the next prime
  +Æp      plus the previous prime
     >Ḥ    is greater than 2n
```
As a bonus, changing `>` to `=` or `<` checks for balanced and strong primes, respectively.
[Answer]
## Mathematica, 24 bytes
```
n=NextPrime;2#+n@-#<n@#&
```
The `NextPrime` built-in can be (ab?)used to compute the previous prime by feeding it a negative argument.
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 9 [bytes](https://github.com/DennisMitchell/jelly/wiki/Code-page)
```
ḤÆRạÞ⁸ḊḢ>
```
Returns `1` for weak and `0` for not weak or balanced (returns `1` for an input of `2`)
**[Try it online!](https://tio.run/##ASEA3v9qZWxsef//4bikw4ZS4bqhw57igbjhuIrhuKI@////NTM "Jelly – Try It Online")**
### How?
```
ḤÆRạÞ⁸ḊḢ> - Link: prime number > 2, p
Ḥ         - double -> 2*p
 ÆR       - yield primes between 2 and 2*p inclusive
     ⁸    - chain's left argument, p
    Þ     - sort by:
   ạ      -   absolute difference (i.e. distance from p)
      Ḋ   - dequeue (removes p from the list, since it has distance zero)
       Ḣ  - head (gives us the nearest, if two the smallest of the two)
        > - greater than p?
```
[Answer]
# [PHP](https://php.net/), 69 bytes
prints one for weak prime and nothing for not weak prime
```
for(;!$t;$t=$d<2)for($n=$d=$argn+$i=-$i+$w^=1;$n%--$d;);echo$n<$argn;
```
[Try it online!](https://tio.run/##K8go@G9jX5BRwKWSWJSeZ6tkbqxk/T8tv0jDWlGlxFqlxFYlxcZIEySgkgdk24KVaatk2uqqZGqrlMfZGlqr5Knq6qqkWGtapyZn5Kvk2YDVWP//DwA "PHP – Try It Online")
[Answer]
# Octave, ~~93~~ 84 bytes
*Thanks to @LuisMendo and @rahnema1 for saving bytes!*
```
function r=f(x);i=j=x;do--i;until(i<1|isprime(i));do++j;until(isprime(j));r=x-i<j-x;
```
[Try it online!](https://tio.run/##TY1BCsIwFET3OcVHKCS0gdalaW7iRpoEJ9RUaqpZePeYBgVn998M7y9TvDxtzm4LU8QSaNWOJ6GgvU7KLFJCbSFi5hiHNx73FTfLIUTp2tb/ui/3ha86SYxeJpVtMAz6qBh7XTFb2h193wtGJXDE/3yV7SkkRMcPjTlRY87h0BE6cvVnHe3WKtBoB8XKmT8)
[Answer]
# Maxima, 42 bytes
```
f(n):=is(n-prev_prime(n)<next_prime(n)-n);
```
[Try it online!](https://tio.run/##y02syMxN/P8/TSNP08o2s1gjT7egKLUsvqAoMzcVKGaTl1pRAufp5mla/@cC8vJKNNI0DA01Na0RPGMUnjkKzxLE@w8A "Maxima – Try It Online")
[Answer]
# [MATL](https://github.com/lmendo/MATL), 13 bytes
```
qZq0)G_Yq+GE>
```
This outputs `1` if weak, `0` otherwise. 
[Try it online!](https://tio.run/##y00syfn/vzCq0EDTPT6yUNvd1e7/f3NjAA "MATL – Try It Online")
### Explanation
```
q      % Implicit input, Subtract 1
Zq     % Vector of primes up to that
0)     % Get last one
G      % Push input again
_Yq    % Next prime
+      % Add
G      % Push input
E      % Multiply by 2
>      % Greater than? Implicit display
```
[Answer]
# GNU APL 1.2, 78 bytes
```
∇f N
X←(R←(~R∊R∘.×R)/R←1↓⍳N×2)⍳N
(|R[X-1]-N)<|R[X+1]-N
∇
```
`∇f N` declares a function that takes an argument.
`(~R∊R∘.×R)/R←1↓⍳N×2` gives a list of all the primes from 2 to twice the argument. I'm assuming that the next prime is less than twice the original. If this is untrue, `N*2` gives N squared and takes the same number of bytes (hopefully that's big enough to exceed the next prime). [(See Wikipedia's explanation for how the prime-finding works)](https://en.wikipedia.org/wiki/APL_(programming_language)#Prime_numbers)
`X←(R←(...))⍳N` assigns that list to vector `R` (overwriting its previous contents), finds the index of the original prime `N` in that list, and then assigns that index to `X`.
`|R[X-1]-N` computes the difference between the previous prime (because `R` contains the primes, the `X-1`th element is the prime before `N`) and `N` and then takes the absolute value (APL operates right-to-left).
`|R[X+1]-N` does the same, but for the next prime.
`(|R[X-1]-N)<|R[X+1]-N` prints 1 if the previous prime is closer to the original than the next prime and 0 otherwise. Parentheses are needed for precedence.
`∇` ends the function.
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 9 bytes
```
Æp;;ÆnI</
```
[Try it online!](https://tio.run/##y0rNyan8//9wW4G19eG2PE8b/f///5sbAwA "Jelly – Try It Online")
[Answer]
# Pyth, 15 bytes
```
>-fP_ThQfPT_tQy
```
[Try it here.](http://pyth.herokuapp.com/?code=%3E-fP_ThQfPT_tQy&test_suite=1&test_suite_input=73&debug=0)
Uses Pietu1998's algorithm.
[Answer]
# [Perl 6](http://perl6.org/), 41 bytes
```
{[>] map ->\n{$_+n,*+n...&is-prime},1,-1}
```
[Try it online!](https://tio.run/##NclLCoAgFAXQrdxBSB999IEmUhupEAcWQYrYKMS1GwTNDhxvwjVm@4DtmHJc5g1We4h5dbFQjeN144iInbfw4bQm8Y6LLmWJWz84gvGI@LMsVAXGsH9IHANR37Yyvw "Perl 6 – Try It Online")
`$_` is the argument to the function. The mapping function `-> \n { $_ + n, * + n ... &is-prime }` takes a number `n` and returns a sequence of numbers `$_ + n, $_ + 2*n, ...` that ends when it reaches a prime number. Mapping this function over the two numbers `1` and `-1` produces a sequence of two sequences; the first starts with `$_ + 1` and ends with the first prime number greater than `$_`, and the second starts with `$_ - 1` and ends with the first prime number less than `$_`. `[>]` reduces this two-element list with the greater-than operator, returning true if the first sequence is greater (ie, longer) than the second.
[Answer]
## Python 2.7 - 120 bytes
```
from math import*
i=lambda x:factorial(x-1)%x==x-1
def f(n,c):return 1 if i(n-c)>i(n+c) else 0 if i(n+c)>0 else f(n,c+1)
```
Since python doesn't have a built-in is prime function, we can use Wilson's theorem to get a nice short prime checker. Wilson's theorem states that a number is prime if and only if (n-1)! is congruent to -1 mod(n). Hence the function i will return 1 if the number is prime and 0 if it's not. Following that the f function will determine if the next prime from that number occurs first when incremented down rather than incremented up. If neither of the incremented numbers prime, it's just recursively called again.
Some example I/O
```
f(3,1)
1
f(15,1)
0
```
[Answer]
# [Python 2](https://docs.python.org/2/), ~~122~~ ~~108~~ ~~103~~ ~~94~~ 92 bytes
```
def a(n):
 r=[2];x=2
 while r[-1]<=n:x+=1;r+=[x]*all(x%i for i in r)
 return sum(r[-3:])>3*n
```
[Try it online!](https://tio.run/##jZLNattQEIX3foqBUrATFaw7c/XjVH2CLrsTWhiqYFFHDrJD3Kd355tAKGSThefOj@fcc47u89/L4TSn2@33@Cj79bzZrWTp@jQ8XLu0ktfDdBxl6b@Vw/du3l3vu/Jhue/663C3Px7X16@TPJ4WmWSaZdn46nh5WWY5vzytfUl3w@aH3s2352V6Gs8/x/P512E/V3krnfSpEC0kF1IXUpb@86okbwtJnic/1fvqPfPTvGeeZ7Z8Vnmv8rr2s/Ze7b3Gz8bPFpwtoFtQt1G23EOZKDWujIyBETK9TK/if6CXddBi2jBtI9CLO1rIwj4FZZBTCgGUICe4pxyBKcipYgDxVNMDOTWh2YNCVwFV6CqmaDgBXbUImIMTihUKU4WpwlSBUnxQSBpGGPINby2FmRGwFJIW5gJqkDS8NZgayAaoNWTIN0RnjM2AZvAy8jNSc3wjUDIAGakZqRmpGZTcxifkG0KtQm@F1KqMjAH8KovAwOphtfJnNF@k/@/NfXxYE8942gzyRV7H/Z@3nc9uzqfL@zZ5INz@AQ "Python 2 – Try It Online")
Uses Pietu's idea... and then saved 28 bytes by golfing shorter prime list iterators; then 2 more by replacing `-3*n>0` with `>3*n` (d'oh!) 
[Answer]
# Regex (most flavors), 47 bytes
```
^(?=(x*)(?!(x+)(\2\2x)+$)\1)x+(?!(xx+)\4+$)\1\1
```
[Try it online!](https://tio.run/##TY3BTsJAFEX37yuAmPAeleK0haHUgZULNix0aTWZwFBGh2kzHaEifjtWEhMXN7k55yb3TR5kvXa68sO60hvl9qV9V58XJ6w6dh5V8dBUiCcxHw0ur7gQ2AwIF11sAsI8yqOGghvKGTXBlbY4T64kZ5fB6EShL5@807ZACmuj1wont8OEKDvutFGIRjglN0ZbhURdYT@Moa9CmLCujPbYH/Yp01tEK4rQKFv4Hc2j81nXK7lCLSrparW0HovnuxeiP6H@CztnCzb71eR3rjz2lvYgjd50nLSFmnV6gcm2pcNM3wuV6SCg9rDX9EKnKiU9agr30q936IjWpa1Lo0JTFi3Pvi8RxDAGDowBi4G1JYUohiiFmEHMIWGQxJBwGLe7FCYMJhw4Ax4DT2EawzSFlEOasjb8Bw)
Takes input in unary. Outputs a match for weak primes, no match for non-weak primes. Works in ECMAScript, Perl, PCRE, Python, Ruby.
Explanation:
Let N be the input, A the closest prime < N, and B the closest prime > N. The main difficulty of a regex approach to this challenge is that we can’t represent numbers greater than the input, like B. Instead, we find the smallest b such that 2b + 1 is prime and 2b + 1 > N, which ensures 2b + 1 = B.
```
(?=
  (x*)              # \1 = N - b, tail = b
  (?!(x+)(\2\2x)+$) # Assert 2b + 1 is prime
  \1                # Assert b ≥ \1 (and thus 2b + 1 > N)
)
```
Then, note that we don’t actually need to find A. As long as *any* prime < N is closer to N than B, N is a weak prime.
```
x+                  # tail iterates over integers < N
(?!(xx+)\4+$)       # assert tail is prime
\1\1                # assert tail ≥ 2 * \1 (and thus tail + B > 2N)
```
[Answer]
# Octave, 53 bytes
```
@(n)diff(list_primes(h=nnz(primes(n))+1)(h-2:h),2)>0
```
[Try it online!](https://tio.run/##y08uSSxL/f/fQSNPMyUzLU0jJ7O4JL6gKDM3tVgjwzYvr0oDysnT1NQ21NTI0DWyytDUMdK0M/ifZpuYV2zNlaZhaKgJIo3BpDmYtNT8DwA "Octave – Try It Online")
[Answer]
# JavaScript ES6, ~~162~~ 154 bytes
8 bytes save based on *Jörg Hülsermann*'s trick "print nothing in one case". No need to `?"Y":"N"` after `one<two`
```
var isWeak=
a=>{p=[2];i=0;f=d=>{j=p[i];l:while(j++){for(x=0;p[x]*p[x]<=j;x++){if(j%p[x]==0){continue l}}return p[++i]=j}};while(p[i]<a+1){f()};return a*2<p[i]+p[i-2]}
[43,//true
53,//false
7901,//false
7907,//true
1299853,//true
1299869//false
].forEach(n=>{console.log(n,isWeak(n))})
```
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E), 17 bytes
```
[<Dp#]¹[>Dp#]+¹·›
```
[Try it online!](https://tio.run/##MzBNTDJM/f8/2salQDn20M5oOxCtfWjnoe2PGnb9/29uDAA "05AB1E – Try It Online")
Uses Pietu1998's algorithm.
[Answer]
# [Python 3](https://docs.python.org/3/), 149 bytes
```
f=lambda n,k=1,P=1:n*[0]and P%k*[k]+f(n-P%k,k+1,P*k*k)
def a(n):p=f(n);return p.index(n)in filter(lambda i:p[i]-p[i-1]<p[i+1]-p[i],range(1,len(p)-1))
```
[Try it online!](https://tio.run/##fZHLboMwEEX3@QoUqZLNI@rYBie0/EP2iIUroLVMJghRqVWUb6eeqstONoA5vneO5fl7/bii3raxmdzlrXcJ5qGB/NxAjWn73Dnsk/NTSNvQZaPAIn7nIYsb0pAGueuHMXECZT03kcqXZVg/F0zmg8d@@Ip/PCajn9ZhEX/9vp5b3xXxUUD3Gl8Z/K66fHH4PgjIpwHFLAuQcpsXj6vY3@717b4/jNfl4lahcieUlHL3L9WRapaWkZYstZFalgJEDMBzGg38bKB6eNB/In5iuaJ@xfcryis@r8lf8/6a/DTvZyhv@LwhP8P7Geo3fH9J@fLB3dH5Sv58FflVvF9F8yt@vqW85fOW/CzvZ8nP8n5Hyh8pv/0A "Python 3 – Try It Online")
I'm using a prime generating function (top line) taken from [this old stack exchange answer.](https://codegolf.stackexchange.com/a/70027/70097)
[Answer]
# JavaScript, 98 bytes
```
let test = _=>(o.innerHTML=f(+prime.value))
let f= 
n=>{P=n=>{for(i=n,p=1;--i>1;)p=p&&n%i};a=b=n;for(p=0;!p;P(--a));for(p=0;!p;P(++b));return n-a<b-n}
```
```
Enter Prime: <input id="prime">
<button type="button" onclick="test()">test if weak</button>
<pre id="o"></pre>
```
**Less Golphed**
```
n=>{
   P=  // is a Prime greater than 1, result in p
       n=>{
           for(i=n,p=1;--i>1;)
               p=p&&n%i
       };
   a=b=n; // initialize lower and upper primes to n
   for(p=0;!p;P(--a)); // find lower,
   for(p=0;!p;P(++b)); // find upper,
   return n-a<b-n // is weak result
}
```
Note the test code does *not* check the input "prime" is actually a prime.
[Answer]
# [braingasm](https://github.com/daniero/braingasm), ~~23~~ 22 bytes
Prints `1` for weak primes and `0` for not weak.
```
;>0$+L[->+>2[>q[#:Q]]]
```
Walkthrough:
```
;                       Read a number to cell 0
 >0$+                   Go to cell 1 and copy the value of cell 0
     L                  Make the tape wrap around after cell 1
      [              ]  Loop:
       ->+>               Decrease cell 1 and increase cell 0
           2[       ]     Twice do:
             >              Go to the other cell
              q[   ]        If it's prime:
                #:Q         Print the current cell number and quit
```
[Answer]
# Julia 0.6, 64 bytes
```
g(x,i)=0∉x%(2:x-1)?1:1+g(x+i,i);x->g(x,1)&(g(x-1,-1)<g(x+1,1))
```
[Answer]
# [Python 2](https://docs.python.org/2/), 81 bytes
```
n=input()
a=b=c=i=2;p=1
while b<n:
 p*=i;i+=1
 if p*p%i:a,b,c=b,c,i
print a+c>2*b
```
[Try it online!](https://tio.run/##DchBCoAgEEbhvadwE5S5yRZBNt1FpeiHmIYwotObi7f4nnz5uNiVwgSWJ7edChQpEch5oUG9B85Nx4VnpcUQPPp6NfYqaTAHG22imoWSG5x16NPqTCxlGn8 "Python 2 – Try It Online")
Uses Wilson's theorem for the primality test.
[Answer]
# [Husk](https://github.com/barbuz/Husk), 15 bytes
```
>D¹Σ§e₁←₁→
ḟṗt¡
```
[Try it online!](https://tio.run/##yygtzv7/387l0M5ziw8tT33U1PiobQKYnMT1cMf8hzunlxxa@P//f2MA "Husk – Try It Online")
]