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

Dashboard
Notifications
Mark all as read
Challenges

Prime Difference

+3
−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?
You might want to add some details to your flag.
Why should this post be closed?

1 comment

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

6 answers

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

0 comments

+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?
You might want to add some details to your flag.

0 comments

+2
−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.
Why does this post require moderator attention?
You might want to add some details to your flag.

0 comments

+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?
You might want to add some details to your flag.

0 comments

+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?
You might want to add some details to your flag.

0 comments

+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?
You might want to add some details to your flag.

0 comments

Sign up to answer this question »

This community is part of the Codidact network. We have other communities too — take a look!

You can also join us in chat!

Want to advertise this community? Use our templates!