Here’s another one of my (for me) brutally difficult homework assignments.

I gotta be honest, this one had me nearly in tears and telling Eston I was ready to quit. Hugs and hot cocoa and a few helpful lines of code later, and I was back on top of the world; but things got dark there for a minute.

Ok. So.

I wrote two classes. One class, called MyMath, contained three methods for calculating prime numbers, Mersenne primes, and perfect numbers, respectively. I spent an hour on the algorithm.

public class MyMath { //boolean method ito determine whether a number is prime public static boolean isPrime( int number ) { // initialize the boolean variable prime to true boolean prime = true; //create a for loop to run through a series of divisors for( int divisor = 2; divisor <= (number/2); divisor++ ) //test whether the number/divisor = 0 // if remainder = 0, variable prime is false { if( (number % divisor) == 0 ) prime = false; } //end for // if prime is true at the end of the loop, return true for the method return prime; } // end method isPrime // boolean method ito determine whether a number is a Mersenne prime public static boolean isMersenne( int number ) { // declare and initialize variable powTwo to 2 int powTwo = 2; boolean mersenne = false; //run isPrime method first isPrime( number ); // if the return is true // test to see if the prime //is one less than a power of 2 if( true ) { while( powTwo <= number ) powTwo *= 2; if( powTwo - number == 1 ) // if the prime is one less than a power of 2, return true mersenne = true; } return mersenne; } // end method isMersenne // boolean method ito determine whether a number is perfect public static boolean isPerfect( int number ) { //declare and initialize variable total to zero int total = 0; // for divisors 1 through half of number, increment divisor for( int divisor = 1; divisor <= number/2; divisor++ ) // get remainder of number and divisor // if remainder is zero, add the divisor to the total { if( (number % divisor) == 0 ) total += divisor; } //if the total is equal to the number, return true return ( total == number ); } // end method isPerfect } // end class MyMath

I then designed another class to offer the user a choice (see a bunch of primes, a bunch of Mersenne primes, or a bunch of perfect numbers), loop a set of numbers, and display the result in a dialog box.

Just for the record, STRING CONCATENATION IS THE EASIEST WAY TO PRINT MULTIPLE LINES IN THE SAME DIALOG BOX. Oh my goodness, it was so flippin’ easy. That is, it was easy after being hard for about an hour.

import java.util.Scanner; import javax.swing.JOptionPane; public class MyMathTest { public static void main( String [] args ) { // display menu, get user choice String userChoice = JOptionPane.showInputDialog( "Choose 1 to see the prime numbers between 1 and 1,000.\n" + "Choose 2 to see the Mersenne prime numbers between 1 and 10,000.\n" + "Choose 3 to see the perfect numbers between 1 and 10,000.\n Choose 4 to quit." ); int choice = Integer.parseInt( userChoice ); String message = ""; switch( choice ) { case 1 : // loop numbers from 1 to 1,000, printing only the primes { int counter = 0; for( int number = 2; number <= 1000; number ++) { MyMath mathTest = new MyMath(); if( mathTest.isPrime( number ) ) { counter++; message += String.format( "%d ", number ); if( counter % 10 == 0 ) message += ( "\n" ); }// end if } // end for message += ( "\n\n Programmed by Jolie O'Dell" ); JOptionPane.showMessageDialog( null, message ); } // end case 1 break; case 2 : // loop numbers 1-10,000, printing only Mersenne primes { int counter = 0; for( int number = 2; number <= 10000; number ++) { MyMath mathTest = new MyMath(); if( mathTest.isMersenne( number ) ) { counter++; message += String.format( "%d ", number ); if( counter % 10 == 0 ) message += ( "\n" ); }// end if } // end for message += ( "\n\n Programmed by Jolie O'Dell" ); JOptionPane.showMessageDialog( null, message ); } // end case 2 break; case 3 : // loop numbers 1-10,000, printing only the perfect numbers { int counter = 0; for( int number = 2; number <= 10000; number ++) { MyMath mathTest = new MyMath(); if( mathTest.isPerfect( number ) ) { counter++; message += String.format( "%d ", number ); if( counter % 10 == 0 ) message += ( "\n" ); }// end if } // end for message += ( "\n\n Programmed by Jolie O'Dell" ); JOptionPane.showMessageDialog( null, message ); } // end case 3 break; case 4: break; } // end switch } // end method main }// end class MyMathTest

I know there’s a lot of code redundancy up there — and believe me, I’d love to knock a few lines out by writing a method or two — but I kinda got to the end of my rope, and I also didn’t want to write any code that wasn’t specifically asked for in the homework spec; doing so will get you dinked in these parts.

I’m now going to get a few hours of sleep before class. If you have the leisure to study and not work full time, thank your lucky stars.

If you’ve got any hints for me, let me know in the comments! I’m always trying to learn a few better ways to get the Java job done.

*Image courtesy of journeyscoffee.*

You aren’t keeping the result of the primeness test – you need to check both that and the mersenne-ness

Some efficiency thoughts:

As soon as you know prime is false, you should return – you only need to find one divisor. Also, you don’t need to divide by all the even numbers, so you can try 2 first, then 3,5,7 etc.

Your Mersenne case would be way faster if you did the power of 2 -1 test before checking primeness.

How are you able to keep up with a computer science study while working full time? That’s just brave! I hardly keep up with college with a part time job.

It’s really, really, REALLY hard. I’m only taking one class — granted, it’s a freakin’ difficult class — which is probably the sole reason I still have my sanity.

I appreciate that you’re honest about how difficult and frustrating it can get being a neophyte coder. I’m having a very similar experience, first class of a comp sci program and gnashing my teeth over how embarrassingly long it can take me to do something that my 10-year veteran programmer boyfriend will do (and do better) in 10 minutes.

I hang in there for the day that I can sport this t-shirt proudly: http://www.zazzle.com/programmer_big_deal_tshirt-235303885818403704. Knowing I’m not the only person getting grumpy once in a while will help a lot too. 🙂