Results 1 to 17 of 17
  1. #1
    blueduiker is offline Member
    Join Date
    Nov 2009
    Location
    California
    Posts
    55
    Rep Power
    0

    Exclamation Help! Factoring application

    Hi everyone, I enrolled in APCS this year and am new to java. So far I have been able to do all my projects with ease however this one seems difficult. Here is what my teacher wants the code to look like:
    ----------------------------------------------------------------------------
    Sample Output:

    Please enter an integer: 150

    Factored:

    150 = 2 x 3 x 5 x 5
    ----------------------------------------------------------------------------
    Could someone please help me? Here are some rules: we have not learned/not supposed to use arrays (idk if that helps), we know switch statements, for/do/while loops, if-statements, scanner class, math class, and some other stuff. Oh and this has to be done in java. Please respond as soon as possible. Thanks!

  2. #2
    blueduiker is offline Member
    Join Date
    Nov 2009
    Location
    California
    Posts
    55
    Rep Power
    0

    Default

    also it has to work for all numbers not just 150

  3. #3
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    12

    Default

    What have you done so far? What approaches have you tried?
    My Hobby Project: LegacyClone

  4. #4
    bubbless is offline Member
    Join Date
    Mar 2009
    Posts
    81
    Rep Power
    0

    Default

    Something like this?

    Java Code:
    int number = 150;
    int factor = 0;
    
    for(int i = 2; i <= number; i++) {
      if((number % i) == 0) {
        factor = i;
        break;
      }
    }
    
    if(factor != 0) {
      System.out.println("Factored: "+number+" = "+factor+" x "+number/factor);
    } else {
      System.out.println("Not possible.");
    }
    You can put it in a loop until you have the final result.

  5. #5
    blueduiker is offline Member
    Join Date
    Nov 2009
    Location
    California
    Posts
    55
    Rep Power
    0

    Default

    i tried doing an extensive number of if statements but that hasn't worked because there are an infinite number of prime numbers, so I deleted the code

  6. #6
    blueduiker is offline Member
    Join Date
    Nov 2009
    Location
    California
    Posts
    55
    Rep Power
    0

    Default

    yeah i think that might work
    thanks!

  7. #7
    blueduiker is offline Member
    Join Date
    Nov 2009
    Location
    California
    Posts
    55
    Rep Power
    0

    Default

    hmm i need to find a way to eliminate all none prime numbers
    my code looks like:

    import java.util.Scanner;
    import java.text.DecimalFormat;
    public class factor{
    public static void main (String[] args) {

    //This is my declaration section
    int user_input, factor;
    Scanner scan = new Scanner(System.in);

    //This section receives the users input
    System.out.print ("Please enter an integer to be factored:");
    user_input = scan.nextInt();

    System.out.println ("Factored:");
    System.out.print (user_input + " = ");

    for (int i = 2; i < user_input; i++) {
    if ((user_input % i) == 0) {
    factor = i;
    System.out.print (factor + " x ");
    }
    }
    }
    }



    The output for 24 with this code is:
    Please enter an integer to be factored: [24]
    Factored:
    24 = 2 x 3 x 4 x 6 x 8 x 12 x



    Also how do i get rid of that extra multiplication sign

  8. #8
    blueduiker is offline Member
    Join Date
    Nov 2009
    Location
    California
    Posts
    55
    Rep Power
    0

    Default

    Also 25 returns as 5 x
    how do i have it return as 5 x 5

  9. #9
    bubbless is offline Member
    Join Date
    Mar 2009
    Posts
    81
    Rep Power
    0

    Default

    Code tags please.

  10. #10
    blueduiker is offline Member
    Join Date
    Nov 2009
    Location
    California
    Posts
    55
    Rep Power
    0

    Default

    what are code tags?
    sorry

  11. #11
    bubbless is offline Member
    Join Date
    Mar 2009
    Posts
    81
    Rep Power
    0

    Default

    Put [ CODE ] and [ /CODE ] around your code. (without the spaces)

  12. #12
    blueduiker is offline Member
    Join Date
    Nov 2009
    Location
    California
    Posts
    55
    Rep Power
    0

    Default

    code looks like this so far:
    Java Code:
    import java.util.Scanner;
    import java.text.DecimalFormat;
    public class factor{
    public static void main (String[] args) {
    
    //This is my declaration section
    int user_input, factor;
    Scanner scan = new Scanner(System.in);
    
    //This section receives the users input
    System.out.print ("Please enter an integer to be factored:");
    user_input = scan.nextInt();
    
    System.out.println ("Factored:");
    System.out.print (user_input + " = ");
    
    for (int i = 2; i < user_input; i++) {
    if ((user_input % i) == 0) {
    factor = i;
    System.out.print (factor + " x ");
    }
    }
    }
    }

  13. #13
    bubbless is offline Member
    Join Date
    Mar 2009
    Posts
    81
    Rep Power
    0

    Default

    Try this as a method.

    Java Code:
    public method getNextFactor(int number) {
      int factor = 0;
      for (int i = 2; i < number; i++) {
        if ((number % i) == 0) {
          factor = i;
          break;
        }
      }
      return factor;
    }
    Use this method inside a loop to find all the factors.

  14. #14
    rdtindsm is offline Member
    Join Date
    Feb 2009
    Posts
    92
    Rep Power
    0

    Default

    OP seems to have the right idea on displaying his ouput. Will leave it to him to figure that out.

    What I have noticed is that he is incrementing his factor after he verifies that it is a factor. This will not be correct.

    Consider a number like 40 (2 x 2 x 2 x5);
    You will find 2, increment to 3 (not a factor), then increment to 4 which is a factor. But 4 is 2 x 2. You need to loop with the value 2 until you find that it is no longer a factor. Don't worry about the number being prime. You will find them as you progress in the algorithm.

    http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes

    The sieve of eratosthenes merely finds the primes, but the reference will help you understand what you are doing.


    Java Code:
    Factor(X)
    n=2
    while (not end condition)
       if (n is a factor of X) {
       add n to factor list
       X = X / n
       } // if
       while(n is a factor of X {
          add n to factor list
          X = X / n
       }  //while
       if((  X  equals 1) ){ 
            end condition = true
       }  //if
       increment n
    } //while
    end Factor
    edit: moved increment statement after if test, removed "and (n divides X) evenly.
    "X" MUST be 1 on the last iteration, or there is another prime factor

    edit:
    Last edited by rdtindsm; 11-02-2009 at 05:15 AM. Reason: logic error

  15. #15
    blueduiker is offline Member
    Join Date
    Nov 2009
    Location
    California
    Posts
    55
    Rep Power
    0

    Default

    thanks for all the hel

  16. #16
    blueduiker is offline Member
    Join Date
    Nov 2009
    Location
    California
    Posts
    55
    Rep Power
    0

    Default

    sorry i meant "help" not "hel"

  17. #17
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,370
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Replies: 0
    Last Post: 04-22-2009, 02:30 PM
  2. Replies: 0
    Last Post: 02-16-2008, 10:31 PM
  3. Replies: 0
    Last Post: 02-16-2008, 10:29 PM
  4. Help, GUI Application
    By Felissa in forum AWT / Swing
    Replies: 2
    Last Post: 07-04-2007, 09:19 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •