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

Post History

50%
+0 −0
Challenges Prime Difference

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

posted 4y ago by Lundin‭  ·  edited 4y ago by Lundin‭

Answer
#2: Post edited by user avatar Lundin‭ · 2020-12-11T11:20:09Z (about 4 years ago)
Got rid of break
  • # C (gcc), 129 bytes
  • ```c
  • 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;if(i-P>=n)break;P=i;}e:return P;}
  • ```
  • <a href="https://tio.run/##FY1BCsIwEEX3nmIQhIyNoK7UcTxCyb52oTWViTSGWN2Unr1OZ/Nh3ue/ZvNsmmkq@ahHrYk4SOwhVWVtxQbrqPPdx/cm2Z0tkdp3NsKO9yRrOZckRYHSmlRJjcMMAyugoCgULJiqUPOWtCIbd@GI9@xvL3IsNPpT9v03R3A0TrO1u0k0v7c8EIYFgM6Bmf8CDDvSODMcNNUJygFSVtya5epxjUsLKkGkxTj9AQ" title="C (gcc) – Try It Online">Try it online!</a>
  • 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...
  • # C (gcc), 126 <strike>129</strike> bytes
  • ```c
  • 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;}
  • ```
  • <a href="https://tio.run/##Fc3BCsIwDAbgu08RBCF1FdSTLkbfoPQ@d5C5SgqrZU4vY88@s1x@@D/I3@xeTTPPjs96FDCZUdIAuXK1FRutp67tPu2A2R6sMxTePQp7PpJs5eJIisJIwFxJbcYFIytQVIoFi8lVrHlPsvNXTrfIrkSvNU1t2bfDt0/gaZqXye4hCX9veRoYVwD6C3DpBRgOpHFhOGnqIKgD5F454HrzvKe1hYBiDK2m@Q8" title="C (gcc) – Try It Online">Try it online!</a>
  • 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: Initial revision by user avatar Lundin‭ · 2020-12-11T11:13:05Z (about 4 years ago)
# C (gcc), 129 bytes

```c
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;if(i-P>=n)break;P=i;}e:return P;}
```

<a href="https://tio.run/##FY1BCsIwEEX3nmIQhIyNoK7UcTxCyb52oTWViTSGWN2Unr1OZ/Nh3ue/ZvNsmmkq@ahHrYk4SOwhVWVtxQbrqPPdx/cm2Z0tkdp3NsKO9yRrOZckRYHSmlRJjcMMAyugoCgULJiqUPOWtCIbd@GI9@xvL3IsNPpT9v03R3A0TrO1u0k0v7c8EIYFgM6Bmf8CDDvSODMcNNUJygFSVtya5epxjUsLKkGkxTj9AQ" title="C (gcc) – Try It Online">Try it online!</a>

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