Advertisement
BlueBear

sieveoferathosthenes

Sep 30th, 2013
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.26 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.            
  4. #define LIMIT 1300000
  5.  
  6. typedef struct prime
  7. {
  8.     int no;
  9.     unsigned long long int number;
  10.     struct prime *next;
  11. }t_primes;
  12.  
  13. void sieveOfErathosthenes(unsigned int count)
  14. {
  15.     int *primes = NULL;
  16.     t_primes *prim = NULL;
  17.     t_primes *head = NULL;
  18.     unsigned long long int i = 0;
  19.     unsigned long long int j = 0;
  20.     int z = 1;
  21.  
  22.     if(count >=1 && count <= 100000)
  23.     {
  24.         primes = (int *)malloc(sizeof(int)* LIMIT);
  25.         prim = (t_primes *)malloc(sizeof(t_primes));
  26.         head = prim;
  27.  
  28.         for (i=2 ; i < LIMIT; i++)
  29.         {
  30.             primes[i]=1;
  31.         }
  32.        
  33.         for (i=2;i<LIMIT;i++)
  34.         {
  35.             if (primes[i])
  36.             {
  37.  
  38.                 prim->no = z++;
  39.                 prim->number = i;
  40.                 prim->next = (t_primes *)malloc(sizeof(t_primes));
  41.                 prim = prim->next;
  42.  
  43.                 for (j=i;i*j<LIMIT;j++)
  44.                 {
  45.                     primes[i*j]=0;
  46.                 }
  47.             }
  48.         }
  49.         prim = head;
  50.         while(prim->no != count)
  51.         {
  52.             prim = prim->next;
  53.         }
  54.  
  55.         printf("\t%d\n", prim->number);
  56.  
  57.         /*for (i=2;i<LIMIT;i++)
  58.         {
  59.  
  60.             if (primes[i] == 1)
  61.             {
  62.                 if(z == count)
  63.                 {
  64.                     printf("%d\n",i);
  65.                     break;
  66.                 }
  67.                 z++;
  68.             }
  69.            
  70.         }*/
  71.     }
  72.     else
  73.     {
  74.         exit(0);
  75.     }
  76. }
  77.  
  78. int main(void)
  79. {
  80.  
  81.     int count = 1;
  82.  
  83.     while (scanf("%d", &count) > 0)
  84.     {
  85.         sieveOfErathosthenes(count);
  86.     }
  87.  
  88.     return 0;
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement