Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Challenges

Prime Difference

+5
−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.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

1 comment thread

General comments (1 comment)

8 answers

+3
−0

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.
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

1 comment thread

Wow, never thought I'd actually see toadj_ used somewhere. +1 (1 comment)
+3
−0

Dyalog APL Extended, 14 bytes

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

Try it online!

{¯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
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+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
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+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...

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+0
−0

Japt, 15 14 bytes

@§XnÈj}aX+j}a2

Try it or run all test cases

@§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
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+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
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+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!

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+0
−0

Ruby, 56 bytes

->n{require'prime';Prime.each_cons(2).find{_2-_1>=n}[0]}

Attempt This Online!

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

Sign up to answer this question »