Results 1 to 4 of 4
  1. #1
    falco321 is offline Member
    Join Date
    Aug 2012
    Posts
    2
    Rep Power
    0

    Default Can not see why this Prime factorization is not working

    Here's my code:


    import java.util.*;

    public class FactorsOfAnInteger {
    public static void main(String[] args) {
    Scanner console = new Scanner(System.in);
    int input = 0;
    System.out.println("Enter an integer: ");
    input = console.nextInt();

    for (int i = 2; i < input; i++) {
    while (input % i == 0){
    System.out.print(i + " ");
    input /= i;
    }
    }

    System.out.println();
    }

    }



    It compiles fine and even properly factors SOME integers but not for all. For example, when I enter 16, I get the correct answer 2 2 2 2 or if I enter 150 I get 2 3 5 5 which is correct. However when I start to use other simple numbers like 80 I get 2 2 2 2 which is incorrect or when I type 9684 i get 2 2 3 3 which is also incorrect. Is there a logic error that I am missing?
    Thanks for any help in advance! =)

  2. #2
    Zyril is offline Senior Member
    Join Date
    Oct 2011
    Location
    Sweden
    Posts
    124
    Rep Power
    0

    Default Re: Can not see why this Prime factorization is not working

    Hello falco,

    please use the [CODE]-tags when posting code to maintain the Java highlighting and the structure of your code.

    Judging by your code, your for-loop is running as long as your int i is less than your specified integer to factorize, which obviously will give you the wrong result since you need to run the for-loop as long as int i is less or equal to your specified integer.

    I guess that adding a simple " = " in your for-loop will give you the correct results.

    Java Code:
     for (int i = 2; i <= input; i++)

    Cheers,
    Z!

  3. #3
    falco321 is offline Member
    Join Date
    Aug 2012
    Posts
    2
    Rep Power
    0

    Default Re: Can not see why this Prime factorization is not working

    Thank you very much, ugh such a simple mistake Sorry my first post, I'll add the [CODE] tag next time.

  4. #4
    Zyril is offline Senior Member
    Join Date
    Oct 2011
    Location
    Sweden
    Posts
    124
    Rep Power
    0

    Default Re: Can not see why this Prime factorization is not working

    Glad it worked out! Good luck!

Similar Threads

  1. Big prime
    By diamonddragon in forum New To Java
    Replies: 25
    Last Post: 02-02-2012, 11:04 PM
  2. Prime Factorization
    By skaterboy987 in forum New To Java
    Replies: 7
    Last Post: 10-27-2011, 01:18 AM
  3. Prime Number - System print all the prime numbers ...
    By pinkdreammsss in forum New To Java
    Replies: 20
    Last Post: 04-26-2009, 01:50 AM
  4. Prime numbers
    By tercius in forum New To Java
    Replies: 3
    Last Post: 05-04-2008, 06:05 AM
  5. Prime numbers
    By gapper in forum New To Java
    Replies: 3
    Last Post: 02-07-2008, 10:09 AM

Posting Permissions

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