What is the shortest C code to "Calculate the n-th prime"?
Shortest in terms of significant characters, i.e. the number of semicolons, non-whitespace characters, keywords and commas.
Input:
Integers n from the standard input, separated by new lines. The input will be terminated by EOF.
Output:
Right after the input n, print the n-th prime to the standard output separated by new lines.
(You may assume the prime numbers are < 10,000, i.e. n < 1,230.)
Test cases:
Input:
1
2
4
8
32
999
42
5
Output:
2
3
7
19
131
7907
181
11
My attempt :
#define m 10000
a[m],b[m],x;
main(i,j){
for(i=2;i<m;i++)
{
if (!a[i])
for (b[++x]=i,j=2*i;j<m;j+=i)
a[j]=1;
}
for(;~scanf("%d",&i);printf("%d\n",b[i]));
}
For this problem readability is not a concern.A costlier code in terms of time and memory but satisfying the constraint will be considered better here.