Post History
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...
Answer
#2: Post edited
# 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
# 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...