# Prime Difference

Given an integer *n*, output the smallest prime such that the difference between it and the next prime is at least *n*.

For example, if `n=5`

, you would output `23`

, since the next prime is `29`

, and `29-23>=5`

.

# More Input/Output Examples

```
1 -> 2 (3 - 2 >= 1)
2 -> 3 (5 - 3 >= 2)
3 -> 7 (11 - 7 >= 3)
4 -> 7
5 -> 23
6 -> 23
7 -> 89
8 -> 89
```

This is code-golf, so shortest code wins.

[Dyalog APL Extended], 14 byte …

5mo ago

[Raku], 33 bytes {1 …

5mo ago

[Husk], 8 bytes Ψḟo≥⁰≠İ …

5mo ago

C (gcc), 126 129 bytes ```c …

4mo ago

[JavaScript (Node.js)], 81 byt …

4mo ago

[JavaScript (Node.js)], 86 byt …

5mo ago

## 6 answers

# Dyalog APL Extended, 14 bytes

```
{¯4⍭4⍭⍣(⍵≤-)2}
```

```
{¯4⍭4⍭⍣(⍵≤-)2} Monadic dfn
2 start with 2
⍣ Repeat
4⍭ the function "next prime"
(⍵≤-) until the difference from the previous one is ≥ the input
¯4⍭ previous prime of that
```

#### 0 comments

# Raku, 33 bytes

```
{1+(3...{($^a...&is-prime)>=$_})}
```

Anonymous code block that takes a number and returns a number.

## Explanation

```
{ } # Anonymous code block
(3...{ }) # Increment from 3 until
( )>=$_ # The input is less than or equal to
$^a...&is-prime # The difference between the current number and the next prime plus 1
1+ # And add one to the length of this list
```

#### 0 comments

# Husk, 8 bytes

```
Ψḟo≥⁰≠İp
```

Try it online! or Verify first 8 values

It is always a good day when you get to use `Ψ`

in your program.

## Explanation

```
Ψḟo≥⁰≠İp
İp to the infinite list of prime numbers,
Ψ apply this higher order function on overlapping pairs
ḟo first element where
≠ absolute difference
≥⁰ is greater than or equal to the input.
```

#### 0 comments

# JavaScript (Node.js), 86 bytes

```
f=(a,b=2,c=(a,b=2)=>a-b?a%b&&c(a,b+1):1,d=a=>c(++a)?a:d(a))=>!c(b)|d(b)-b<a?f(a,b+1):b
```

#### 0 comments

# JavaScript (Node.js), 81 bytes

```
d=(p,i=2)=>i<p?!(p%i)||d(p,i+1):0
f=(n,a=2,p=a+1)=>d(p)?f(n,a,p+1):p-a<n?f(n,p):a
```

Explanation:

`d`

is a helper function that returns true if `p`

is not prime.

```
f=(n, a=2, p=a+1) =>
d(p)?f(n, a, p + 1) // Recurse until p is prime
// p starts at a+1 so it gets the prime after a
:p-a<n?f(n, p):a // If a, p doesn't work, we already computed the next prime so it's easy to recurse
```

#### 0 comments

# C (gcc), 126 ~~129~~ bytes

```
N=9999;f(n){int p[N],i,j,P;memset(p,1,N);for(i=P=2;i*i<N;i++)if(p[i]){for(j=i*i;j<N;j+=i)p[j]=0;i-P>=n?j=N:(P=i);}e:return P;}
```

This is an integer input/output function solution. The upper limit of prime number supported is the square root of`N`

, so currently it counts prime numbers up to 99 and prints nonsense if needed to go beyond that, but it can be extended up to `sqrt(INT_MAX)`

long as the stack can handle the VLA `p`

.

I'm still a rookie at this, quite likely the algorithm itself (Sieve of Eratosthenes) is naive for code golfing purposes. I'm also quite sure that this could be rewritten with recursion somehow to shave off a bit of loop syntax overhead...

## 1 comment

I thought this was a nice, simple challenge to start things off — Quintec 5 months ago