Advertisement
Guest User

Sito Eratostenesa

a guest
Dec 5th, 2018
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.65 KB | None | 0 0
  1. Tablicowa wersja sita Eratostenesa
  2.  
  3. #include<iostream>
  4. #include<cstdlib>
  5.  
  6. using namespace std;
  7.  
  8. int main(int argc,char **argv)
  9. {
  10. long int counter,i,j,n;
  11. bool *p;
  12. n = atol(argv[1]);
  13. p = new bool[n+1];
  14. p[0] = false;
  15. p[1] = false;
  16. counter = 0;
  17. for(i = 2;i <= n;i++)
  18. p[i]=true;
  19. i = 2;
  20. while(i <= n)
  21. {
  22. if(p[i])
  23. {
  24. j = i + i;
  25. while(j <= n)
  26. {
  27. p[j] = false;
  28. j+=i;
  29. }
  30. }
  31. i++;
  32. }
  33. for(i = 2; i <= n;i++)
  34. if(p[i])
  35. {
  36. cout << i << " -> ";
  37. counter++;
  38. }
  39. cout << '\n';
  40. cout << " Liczba liczb pierwszych :" << counter <<"\n";
  41. delete[] p;
  42. return 0;
  43. }
  44.  
  45.  
  46. Listowa wersja sita Eratostenesa
  47.  
  48. #include<iostream>
  49. #include<cstdlib>
  50.  
  51. using namespace std;
  52.  
  53. typedef long int TData;
  54.  
  55. struct node
  56. {
  57. TData data;
  58. struct node *next;
  59. };
  60.  
  61. typedef struct node node;
  62.  
  63. void ListInit(node *&head)
  64. {
  65. head = nullptr;
  66. }
  67.  
  68. bool ListIsEmpty(node *head)
  69. {
  70. return head == nullptr;
  71. }
  72.  
  73. void ListPrint(node *head)
  74. {
  75. node *p = head;
  76. long int counter = 0;
  77. while(p != nullptr)
  78. {
  79. cout << p->data << " -> ";
  80. counter++;
  81. p = p->next;
  82. }
  83. cout<<"NULL \n";
  84. cout<<"Liczba wezlow listy : "<< counter<<"\n";
  85. }
  86.  
  87. node *ListSearchNode(node *head,TData k,node *&prev)
  88. {
  89. node *curr = head;
  90. prev = nullptr;
  91. while(curr != nullptr && curr->data != k)
  92. {
  93. prev = curr;
  94. curr = curr->next;
  95. }
  96. return curr;
  97. }
  98.  
  99. void ListPush(node *&head,TData k)
  100. {
  101. node *x = new node;
  102. x->data = k;
  103. x->next = head;
  104. head = x;
  105. }
  106.  
  107. void ListPop(node *&head)
  108. {
  109. node *x;
  110. if(!ListIsEmpty(head))
  111. {
  112. x = head;
  113. head = x->next;
  114. delete x;
  115. }
  116. }
  117.  
  118. void ListDeleteNode(node *&head,TData k)
  119. {
  120. node *x,*y;
  121. x = ListSearchNode(head,k,y);
  122. if(x != nullptr)
  123. {
  124. if(head == x)
  125. head = x->next;
  126. else
  127. y->next = x->next;
  128. delete x;
  129. }
  130. }
  131.  
  132. int main(int argc, char**argv)
  133. {
  134. long int j,n;
  135. node *aux;
  136. node *head;
  137. ListInit(head);
  138. n=atol(argv[1]);
  139. for(j = n;j>=2;j--)
  140. ListPush(head,j);
  141. aux = head;
  142. while(aux != nullptr)
  143. {
  144. j = aux->data+aux->data;
  145. while(j <= n)
  146. {
  147. ListDeleteNode(head,j);
  148. j+=aux->data;
  149. }
  150. aux = aux->next;
  151. }
  152. ListPrint(head);
  153. while(!ListIsEmpty(head))
  154. ListPop(head);
  155. return 0;
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement