![]() |
![]() |
#1 |
24×547 Posts |
![]() Code:
bool Lucas_Lehmer_Test(unsigned long p) { // check if p is an odd prime /*if(p % 2 == 0) return false; for(unsigned long c = 3; (c * c) <= p; c += 2) if (p % c == 0) return false;*/ unsigned long int mp = (1 << p) - 1; // 2^p - 1 unsigned long s = 4; // s := 4 for(unsigned long i = 3; i <= p; i++) // for i from 3 to p do { s = ((s * s) - 2) % mp; // s := s^2 - 2 mod 2^p - 1 } return !s; // if s == 0 then // 2p-1 is prime // else // 2p-1 is composite } for numbers 1 to 31 inclusive so anyone know why this is not working |
![]() |
![]() |
#2 |
"Richard B. Woods"
Aug 2002
Wisconsin USA
22×3×641 Posts |
![]()
Unsigned long is 32 bits, right?
When p = 17 (or more), mp = 2^17-1 (or more). So s could reach a value greater than 2^16. If so, then in your s = ((s * s) - 2) % mp step, the s*s intermediate value could overflow 32-bits. |
![]() |
![]() |
![]() |
#3 |
Aug 2002
5008 Posts |
![]()
Also make sure the value is not negative when you're doing the modular reduction. The mod operation in C has undefined behavior for negative integers.
|
![]() |
![]() |
![]() |
Thread Tools | |
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
Lucas-Lehmer test | Mathsgirl | Information & Answers | 23 | 2014-12-10 16:25 |
lucas-lehmer theorem | Robot2357 | Math | 6 | 2013-06-15 03:10 |
lucas lehmer outstretch | science_man_88 | Miscellaneous Math | 7 | 2010-07-14 12:35 |
Lucas-Lehmer Test | storm5510 | Math | 22 | 2009-09-24 22:32 |
Lucas-Lehmer | Dougal | Information & Answers | 9 | 2009-02-06 10:25 |