Guest User

Untitled

a guest
Mar 22nd, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.87 KB | None | 0 0
  1. import java.util.LinkedList;
  2. import java.util.concurrent.LinkedBlockingDeque;
  3.  
  4. public class Primes {
  5. private long timeout = System.nanoTime();
  6. LinkedList<Integer> lprimes = new LinkedList<Integer>();
  7. LinkedList<Integer> lnums = new LinkedList<Integer>();
  8. public long getTimeout() {
  9. return timeout;
  10. }
  11.  
  12. public void setTimeout(long timeout) {
  13. this.timeout = timeout;
  14. }
  15.  
  16. public int getCountPrimes(){
  17. return this.lprimes.size();
  18. }
  19. public void timeRun() {
  20. System.out.println(System.nanoTime());
  21. System.out.println(timeout);
  22. System.out.println("Время выполнения равно " + String.valueOf(System.nanoTime() - this.timeout)+" ns или " + String.valueOf((System.nanoTime() - this.timeout)/1000000));
  23. }
  24.  
  25. public void keepPrimes() {
  26. lnums.add(2);
  27. for(int i = 3;i <= 10000;i += 2){
  28. lnums.add(i);
  29. }
  30. /*for (int i = 2; i <= 10000; i++) {
  31. lnums.add(i);
  32. }*/
  33. while(lnums.size()>0){
  34. int nextPrime = lnums.remove();
  35. for (int i = nextPrime*nextPrime; i <=10000; i+=nextPrime) {
  36. lnums.removeFirstOccurrence(i);
  37. }
  38. lprimes.add(nextPrime);
  39. System.out.println(nextPrime);
  40. }
  41. }
  42.  
  43. public static void main(String[] args) {
  44. // TODO Auto-generated method stub
  45. System.out.println("Максимальное значение INT: "+Integer.MAX_VALUE);
  46. Primes primes = new Primes();
  47. primes.keepPrimes();
  48. System.out.println("Формирование коллекции закончено!");
  49. primes.timeRun();
  50. System.out.println(primes.getCountPrimes());
  51. }
  52.  
  53. #include <stdlib.h>
  54. #include <stdio.h>
  55. #include <stdint.h>
  56. #define lim INT32_MAX
  57. #define BITS 32
  58. int main()
  59. {
  60. uint32_t *x= calloc(sizeof*x, ((unsigned)lim-3+BITS*2-1)/(BITS*2));
  61. fputs("2n", stdout);
  62. for(unsigned i=3;i<=lim;i+=2) {
  63. unsigned b= i-3 >> 1;
  64. if(x[b/BITS] & 1<<b%BITS) continue;
  65. printf("%dn", i);
  66. while( (b+=i) <= (lim-3>>1) ) x[b/BITS] |= 1<<b%BITS;
  67. }
  68. }
  69.  
  70. #include <stdlib.h>
  71. #include <stdio.h>
  72. #include <stdint.h>
  73. #define lim INT32_MAX
  74. #define BITS 32
  75. int main()
  76. {
  77. uint32_t *x= calloc(sizeof*x, ((unsigned)lim-5+BITS*3-3)/(BITS*3));
  78. fputs("2n3n", stdout);
  79. for(unsigned i=5, b0=0; i<=lim; i+=6, b0++) {
  80. unsigned b=b0;
  81. if(!( x[b/(BITS/2)] & 1<<b%(BITS/2)*2 )) {
  82. printf("%dn", i);
  83. while( (b+=i) <= (lim-5)/6 ) x[b/(BITS/2)] |= 1<<b%(BITS/2)*2;
  84. for(b=b0+2*i/3; b <= (lim-5)/6; b+=i) x[b/(BITS/2)] |= 2<<b%(BITS/2)*2;
  85. }
  86. b=b0;
  87. if(!( x[b/(BITS/2)] & 2<<b%(BITS/2)*2 )) {
  88. printf("%dn", i+2);
  89. while( (b+=i+2) <= (lim-5)/6 ) x[b/(BITS/2)] |= 2<<b%(BITS/2)*2;
  90. for(b=b0+2*i/3+2; b <= (lim-5)/6; b+=i+2) x[b/(BITS/2)] |= 1<<b%(BITS/2)*2;
  91. }
  92. }
  93. }
  94.  
  95. #include <stdlib.h>
  96. #include <stdio.h>
  97. #include <string.h>
  98.  
  99. constexpr inline unsigned long pow2(unsigned long i) { return 1 << i; }
  100.  
  101. unsigned long isqrt(unsigned long a)
  102. {
  103. unsigned long x = a;
  104. for(unsigned long z = 0; x != z; )
  105. {
  106. z = x;
  107. x = (x + a/x)/2;
  108. }
  109. return x;
  110. }
  111.  
  112. constexpr unsigned long MAX_LIM = pow2(31) - 1;
  113. constexpr unsigned long ARR_LIM = (MAX_LIM >> 6) + 1;
  114. const unsigned long SQR_LIM = isqrt(MAX_LIM);;
  115.  
  116. unsigned long primes[ARR_LIM] = { 0 }; // 0 - простое, 1 - составное
  117.  
  118. auto set_primes = [](unsigned long idx)
  119. { primes[idx >> 6] |= pow2((idx&0x0000003F)>>1); };
  120. auto get_primes = [](unsigned long idx)
  121. { return primes[idx >> 6] & pow2((idx&0x0000003F)>>1); };
  122.  
  123. int main(int argc, const char * argv[])
  124. {
  125. for(unsigned long i = 3; i <= SQR_LIM; i += 2)
  126. {
  127. if (get_primes(i)) continue;
  128. for(unsigned long j = i * i; j <= MAX_LIM; j += 2*i)
  129. {
  130. set_primes(j);
  131. }
  132. }
  133.  
  134. puts("2");
  135. for(unsigned long i = 3; i <= MAX_LIM; i+=2)
  136. {
  137. if (get_primes(i)) continue;
  138. printf("%lun",i);
  139. }
  140. }
Add Comment
Please, Sign In to add comment