Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Trying to write Factoring Numbers Code--Please Help.

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Trying to write Factoring Numbers Code--Please Help.

    I am trying to finish the code for a simple program that will break a number down and give you the factors of that number. I have the program working all for one part. Right now it returns something like "2*2*2*5*5*" and instead I want it to say 2^3+5^2. Can anyone tell me what I would need to have to do this? I need to use some type of loop I think, and a While statement maybe?

    import java.util.*;
     
    class Factor
    {
        String factor(int n)
        {
            String S = "";
            int f = 2;
            while (n > 1)
                if (n%f == 0)
                {
                    S += f + "*";
                    n /= f;
                }
                else
                    f++;
                return S;
        }
     
        public void setN(int n)
        {
            n = 0;
        }
     
        public void getN(int n)
        {
     
        }
     
    }
     
    class FactorDemo // main Class
            {
                public static void main(String [] args)
                {
                    Scanner in = new Scanner(System.in);
     
                    Factor ML = new Factor();
     
                    System.out.print("Enter a positive integer(-1 to stop):");
                    int n = in.nextInt();
     
                    while (n > 0) //0 or negative number to stop the program
                                  // is called a sentinel
                    {
                        System.out.println(ML.factor(n));
     
                        System.out.print("Enter a positive integer(-1 to stop):");
                        n = in.nextInt();
                    }
     
                    System.out.println("Thanks for using my program.");
     
                }
            }
    Last edited by uks2h; November 11th, 2010 at 05:56 PM.


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Trying to write Factoring Numbers Code--Please Help.

    You could, but you'd have to test to see if each factor is prime. If it isn't, then it can be simplified.

    Beyond that, I've no clue what to do.

    Using charAt() won't work as you could have an 11 as a factor.

  3. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Trying to write Factoring Numbers Code--Please Help.

    Why are you using another username?

    help factor

  4. #4
    Junior Member
    Join Date
    Nov 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Trying to write Factoring Numbers Code--Please Help.

    Quote Originally Posted by javapenguin View Post
    Why are you using another username?

    help factor

    ??? I'm new to this site. Only have this username.

  5. #5
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,607
    Thanks
    24
    Thanked 315 Times in 295 Posts

    Default Re: Trying to write Factoring Numbers Code--Please Help.

    You want to make some sort of int array in your factor method. Then use that int array to determine how many instances of x number appears. I would advise a List (such as a Vector) since you will not need to worry about size. Here is what you need:
    String factor(int n)
    {
    	String s;
    	//Create Integer Vector
    	Vector<Integer> numbers = new Vector<Integer>();
      	int f = 2;
      	while (n > 1)
    	{
    		if (n%f == 0)
    		{
    			//Add number to Integer Vector
    			numbers.add(new Integer(f));
    			n /= f;
    		}
    		else
    			f++;
    	}
    	//Go through Vector. Doing this way because we will remove from the list as we go
    	while(numbers.size()>0)
    	{
    		//Get number and remove it to allow counting
    		Integer tempNumber = numbers.remove(i);
    		//Counter Variable
    		int numberOfTimes = 1;
    		//I think this works, not entirely sure
    		while(numbers.indexOf(tempNumber)!=-1)
    		{
    			//Remove this number from the Vector
    			numbers.remove(numbers.indexOf(tempNumber));
    			//Increment counter
    			numberOfTimes++;
    		}
    		if(numberOfTimes==1)
    			s+=tempNumber+"*";
    		else
    			s+=tempNumber+"^"+numberOfTimes+"*";
    	}
    	//Remove the last * symbol
    	return (S.substring(0,s.length()-1));
    }

    There is probably a better way, but that is the best I could come up with without putting more effort in. I haven't tested the code, so tell me if it doesnt work.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

Similar Threads

  1. How to write server?
    By snytkine in forum Java Networking
    Replies: 4
    Last Post: October 22nd, 2010, 02:25 PM
  2. How to write 2 dimensional array of float numbers to binary file?
    By Ghuynh in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: June 17th, 2010, 04:26 PM
  3. Replies: 0
    Last Post: March 5th, 2010, 12:32 AM
  4. How can i write this in code?
    By Mr. steve in forum Java Theory & Questions
    Replies: 2
    Last Post: November 30th, 2009, 09:07 PM
  5. Need Help - Factoring & Prime Finding Code
    By prodigytoast in forum Algorithms & Recursion
    Replies: 5
    Last Post: November 5th, 2009, 06:38 AM