Flash Website Builder- Trendy Site Builder is a Flash Site Building tool that helps users build stunning websites. Check Out Custom Custom Logo Design by LogoBee. Website Design and Free Logo Templates available.
 CodingForums.com Trying to write a program that "factors" any number into power of primes

Before you post, read our: Rules & Posting Guidelines

Enjoy an ad free experience by logging in. Not a member yet? Register.
07-03-2012, 02:24 PM   PM User | #1
koudai8
New to the CF scene

Join Date: Jul 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Trying to write a program that "factors" any number into power of primes

I am a beginner and let's just cut to the chase.

Here is the code I've written.

Quote:
 import java.io.*; import java.util.Scanner; public class primes { public static void main(String[] args) { double x=2d, y=0d, z=0d, count=0; //z=13699293826d Scanner sc = new Scanner(System.in); System.out.print ("Enter a number: "); z=sc.nextFloat(); while(x

Here is the problem:

1. Sometimes, a number has so many powers of 2 that the output window is filled with 2's. Is there any way to output one "2" and then the number of times it appears?

2. Is there any better way to write a program that completes this objective? I don't mind starting from scratch.

Thanks!

Last edited by koudai8; 07-03-2012 at 04:46 PM..

 07-11-2012, 09:44 PM PM User | #2 michaelh73 New Coder   Join Date: Jun 2012 Posts: 15 Thanks: 0 Thanked 3 Times in 3 Posts Well you could have a counter, so once it gets one success it won't print again. Just set the counter to 0 and in the if loop with the 'y', put if( y==0&&counter==0){ counter++; ...} then it won't print out again after the counter is set to 1. Also you should probably just make these ints, comparing with doubles can sometimes be tricky. You may also only need to make x go to z/2 and not z. Not sure if that mathematically makes sense but i think it does, it'll just make it goes a little faster. You could use a for loop which is essentially the same thing you are doing right now __________________ Tech jobs and developer jobs at Headhuntable

 Bookmarks

 Thread Tools Rate This Thread Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 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 :: Client side development     JavaScript programming         DOM and JSON scripting         Ajax and Design         JavaScript frameworks         Post a JavaScript     HTML & CSS     XML     Flash & ActionScript         Adobe Flex     Graphics and Multimedia discussions     General web building         Site reviews         Building for mobile devices :: Server side development     Apache configuration     Perl/ CGI     PHP         Post a PHP snippet     MySQL         Other Databases     Ruby & Ruby On Rails     ASP     ASP.NET     Java and JSP     Other server side languages/ issues         ColdFusion         Python :: Computing & Sciences     Computer Programming     Computer/PC discussions     Geek News and Humour Web Projects and Services Marketplace     Web Projects         Small projects (quick fixes and changes)         Medium projects (new script, new features, etc)         Large Projects (new web application, complex features etc)         Unknown sized projects (request quote)         Vacant job positions         Looking for work/ for hire         Project collaboration/ partnership         Paid work offers and requests (Now CLOSED)     Career, job, and business ideas or advice     Domains, Sites, and Designs for sale         Domains for sale         Websites for sale         Design templates and graphics for sale :: Other forums     Member Offers     Forum feedback and announcements

All times are GMT +1. The time now is 11:48 AM.