mersenne.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /* Finds Mersenne primes using the Lucas-Lehmer test
  2. *
  3. * Tom St Denis, tomstdenis@gmail.com
  4. */
  5. #include <time.h>
  6. #include <tommath.h>
  7. int
  8. is_mersenne (long s, int *pp)
  9. {
  10. mp_int n, u;
  11. int res, k;
  12. *pp = 0;
  13. if ((res = mp_init (&n)) != MP_OKAY) {
  14. return res;
  15. }
  16. if ((res = mp_init (&u)) != MP_OKAY) {
  17. goto LBL_N;
  18. }
  19. /* n = 2^s - 1 */
  20. if ((res = mp_2expt(&n, s)) != MP_OKAY) {
  21. goto LBL_MU;
  22. }
  23. if ((res = mp_sub_d (&n, 1, &n)) != MP_OKAY) {
  24. goto LBL_MU;
  25. }
  26. /* set u=4 */
  27. mp_set (&u, 4);
  28. /* for k=1 to s-2 do */
  29. for (k = 1; k <= s - 2; k++) {
  30. /* u = u^2 - 2 mod n */
  31. if ((res = mp_sqr (&u, &u)) != MP_OKAY) {
  32. goto LBL_MU;
  33. }
  34. if ((res = mp_sub_d (&u, 2, &u)) != MP_OKAY) {
  35. goto LBL_MU;
  36. }
  37. /* make sure u is positive */
  38. while (u.sign == MP_NEG) {
  39. if ((res = mp_add (&u, &n, &u)) != MP_OKAY) {
  40. goto LBL_MU;
  41. }
  42. }
  43. /* reduce */
  44. if ((res = mp_reduce_2k (&u, &n, 1)) != MP_OKAY) {
  45. goto LBL_MU;
  46. }
  47. }
  48. /* if u == 0 then its prime */
  49. if (mp_iszero (&u) == 1) {
  50. mp_prime_is_prime(&n, 8, pp);
  51. if (*pp != 1) printf("FAILURE\n");
  52. }
  53. res = MP_OKAY;
  54. LBL_MU:mp_clear (&u);
  55. LBL_N:mp_clear (&n);
  56. return res;
  57. }
  58. /* square root of a long < 65536 */
  59. long
  60. i_sqrt (long x)
  61. {
  62. long x1, x2;
  63. x2 = 16;
  64. do {
  65. x1 = x2;
  66. x2 = x1 - ((x1 * x1) - x) / (2 * x1);
  67. } while (x1 != x2);
  68. if (x1 * x1 > x) {
  69. --x1;
  70. }
  71. return x1;
  72. }
  73. /* is the long prime by brute force */
  74. int
  75. isprime (long k)
  76. {
  77. long y, z;
  78. y = i_sqrt (k);
  79. for (z = 2; z <= y; z++) {
  80. if ((k % z) == 0)
  81. return 0;
  82. }
  83. return 1;
  84. }
  85. int
  86. main (void)
  87. {
  88. int pp;
  89. long k;
  90. clock_t tt;
  91. k = 3;
  92. for (;;) {
  93. /* start time */
  94. tt = clock ();
  95. /* test if 2^k - 1 is prime */
  96. if (is_mersenne (k, &pp) != MP_OKAY) {
  97. printf ("Whoa error\n");
  98. return -1;
  99. }
  100. if (pp == 1) {
  101. /* count time */
  102. tt = clock () - tt;
  103. /* display if prime */
  104. printf ("2^%-5ld - 1 is prime, test took %ld ticks\n", k, tt);
  105. }
  106. /* goto next odd exponent */
  107. k += 2;
  108. /* but make sure its prime */
  109. while (isprime (k) == 0) {
  110. k += 2;
  111. }
  112. }
  113. return 0;
  114. }
  115. /* $Source: /cvs/libtom/libtommath/etc/mersenne.c,v $ */
  116. /* $Revision: 1.3 $ */
  117. /* $Date: 2006/03/31 14:18:47 $ */