Java Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingJava Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old September 28th, 2006, 10:38 AM
Robbert Robbert is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2006
Posts: 3 Robbert User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 57 m 9 sec
Reputation Power: 0
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:
public class CollectionB {
  private static 
int numberOfPrimes 10;

  public static 
void main(String[] args) {
    
calculate();
  }

  private static 
void calculate() {
    
int primesFound 0;
    
int exponentToCheck 0;

    while(
primesFound numberOfPrimes) {
      
exponentToCheck++;

      
//Yeah, I accidentally calculated this twice.
      
int maxDivisor = (int)(Math.pow(2exponentToCheck)) - 1;
      
int numberToCheck = (int)(Math.pow(2exponentToCheck)) - 1;
      
      
int divisor 2;

      
boolean isPrime true;

      while(
divisor maxDivisor && isPrime) {
        
isPrime numberToCheck divisor 0;
        
divisor++;
      }
      if(
isPrime) {
        
primesFound++;
        
System.out.println("Prime #" primesFound ": " numberToCheck);
      }
    }
  }



I'm not sure where the problem lies, to be honest. Any ideas would be welcome.
Thanks in advance!

Reply With Quote
  #2  
Old November 6th, 2006, 05:17 PM
fizker fizker is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Location: denmark
Posts: 42 fizker User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 18 m 18 sec
Reputation Power: 5
Send a message via MSN to fizker
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

Reply With Quote
  #3  
Old February 27th, 2007, 09:44 PM
DeadSpam DeadSpam is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2007
Posts: 2 DeadSpam User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 17 m 21 sec
Reputation Power: 0
Use Unsigned Integers (uint) rather than int

Quote:
Originally Posted by Robbert
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:
public class CollectionB {
  private static 
int numberOfPrimes 10;

  public static 
void main(String[] args) {
    
calculate();
  }

  private static 
void calculate() {



I'm not sure where the problem lies, to be honest. Any ideas would be welcome.
Thanks in advance!


Define the ints as uints and it should be fine. Or switch to a Sun with 64 bit integers...

Ed

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingJava Development > Quick question about my small Mersenne prime programme.


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
Stay green...Green IT