FlashFlashRevolution [College - Java] Prime Number Generator
 Register FAQ Members List Social Groups Calendar Search Today's Posts Mark Forums Read

 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

 03-1-2009, 10:57 AM #1 musicdemon FFR Veteran     FFR Veteran Join Date: Dec 2008 Location: Massachusetts Age: 25 Posts: 274 [College - Java] Prime Number Generator 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); } }``` This is due by 12 AM EST tomorrow. It's an online assignment. Any help would be much appreciated! __________________ 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..
 03-1-2009, 11:40 AM #2 musicdemon FFR Veteran     FFR Veteran Join Date: Dec 2008 Location: Massachusetts Age: 25 Posts: 274 Re: [College - Java] Prime Number Generator I'm willing to offer a CREDIT reward for anyone who can be of some help __________________ Winner of Wargasm's 1 Billion Score Challenge 3rd place in badman's 2nd tourney of darkness (beginner's division)
 03-1-2009, 12:37 PM #3 fido123 FFR Player     Join Date: Sep 2005 Age: 21 Posts: 4,188 Re: [College - Java] Prime Number Generator 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.
 03-1-2009, 03:20 PM #4 Afrobean FFR Player     Join Date: Dec 2003 Location: Hazel Park, MI Age: 25 Posts: 12,877 Re: [College - Java] Prime Number Generator 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..
 03-1-2009, 05:52 PM #5 musicdemon FFR Veteran     FFR Veteran Join Date: Dec 2008 Location: Massachusetts Age: 25 Posts: 274 Re: [College - Java] Prime Number Generator 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). __________________ Winner of Wargasm's 1 Billion Score Challenge 3rd place in badman's 2nd tourney of darkness (beginner's division)
 03-1-2009, 05:55 PM #6 Afrobean FFR Player     Join Date: Dec 2003 Location: Hazel Park, MI Age: 25 Posts: 12,877 Re: [College - Java] Prime Number Generator 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. __________________
 03-1-2009, 06:01 PM #7 musicdemon FFR Veteran     FFR Veteran Join Date: Dec 2008 Location: Massachusetts Age: 25 Posts: 274 Re: [College - Java] Prime Number Generator 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. __________________ Winner of Wargasm's 1 Billion Score Challenge 3rd place in badman's 2nd tourney of darkness (beginner's division)
 03-1-2009, 06:07 PM #8 qqwref stepmania archaeologist FFR Simfile Author     FFR Veteran Join Date: Aug 2005 Age: 23 Posts: 3,826 Re: [College - Java] Prime Number Generator 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"; }``` 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). __________________ 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!
 03-1-2009, 09:05 PM #9 musicdemon FFR Veteran     FFR Veteran Join Date: Dec 2008 Location: Massachusetts Age: 25 Posts: 274 Re: [College - Java] Prime Number Generator 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! __________________ Winner of Wargasm's 1 Billion Score Challenge 3rd place in badman's 2nd tourney of darkness (beginner's division)

 Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 Thread Tools Display Modes Linear Mode

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is Off Forum Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home Introductions     Newbie Forum     Chit Chat         Ask Me Anything         Birthdays         Radio     FFR Events Flash Flash Revolution Game/Site Related Forums     FFR General Talk         FFR Songs/Permissions Subforum         FFR Tournaments Subforum         FFR Difficulty Subforum     FFR Bugreport & Suggestions     Multiplayer Plaza     Spin It Up     Brag Board         Brag Board Archives     FFR Profiles Gaming     General Video Games     Music Games     Stepmania         Simulator Files Life & The Arts     32-Bit RGB     Bits and Bytes     Critical Thinking         Homework & General Information Forum     "Game" threads         The Werewolf Game     Literature     Media     Musicians' Jungle Gym

All times are GMT -5. The time now is 10:19 AM.