Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #define LIMIT 1300000
- typedef struct prime
- {
- int no;
- unsigned long long int number;
- struct prime *next;
- }t_primes;
- void sieveOfErathosthenes(unsigned int count)
- {
- int *primes = NULL;
- t_primes *prim = NULL;
- t_primes *head = NULL;
- unsigned long long int i = 0;
- unsigned long long int j = 0;
- int z = 1;
- if(count >=1 && count <= 100000)
- {
- primes = (int *)malloc(sizeof(int)* LIMIT);
- prim = (t_primes *)malloc(sizeof(t_primes));
- head = prim;
- for (i=2 ; i < LIMIT; i++)
- {
- primes[i]=1;
- }
- for (i=2;i<LIMIT;i++)
- {
- if (primes[i])
- {
- prim->no = z++;
- prim->number = i;
- prim->next = (t_primes *)malloc(sizeof(t_primes));
- prim = prim->next;
- for (j=i;i*j<LIMIT;j++)
- {
- primes[i*j]=0;
- }
- }
- }
- prim = head;
- while(prim->no != count)
- {
- prim = prim->next;
- }
- printf("\t%d\n", prim->number);
- /*for (i=2;i<LIMIT;i++)
- {
- if (primes[i] == 1)
- {
- if(z == count)
- {
- printf("%d\n",i);
- break;
- }
- z++;
- }
- }*/
- }
- else
- {
- exit(0);
- }
- }
- int main(void)
- {
- int count = 1;
- while (scanf("%d", &count) > 0)
- {
- sieveOfErathosthenes(count);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement