|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Quick question about my small Mersenne prime programme.
Hi,
For a project, I've to write a program that returns the first 10 Mersenne primes. For the uninitiated: A Mersenne Prime can be defined as this: If 2^n-1 is a prime, then this number is Mersenne prime. E.G.: 2^3 - 1 = 7 is a Mersenne prime. 2^6 - 1 = 63 is not a Mersenne prime since 63 % 3 = 0 Making the program was no problem, but one small error occurs: When it reaches 2^31 - 1, it finds that 2 is a divisor (hence not a prime), and then it loops infinitely, calculating (2^31 - 1) % 2 all of the time without stopping. Other numbers don't show this behaviour when the program finds out it's not a prime. In essence, the program detects if it's a prime. If it is, it stops, outputs the prime, increments the exponent and starts anew. If it isn't, it stops, increments the exponent and starts anew. Except with that number. It always occurs at that particular number. Even if I change the exponent to >32, it calculates (2^31 - 1) % 2. Here's the code: PHP Code:
I'm not sure where the problem lies, to be honest. Any ideas would be welcome. Thanks in advance! |
|
#2
|
|||
|
|||
|
without looking too closely at your code, i think the problem you have encountered lies in the natural boundary of using a 32-bit integer (where, incidentially, 2^32-1 is the maximum number)
__________________
Benjamin Horsleben horsleben.com/benjamin Don't blame malice for what stupidity can explain |
|
#3
|
|||
|
|||
|
Use Unsigned Integers (uint) rather than int
Quote:
Define the ints as uints and it should be fine. Or switch to a Sun with 64 bit integers... Ed |
![]() |
| Viewing: Dev Articles Community Forums > Programming > Java Development > Quick question about my small Mersenne prime programme. |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|