Advertisement
Guest User

Untitled

a guest
Nov 21st, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.71 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <time.h>
  3. #define MINNUM 3990000000
  4. #define MAXNUM 4010000000
  5.  
  6. unsigned long modular_pow (unsigned long base,unsigned long exponent,unsigned long modulus)
  7. {
  8. unsigned long result;
  9.  
  10. if (modulus ==1)
  11. return 0;
  12.  
  13. result=1;
  14. base=base%modulus;
  15. while (exponent>0) {
  16. if (exponent%2==1)
  17. result = (result*base) % modulus;
  18.  
  19. exponent=exponent>>1;
  20. base= (base*base) % modulus;
  21.  
  22. }
  23.  
  24. return result;
  25.  
  26. }
  27.  
  28.  
  29. int choose(int x)
  30. {
  31. int a;
  32.  
  33. if (x==1)
  34. a = 2;
  35. else if (x==2)
  36. a = 7;
  37. else if (x==3)
  38. a = 61;
  39.  
  40. return a;
  41. }
  42.  
  43.  
  44. int main(void){
  45. double sttime, endtime ;
  46.  
  47. unsigned long x,q ;
  48.  
  49. unsigned long a,d,s ;
  50. int w,flag,r,isprime;
  51. int p=0;
  52.  
  53.  
  54. sttime = (double) clock();
  55.  
  56. for (s = MINNUM+1; s <= MAXNUM; s = s + 2){
  57. d = s - 1;
  58. r = 0;
  59.  
  60. while (d % 2 == 0 ){
  61. d = d/2;
  62. r++;
  63. }
  64.  
  65. isprime = 1;
  66. for(int i = 1; i <= 3; i++){
  67. flag = 0;
  68. a = choose(i);
  69.  
  70. x = modular_pow (a,d,s);
  71. //x=(a*q)%s;
  72.  
  73. if ((x==1) || (x==s-1))
  74. continue;
  75.  
  76. for(w=1; (w<=(r-1)); w++){
  77. //χ=χ^2mod n
  78. x = modular_pow(x,2,s);
  79.  
  80. if (x==s-1){
  81. flag=1;
  82. break;
  83. }
  84. }
  85.  
  86. if (flag==1)
  87. continue;
  88. else{
  89. isprime = 0;
  90. break;
  91. }
  92.  
  93. }
  94.  
  95. if (isprime)
  96. p=p+1;
  97. }
  98.  
  99. endtime = (double) clock();
  100. //printf("Checking range [%d,%d] for primes\n",min,max);
  101. printf("Found %d primes in ", p);
  102. printf("Time: %.3f secs\n", (endtime - sttime)/CLOCKS_PER_SEC);
  103.  
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement