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.
[Husk], 8 bytes Ψḟo≥⁰≠İ …
4y ago
[Dyalog APL Extended], 14 byte …
4y ago
[Raku], 33 bytes {1 …
4y ago
Ruby, 56 bytes ```ruby ->n …
3y ago
Japt, 15 14 bytes @§XnÈ …
3y ago
C (gcc), 126 129 bytes ```c …
4y ago
[JavaScript (Node.js)], 81 byt …
4y ago
[JavaScript (Node.js)], 86 byt …
4y ago
8 answers
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.
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 comment threads
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 comment threads
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 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...
0 comment threads
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
0 comment threads
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 comment threads
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
1 comment thread