Advertisement
palmerstone

Sieve

Jun 23rd, 2011
471
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. //
  2. // This program is free software; you can redistribute it and/or modify
  3. // it under the terms of the GNU General Public License as published by
  4. // the Free Software Foundation; either version 2 of the License, or
  5. // (at your option) any later version.
  6. //
  7. // This program is distributed in the hope that it will be useful,
  8. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. // GNU General Public License for more details.
  11. //
  12. // You should have received a copy of the GNU General Public License
  13. // along with this program; if not, write to the Free Software
  14. // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  15. // MA 02110-1301, USA.
  16.  
  17. #include <stdio.h>
  18. //#include <math.h>
  19.  
  20. int main()
  21. {
  22. int i, t, n, m, r, x, z, a, b, c, d, count, flag, y, l, j, k;
  23. int ar[100000];
  24.  
  25. ar[0] = 2, ar[1] = 3, ar[2] = 5, ar[3] = 7, ar[4] = 11;
  26. for (j = 5; j < 100000; j++)
  27. {
  28. l = j - 1;
  29.  
  30. for (k = ar[j - 1] + 2; ; k = k + 2)
  31. {
  32. if ( (k % 5 != 0) && (k % 3 != 0) && (k % 7 != 0) )
  33. {
  34. y = k / 2;
  35. for (a = 4; a < l; a++)
  36. {
  37. if (ar[a] > y)
  38. {
  39. a = l;
  40. break;
  41. }
  42. if (k % ar[a] == 0)
  43. {
  44. break;
  45. }
  46. else if (a > 1140)
  47. {
  48. a = l;
  49. break;
  50. }
  51. }
  52. if (a == l)
  53. {
  54. ar[j] = k;
  55. break;
  56. }
  57. }
  58. }
  59. }
  60.  
  61. scanf("%d", &t);
  62. for (i = 1; i <= t; i++)
  63. {
  64. scanf("%d", &n);
  65. printf("prime[%d] = %d\n", n, ar[n - 1]);
  66. }
  67. //printf("Hello World\n");
  68.  
  69. return 0;
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement