Challenges

# Prime Difference

+4
−0

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.

Why does this post require moderator attention?
Why should this post be closed?

+3
−0

# Dyalog APL Extended, 14 bytes

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


Try it online!

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

Why does this post require moderator attention?

+3
−0

# Husk, 8 bytes

Ψḟo≥⁰≠İp


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.
Why does this post require moderator attention?

+2
−0

# Raku, 33 bytes

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


Try it online!

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

Why does this post require moderator attention?

+0
−0

# 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


Try it online!

Why does this post require moderator attention?

+0
−0

# 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;}


Try it online!

This is an integer input/output function solution. The upper limit of prime number supported is the square root ofN, 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...

Why does this post require moderator attention?

+0
−0

# 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


Try it online!

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

Why does this post require moderator attention?

+0
−0

# Japt, 15 14 bytes

@§XnÈj}aX+j}a2

@§XnÈj}aX+j}a2     :Implicit input of integer U
@                  :Function taking an integer X as argument
§                 :  Is U <= ...
Xn               :  Subtract X from
È              :    Function taking an integer as input
j             :      Is prime?
}            :    End function
a           :    Get the first integer that returns true when passed through that function starting at
X+j        :      X plus is X prime
}       :End function
a2     :Get the first integer that returns true when passed through that function starting at 2
Why does this post require moderator attention?