|
|||||||
| Homework & General Information Forum This is a forum to ask for assistance with your homework, as well as seeking and providing information on a variety of subjects |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
FFR Veteran
FFR Veteran
Join Date: Dec 2008
Location: Massachusetts
Age: 25
Posts: 274
|
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: 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);
}
}
__________________
Winner of Wargasm's 1 Billion Score Challenge 3rd place in badman's 2nd tourney of darkness (beginner's division) ![]()
Last edited by musicdemon; 03-1-2009 at 05:55 PM.. |
|
|
|
|
|
#2 |
|
FFR Veteran
FFR Veteran
Join Date: Dec 2008
Location: Massachusetts
Age: 25
Posts: 274
|
I'm willing to offer a CREDIT reward for anyone who can be of some help
![]() |
|
|
|
|
|
#3 |
|
FFR Player
Join Date: Sep 2005
Age: 21
Posts: 4,188
|
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.
|
|
|
|
|
|
#4 |
|
FFR Player
|
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.
__________________
Last edited by Afrobean; 03-1-2009 at 03:36 PM.. |
|
|
|
|
|
#5 |
|
FFR Veteran
FFR Veteran
Join Date: Dec 2008
Location: Massachusetts
Age: 25
Posts: 274
|
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). |
|
|
|
|
|
#6 |
|
FFR Player
|
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.
__________________
|
|
|
|
|
|
#7 |
|
FFR Veteran
FFR Veteran
Join Date: Dec 2008
Location: Massachusetts
Age: 25
Posts: 274
|
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.
|
|
|
|
|
|
#8 |
|
stepmania archaeologist
FFR Simfile Author
|
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:
Code:
//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";
}
__________________
Best AAAs: Starwolf (72), Futsuu No Uta (70), EHHS, Powerpuff Gabberz, Still Blastin (69), .357 Magnum, The earth blew up v2, Pure Ruby, Variations 2, VS Boss Battle (68), eight 67s, eight 66s Good scores: Gotta Catch 'Em All (1-0-0-1), Chaosmaid (BF), Piano Etude Demon Fire (4g), Ketsarku Mozgalom (4g), Staring at My Spaceship (BF), World Tour 2004 (BF), Pants (5g) Stepmania Song Search - 1304 packs and counting! |
|
|
|
|
|
#9 |
|
FFR Veteran
FFR Veteran
Join Date: Dec 2008
Location: Massachusetts
Age: 25
Posts: 274
|
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!
|
|
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|