PDA

View Full Version : [College - Java] Prime Number Generator


musicdemon
03-1-2009, 10:57 AM
My assignment is to build a prime number generator using nested loops. If the number is prime, it gets concatenated to a String object. The program outputs the results into a message box using JOptionPane. No user input is needed.

My code compiles, but every single number starting from 2 gets output, instead of only Prime Numbers.

[EDIT] Normally I'd use an array for this, but my professor is pretty picky about everyone staying at the skill level of the class with assignments that are reviewed by my peers. It's so that the other kids will understand my code and be focused on helping me change my programming style instead of trying to figure out what an array is.

Here's my code:

import javax.swing.*;

public class Assignment7PrimeNumGen
{
public static void main(String [] args)
{

int intNumberToTest;
int intPrimeCheck;
String strOutput = "";

//Loop 1: Each number is chosen 1 by 1 and incremented to 100.
for(intNumberToTest = 1; intNumberToTest <= 100; intNumberToTest++)
{


//Loop 2: The number is now tested to see if it's prime
for(intPrimeCheck = intNumberToTest - 1; intPrimeCheck > 0; intPrimeCheck--)
{
if(intNumberToTest % intPrimeCheck == 0)
{
strOutput += intNumberToTest + "\n";
break;
}
}

}
//Output
JOptionPane.showMessageDialog(null, strOutput, "Prime Numbers", JOptionPane.INFORMATION_MESSAGE);


}
}

This is due by 12 AM EST tomorrow. It's an online assignment. Any help would be much appreciated!

musicdemon
03-1-2009, 11:40 AM
I'm willing to offer a CREDIT reward for anyone who can be of some help :)

fido123
03-1-2009, 12:37 PM
I did this a few times before but not in Java...all I remember is you have to use modular division along with integer division to find it. Instead of planning this algorithm at the keyboard, take the time and write down a formula which will find the prime numbers...you do need to use loops though with an exit condition.

Afrobean
03-1-2009, 03:20 PM
I don't feel like working out the algorithm for you, but wouldn't an array of int be a LOT better than a concatenated string? I'm sure you're just learning, but arrays aren't too difficult and you'd probably impress your instructor by showing that you understand when better programming techniques should be used even when you've not yet learned them.

Also, your overall technique is not very robust at all. What if you needed to repurpose the code to another similar function?

And what's with that break there in your if statement? And god, I hate += so ****ing much.

But yeah, I'm not a fan of psuedocode myself, but if you can't crack a good method to check primes and can't find one online to mimic, grabbing a piece of paper and a pen and going to town would be your best bet here.

ps dudebro, your algorithm is definitely ****ed. Looks like you're only checking the number against one of it's potential factors. For every int x, it appears that you only check it if it's divisible by (x-1). This is probably why 2 and 1 wouldn't show up in your list; 2-1=1 and 2 is divisible by 1; I guess modular division doesn't throw a divide by 0 error though, eh?

edit: I'm not doing any more grunt work for ya, but this should point you in the right direction: http://en.wikipedia.org/wiki/Primality_test

edit: haha reading that wiki article, and the thing actually suggests holding a bank of a predetermined amount of primes to check against to save processing time. Don't think you'd get a good grade doing that, but it actually does sound like a decent idea. Maybe if you make your **** more robust, you could put the first 200 or so primes in an array like it suggests, but also ensure that you have code in place that can check any int greater than 200 as well.

musicdemon
03-1-2009, 05:52 PM
Normally I'd use an array too, but my professor is pretty picky about everyone staying at the skill level of the class with assignments that are reviewed by the rest of the class. It's so that the other kids will understand my code and be focused on helping me change my programming style instead of trying to figure out what an array is. I'll edit that into my first post to avoid future confusion.

Anyway, thanks for the ideas, fido123 and Afrobean. That link for the Primality test will help me understand the logic of this whole thing. I tried sketching out the frame of the program on a piece of paper, but I still don't understand why my loop is outputting all non-primes as well. I thought my second loop took care of that, but I guess there's a problem with the second loop that I'm not seeing. I think I'll just have the program output a "1" before the string to avoid having problems with the loop. It's not elegant, but at least it'll be functional. I'll send you guys some credits (not a lot, though, since I'm pretty poor credit-wise).

Afrobean
03-1-2009, 05:55 PM
Actually, 1 isn't really considered a prime number, so it shouldn't be included. Depending on your method of checking though, it could potentially return 1 as a prime, so just be weary of that.

musicdemon
03-1-2009, 06:01 PM
Thanks for the clarification. The sample output my professor gave us had 1 on it, though, so I'll include it for now. I'll try to delicately tell him that's he's wrong about 1 being prime in the comments when I submit the program.

qqwref
03-1-2009, 06:07 PM
Your second loop makes no sense - it seems to be adding the NumberToTest to the string whenever it's divisible by a smaller number. Here's how I'd do it:


//Loop 2: The number is now tested to see if it's prime
bool boolHasAFactor = false;
for(intPrimeCheck = 2; intPrimeCheck < intNumberToTest; intPrimeCheck++)
{
if(intNumberToTest % intPrimeCheck == 0)
{
boolHasAFactor = true;
}
}
if (boolHasAFactor == false)
{
strOutput += intNumberToTest + "\n";
}


The boolean variable keeps track of whether the number has a factor or not - it starts out as false, but if the loop finds a factor it's set to true. We only add the number to the string if we couldn't find a factor (i.e. it's prime).

musicdemon
03-1-2009, 09:05 PM
I see! A boolean is much easier to work with in this situation. So my 2nd loop was taking 1 less from the NumberToTest and decrementing each time, whereas yours started at 2 and increments until the PrimeCheck is no longer < NumberToTest. I fixed up a minor mistake and it ended up working fine. Thank you, qqwref!