Find out unique factors using Java












1















Problem:
Print all unique combination of factors (except 1) of a given number.



For example:
Input: 12



Output: [[2, 2, 3], [2, 6], [3, 4]]



My Solution:



public class Unique_factor {
public static void main(String args) {
int number = 12;
ArrayList<ArrayList<Integer>> combination = new ArrayList<ArrayList<Integer>>();
ArrayList<Integer> abc = new ArrayList<>();
for(int i = 2; i <= number; i++) {
if(number % i == 0) {
abc.add(i);
int result = number;
for(int j = i; j <= (number/i); j++) {
if(result % j == 0) {
result = result / j;
abc.add(j);
}
}
}
}

//System.out.println(combination);
System.out.println(abc);
}
}


Output:



[2, 2, 3, 3, 3, 4, 4, 6, 12]


As per my code, it prints out all the possible factors of 12. The j loop iterates until the (number/i). I create a list of list type ArrayList called combination to create a list of lists, but I don't know how to utilize it. Where should I change my code?










share|improve this question




















  • 2





    there are two mistakes here 1) logic and 2) not even adding abc to combination

    – Deadpool
    Dec 28 '18 at 19:37
















1















Problem:
Print all unique combination of factors (except 1) of a given number.



For example:
Input: 12



Output: [[2, 2, 3], [2, 6], [3, 4]]



My Solution:



public class Unique_factor {
public static void main(String args) {
int number = 12;
ArrayList<ArrayList<Integer>> combination = new ArrayList<ArrayList<Integer>>();
ArrayList<Integer> abc = new ArrayList<>();
for(int i = 2; i <= number; i++) {
if(number % i == 0) {
abc.add(i);
int result = number;
for(int j = i; j <= (number/i); j++) {
if(result % j == 0) {
result = result / j;
abc.add(j);
}
}
}
}

//System.out.println(combination);
System.out.println(abc);
}
}


Output:



[2, 2, 3, 3, 3, 4, 4, 6, 12]


As per my code, it prints out all the possible factors of 12. The j loop iterates until the (number/i). I create a list of list type ArrayList called combination to create a list of lists, but I don't know how to utilize it. Where should I change my code?










share|improve this question




















  • 2





    there are two mistakes here 1) logic and 2) not even adding abc to combination

    – Deadpool
    Dec 28 '18 at 19:37














1












1








1








Problem:
Print all unique combination of factors (except 1) of a given number.



For example:
Input: 12



Output: [[2, 2, 3], [2, 6], [3, 4]]



My Solution:



public class Unique_factor {
public static void main(String args) {
int number = 12;
ArrayList<ArrayList<Integer>> combination = new ArrayList<ArrayList<Integer>>();
ArrayList<Integer> abc = new ArrayList<>();
for(int i = 2; i <= number; i++) {
if(number % i == 0) {
abc.add(i);
int result = number;
for(int j = i; j <= (number/i); j++) {
if(result % j == 0) {
result = result / j;
abc.add(j);
}
}
}
}

//System.out.println(combination);
System.out.println(abc);
}
}


Output:



[2, 2, 3, 3, 3, 4, 4, 6, 12]


As per my code, it prints out all the possible factors of 12. The j loop iterates until the (number/i). I create a list of list type ArrayList called combination to create a list of lists, but I don't know how to utilize it. Where should I change my code?










share|improve this question
















Problem:
Print all unique combination of factors (except 1) of a given number.



For example:
Input: 12



Output: [[2, 2, 3], [2, 6], [3, 4]]



My Solution:



public class Unique_factor {
public static void main(String args) {
int number = 12;
ArrayList<ArrayList<Integer>> combination = new ArrayList<ArrayList<Integer>>();
ArrayList<Integer> abc = new ArrayList<>();
for(int i = 2; i <= number; i++) {
if(number % i == 0) {
abc.add(i);
int result = number;
for(int j = i; j <= (number/i); j++) {
if(result % j == 0) {
result = result / j;
abc.add(j);
}
}
}
}

//System.out.println(combination);
System.out.println(abc);
}
}


Output:



[2, 2, 3, 3, 3, 4, 4, 6, 12]


As per my code, it prints out all the possible factors of 12. The j loop iterates until the (number/i). I create a list of list type ArrayList called combination to create a list of lists, but I don't know how to utilize it. Where should I change my code?







java algorithm arraylist






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 2 at 6:24









LAD

1,9672720




1,9672720










asked Dec 28 '18 at 19:32









EncipherEncipher

31512




31512








  • 2





    there are two mistakes here 1) logic and 2) not even adding abc to combination

    – Deadpool
    Dec 28 '18 at 19:37














  • 2





    there are two mistakes here 1) logic and 2) not even adding abc to combination

    – Deadpool
    Dec 28 '18 at 19:37








2




2





there are two mistakes here 1) logic and 2) not even adding abc to combination

– Deadpool
Dec 28 '18 at 19:37





there are two mistakes here 1) logic and 2) not even adding abc to combination

– Deadpool
Dec 28 '18 at 19:37












1 Answer
1






active

oldest

votes


















1














I came up with the following method for finding the unique factors of a number. It is a bit more complex, though, than what you had previously tried and there might be a better solution, but the method seems to work correctly.



public class UniqueFactors {
public static void main(String args) {
int input = 12; // Currently, the output is blank if the input is 1
ArrayList<ArrayList<Integer>> combinations = new ArrayList<>();

for (int i = 2; i <= input; i++) {
int result;
if (input % i == 0) {
result = input / i;
ArrayList<Integer> factorSet = new ArrayList<>();
factorSet.add(i);
boolean moreFactors = false;
int result2 = result;
for (int j = 2; j <= result2; j++) {
if (result2 % j == 0) {
moreFactors = true;
factorSet.add(j);
result2 = result2 / j;
j = 1; // Reset to one because it will be added to on the next iteration
}
}
if (!moreFactors) factorSet.add(result);
//> The following chunk just gets rid of duplicate combinations that were in different orders
boolean copy = false;
for (int k = 0; k < combinations.size(); k++) {
if (combinations.get(k).size() == factorSet.size()) {
Collections.sort(combinations.get(k));
Collections.sort(factorSet);
if (combinations.get(k).equals(factorSet)) {
copy = true;
break;
}
}
}
if (!copy) combinations.add(factorSet);
}
}

for (int i = 0; i < combinations.size(); i++) {
System.out.println(combinations.get(i));
}
}
}


Output:



[2, 2, 3]
[3, 4]
[2, 6]
[1, 12]


Hopefully this post helps in some way.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53963465%2ffind-out-unique-factors-using-java%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    I came up with the following method for finding the unique factors of a number. It is a bit more complex, though, than what you had previously tried and there might be a better solution, but the method seems to work correctly.



    public class UniqueFactors {
    public static void main(String args) {
    int input = 12; // Currently, the output is blank if the input is 1
    ArrayList<ArrayList<Integer>> combinations = new ArrayList<>();

    for (int i = 2; i <= input; i++) {
    int result;
    if (input % i == 0) {
    result = input / i;
    ArrayList<Integer> factorSet = new ArrayList<>();
    factorSet.add(i);
    boolean moreFactors = false;
    int result2 = result;
    for (int j = 2; j <= result2; j++) {
    if (result2 % j == 0) {
    moreFactors = true;
    factorSet.add(j);
    result2 = result2 / j;
    j = 1; // Reset to one because it will be added to on the next iteration
    }
    }
    if (!moreFactors) factorSet.add(result);
    //> The following chunk just gets rid of duplicate combinations that were in different orders
    boolean copy = false;
    for (int k = 0; k < combinations.size(); k++) {
    if (combinations.get(k).size() == factorSet.size()) {
    Collections.sort(combinations.get(k));
    Collections.sort(factorSet);
    if (combinations.get(k).equals(factorSet)) {
    copy = true;
    break;
    }
    }
    }
    if (!copy) combinations.add(factorSet);
    }
    }

    for (int i = 0; i < combinations.size(); i++) {
    System.out.println(combinations.get(i));
    }
    }
    }


    Output:



    [2, 2, 3]
    [3, 4]
    [2, 6]
    [1, 12]


    Hopefully this post helps in some way.






    share|improve this answer




























      1














      I came up with the following method for finding the unique factors of a number. It is a bit more complex, though, than what you had previously tried and there might be a better solution, but the method seems to work correctly.



      public class UniqueFactors {
      public static void main(String args) {
      int input = 12; // Currently, the output is blank if the input is 1
      ArrayList<ArrayList<Integer>> combinations = new ArrayList<>();

      for (int i = 2; i <= input; i++) {
      int result;
      if (input % i == 0) {
      result = input / i;
      ArrayList<Integer> factorSet = new ArrayList<>();
      factorSet.add(i);
      boolean moreFactors = false;
      int result2 = result;
      for (int j = 2; j <= result2; j++) {
      if (result2 % j == 0) {
      moreFactors = true;
      factorSet.add(j);
      result2 = result2 / j;
      j = 1; // Reset to one because it will be added to on the next iteration
      }
      }
      if (!moreFactors) factorSet.add(result);
      //> The following chunk just gets rid of duplicate combinations that were in different orders
      boolean copy = false;
      for (int k = 0; k < combinations.size(); k++) {
      if (combinations.get(k).size() == factorSet.size()) {
      Collections.sort(combinations.get(k));
      Collections.sort(factorSet);
      if (combinations.get(k).equals(factorSet)) {
      copy = true;
      break;
      }
      }
      }
      if (!copy) combinations.add(factorSet);
      }
      }

      for (int i = 0; i < combinations.size(); i++) {
      System.out.println(combinations.get(i));
      }
      }
      }


      Output:



      [2, 2, 3]
      [3, 4]
      [2, 6]
      [1, 12]


      Hopefully this post helps in some way.






      share|improve this answer


























        1












        1








        1







        I came up with the following method for finding the unique factors of a number. It is a bit more complex, though, than what you had previously tried and there might be a better solution, but the method seems to work correctly.



        public class UniqueFactors {
        public static void main(String args) {
        int input = 12; // Currently, the output is blank if the input is 1
        ArrayList<ArrayList<Integer>> combinations = new ArrayList<>();

        for (int i = 2; i <= input; i++) {
        int result;
        if (input % i == 0) {
        result = input / i;
        ArrayList<Integer> factorSet = new ArrayList<>();
        factorSet.add(i);
        boolean moreFactors = false;
        int result2 = result;
        for (int j = 2; j <= result2; j++) {
        if (result2 % j == 0) {
        moreFactors = true;
        factorSet.add(j);
        result2 = result2 / j;
        j = 1; // Reset to one because it will be added to on the next iteration
        }
        }
        if (!moreFactors) factorSet.add(result);
        //> The following chunk just gets rid of duplicate combinations that were in different orders
        boolean copy = false;
        for (int k = 0; k < combinations.size(); k++) {
        if (combinations.get(k).size() == factorSet.size()) {
        Collections.sort(combinations.get(k));
        Collections.sort(factorSet);
        if (combinations.get(k).equals(factorSet)) {
        copy = true;
        break;
        }
        }
        }
        if (!copy) combinations.add(factorSet);
        }
        }

        for (int i = 0; i < combinations.size(); i++) {
        System.out.println(combinations.get(i));
        }
        }
        }


        Output:



        [2, 2, 3]
        [3, 4]
        [2, 6]
        [1, 12]


        Hopefully this post helps in some way.






        share|improve this answer













        I came up with the following method for finding the unique factors of a number. It is a bit more complex, though, than what you had previously tried and there might be a better solution, but the method seems to work correctly.



        public class UniqueFactors {
        public static void main(String args) {
        int input = 12; // Currently, the output is blank if the input is 1
        ArrayList<ArrayList<Integer>> combinations = new ArrayList<>();

        for (int i = 2; i <= input; i++) {
        int result;
        if (input % i == 0) {
        result = input / i;
        ArrayList<Integer> factorSet = new ArrayList<>();
        factorSet.add(i);
        boolean moreFactors = false;
        int result2 = result;
        for (int j = 2; j <= result2; j++) {
        if (result2 % j == 0) {
        moreFactors = true;
        factorSet.add(j);
        result2 = result2 / j;
        j = 1; // Reset to one because it will be added to on the next iteration
        }
        }
        if (!moreFactors) factorSet.add(result);
        //> The following chunk just gets rid of duplicate combinations that were in different orders
        boolean copy = false;
        for (int k = 0; k < combinations.size(); k++) {
        if (combinations.get(k).size() == factorSet.size()) {
        Collections.sort(combinations.get(k));
        Collections.sort(factorSet);
        if (combinations.get(k).equals(factorSet)) {
        copy = true;
        break;
        }
        }
        }
        if (!copy) combinations.add(factorSet);
        }
        }

        for (int i = 0; i < combinations.size(); i++) {
        System.out.println(combinations.get(i));
        }
        }
        }


        Output:



        [2, 2, 3]
        [3, 4]
        [2, 6]
        [1, 12]


        Hopefully this post helps in some way.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 2 at 5:25









        LADLAD

        1,9672720




        1,9672720






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53963465%2ffind-out-unique-factors-using-java%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Mossoró

            Error while reading .h5 file using the rhdf5 package in R

            Pushsharp Apns notification error: 'InvalidToken'