Getting a wrong output using arraylistsDoes a finally block always get executed in Java?Create ArrayList from arrayWhen to use LinkedList over ArrayList in Java?How do I get a consistent byte representation of strings in C# without manually specifying an encoding?How to get an enum value from a string value in Java?How to get the last value of an ArrayListInitialization of an ArrayList in one lineSort ArrayList of custom Objects by propertyConverting 'ArrayList<String> to 'String[]' in JavaConvert ArrayList<String> to String[] array

Was the dragon prowess intentionally downplayed in S08E04?

FIFO data structure in pure C

Square spiral in Mathematica

Why would you put your input amplifier in front of your filtering for and ECG signal?

Canadian citizen who is presently in litigation with a US-based company

Why are lawsuits between the President and Congress not automatically sent to the Supreme Court

How to know the path of a particular software?

Pedaling at different gear ratios on flat terrain: what's the point?

Why is vowel phonology represented in a trapezoid instead of a square?

​Cuban​ ​Primes

When did Britain learn about American independence?

Why does string strummed with finger sound different from the one strummed with pick?

How come Arya Stark didn't burn in Game of Thrones Season 8 Episode 5

SHAKE-128/256 or SHA3-256/512

Bash grep result from command whole line

Why would company (decision makers) wait for someone to retire, rather than lay them off, when their role is no longer needed?

Why is it correct to use ~た in this sentence, even though we're talking about next week?

What color to choose as "danger" if the main color of my app is red

Deleting the same lines from a list

How was the blinking terminal cursor invented?

Solenoid fastest possible release - for how long should reversed polarity be applied?

Is it standard for US-based universities to consider the ethnicity of an applicant during PhD admissions?

Why are there five extra turns in tournament Magic?

Why do galaxies collide?



Getting a wrong output using arraylists


Does a finally block always get executed in Java?Create ArrayList from arrayWhen to use LinkedList over ArrayList in Java?How do I get a consistent byte representation of strings in C# without manually specifying an encoding?How to get an enum value from a string value in Java?How to get the last value of an ArrayListInitialization of an ArrayList in one lineSort ArrayList of custom Objects by propertyConverting 'ArrayList<String> to 'String[]' in JavaConvert ArrayList<String> to String[] array






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








11















The challenge is to find a number whose individual digits multiplied by consecutively increasing power and added up, equal the initial number.



Eg: take 89, split it into 8 and 9, then 8^1 + 9^2 = 89



static List<Integer> sumDigPow(int a, int b) 
List<Integer> eureka = new ArrayList<Integer>(0);
List<String> digits = new ArrayList<String>();
String num;
int sum = 0, multi;

for (int i=a; i<=b; i++)
num = String.valueOf(i);
digits.add(num);

for (int j=0; j<digits.size(); j++)
multi = (int)Math.pow(Integer.parseInt(digits.get(j)), j+1);
sum += multi;


if (sum == i) eureka.add(i);

sum = 0;
digits.clear();


return eureka;



With an input of 1 and 100 (the range), the output should be [1, 2, 3, 4, 5, 6, 7, 8, 9, 89], but I'm getting all of the numbers [1, 2 ... 100].



I've started learning java fairly recently and can't seem to find the issue in the code. Any hints would be greatly appreciated.










share|improve this question









New contributor



PrometeusH is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

























    11















    The challenge is to find a number whose individual digits multiplied by consecutively increasing power and added up, equal the initial number.



    Eg: take 89, split it into 8 and 9, then 8^1 + 9^2 = 89



    static List<Integer> sumDigPow(int a, int b) 
    List<Integer> eureka = new ArrayList<Integer>(0);
    List<String> digits = new ArrayList<String>();
    String num;
    int sum = 0, multi;

    for (int i=a; i<=b; i++)
    num = String.valueOf(i);
    digits.add(num);

    for (int j=0; j<digits.size(); j++)
    multi = (int)Math.pow(Integer.parseInt(digits.get(j)), j+1);
    sum += multi;


    if (sum == i) eureka.add(i);

    sum = 0;
    digits.clear();


    return eureka;



    With an input of 1 and 100 (the range), the output should be [1, 2, 3, 4, 5, 6, 7, 8, 9, 89], but I'm getting all of the numbers [1, 2 ... 100].



    I've started learning java fairly recently and can't seem to find the issue in the code. Any hints would be greatly appreciated.










    share|improve this question









    New contributor



    PrometeusH is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.





















      11












      11








      11


      1






      The challenge is to find a number whose individual digits multiplied by consecutively increasing power and added up, equal the initial number.



      Eg: take 89, split it into 8 and 9, then 8^1 + 9^2 = 89



      static List<Integer> sumDigPow(int a, int b) 
      List<Integer> eureka = new ArrayList<Integer>(0);
      List<String> digits = new ArrayList<String>();
      String num;
      int sum = 0, multi;

      for (int i=a; i<=b; i++)
      num = String.valueOf(i);
      digits.add(num);

      for (int j=0; j<digits.size(); j++)
      multi = (int)Math.pow(Integer.parseInt(digits.get(j)), j+1);
      sum += multi;


      if (sum == i) eureka.add(i);

      sum = 0;
      digits.clear();


      return eureka;



      With an input of 1 and 100 (the range), the output should be [1, 2, 3, 4, 5, 6, 7, 8, 9, 89], but I'm getting all of the numbers [1, 2 ... 100].



      I've started learning java fairly recently and can't seem to find the issue in the code. Any hints would be greatly appreciated.










      share|improve this question









      New contributor



      PrometeusH is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      The challenge is to find a number whose individual digits multiplied by consecutively increasing power and added up, equal the initial number.



      Eg: take 89, split it into 8 and 9, then 8^1 + 9^2 = 89



      static List<Integer> sumDigPow(int a, int b) 
      List<Integer> eureka = new ArrayList<Integer>(0);
      List<String> digits = new ArrayList<String>();
      String num;
      int sum = 0, multi;

      for (int i=a; i<=b; i++)
      num = String.valueOf(i);
      digits.add(num);

      for (int j=0; j<digits.size(); j++)
      multi = (int)Math.pow(Integer.parseInt(digits.get(j)), j+1);
      sum += multi;


      if (sum == i) eureka.add(i);

      sum = 0;
      digits.clear();


      return eureka;



      With an input of 1 and 100 (the range), the output should be [1, 2, 3, 4, 5, 6, 7, 8, 9, 89], but I'm getting all of the numbers [1, 2 ... 100].



      I've started learning java fairly recently and can't seem to find the issue in the code. Any hints would be greatly appreciated.







      java string arraylist






      share|improve this question









      New contributor



      PrometeusH is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.










      share|improve this question









      New contributor



      PrometeusH is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.








      share|improve this question




      share|improve this question








      edited May 11 at 18:56









      Nicholas K

      8,99571839




      8,99571839






      New contributor



      PrometeusH is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.








      asked May 11 at 12:48









      PrometeusHPrometeusH

      584




      584




      New contributor



      PrometeusH is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.




      New contributor




      PrometeusH is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.
























          3 Answers
          3






          active

          oldest

          votes


















          6














          You can use the following:



          static List<Integer> sumDigPow(int a, int b) 
          List<Integer> eureka = new ArrayList<Integer>(0);
          String num;
          int sum = 0, multi;

          for (int i = a; i <= b; i++)
          num = String.valueOf(i);
          for (int j = 0; j < num.length(); j++)
          multi = (int) Math.pow(Character.getNumericValue(num.charAt(j)), j + 1);
          sum += multi;


          if (sum == i)
          eureka.add(i);

          sum = 0;

          return eureka;



          Explanation:



          1. You were not checking the second digit of the number.

          2. Loop over each character of the String num.

          3. There is no need of the digits arraylist, you can just use the numeric value of the char.





          share|improve this answer
































            4














            The problem were those lines :



            num = String.valueOf(i);
            digits.add(num);


            You did not split your number into digits. You were just putting your whole numbers into digits list. Look at this code :



            static List<Integer> sumDigPow(int a, int b) 
            List<Integer> eureka = new ArrayList<Integer>();
            List<String> digits;
            String num;
            int sum = 0, multi;

            for (int i = a; i <= b; i++)
            num = String.valueOf(i);
            digits = Arrays.asList(num.split(""));

            for (int j = 0; j < digits.size(); j++)
            multi = (int) Math.pow(Integer.parseInt(digits.get(j)), j + 1);
            sum += multi;


            if (sum == i) eureka.add(i);

            sum = 0;


            return eureka;



            I simply split your string number into digits using Arrays.asList(num.split("")). It outputs for a=1, b=100 the list :



            1
            2
            3
            4
            5
            6
            7
            8
            9
            89





            share|improve this answer

























            • That works, thank you. I used an arraylist to store the digits solely to use the .clear() method but I see it's not necessary.

              – PrometeusH
              May 11 at 13:06











            • There are more optimizations that could be made but I tried not to modify your code that much :)

              – michalk
              May 11 at 13:07


















            4














            Use char[] to split the numbers into digits as an array of characters (you are just adding the whole number as a single string to the list, not its individual digits):



            ...
            char[] digits;
            ...
            digits = String.valueOf(i).toCharArray();


            Then if you subtract '0' from each char digit you automatically get the actual int value of the digit without having to invoke the Integer.parseInt method on a String, or any other parsing method:



             (int)Math.pow(digits[j] - '0', j + 1);


            The full code would look like this:



            static List<Integer> sumDigPow(int a, int b) 
            List<Integer> eureka = new ArrayList<Integer>();
            int sum = 0;
            char[] digits;

            for (int i = a; i <= b; i++)
            digits = String.valueOf(i).toCharArray();
            for (int j = 0; j < num.length(); j++)
            sum += (int)Math.pow(digits[j] - '0', j + 1);

            if (sum == i) eureka.add(i);
            sum = 0;

            return eureka;






            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
              );



              );






              PrometeusH is a new contributor. Be nice, and check out our Code of Conduct.









              draft saved

              draft discarded


















              StackExchange.ready(
              function ()
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f56090501%2fgetting-a-wrong-output-using-arraylists%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              6














              You can use the following:



              static List<Integer> sumDigPow(int a, int b) 
              List<Integer> eureka = new ArrayList<Integer>(0);
              String num;
              int sum = 0, multi;

              for (int i = a; i <= b; i++)
              num = String.valueOf(i);
              for (int j = 0; j < num.length(); j++)
              multi = (int) Math.pow(Character.getNumericValue(num.charAt(j)), j + 1);
              sum += multi;


              if (sum == i)
              eureka.add(i);

              sum = 0;

              return eureka;



              Explanation:



              1. You were not checking the second digit of the number.

              2. Loop over each character of the String num.

              3. There is no need of the digits arraylist, you can just use the numeric value of the char.





              share|improve this answer





























                6














                You can use the following:



                static List<Integer> sumDigPow(int a, int b) 
                List<Integer> eureka = new ArrayList<Integer>(0);
                String num;
                int sum = 0, multi;

                for (int i = a; i <= b; i++)
                num = String.valueOf(i);
                for (int j = 0; j < num.length(); j++)
                multi = (int) Math.pow(Character.getNumericValue(num.charAt(j)), j + 1);
                sum += multi;


                if (sum == i)
                eureka.add(i);

                sum = 0;

                return eureka;



                Explanation:



                1. You were not checking the second digit of the number.

                2. Loop over each character of the String num.

                3. There is no need of the digits arraylist, you can just use the numeric value of the char.





                share|improve this answer



























                  6












                  6








                  6







                  You can use the following:



                  static List<Integer> sumDigPow(int a, int b) 
                  List<Integer> eureka = new ArrayList<Integer>(0);
                  String num;
                  int sum = 0, multi;

                  for (int i = a; i <= b; i++)
                  num = String.valueOf(i);
                  for (int j = 0; j < num.length(); j++)
                  multi = (int) Math.pow(Character.getNumericValue(num.charAt(j)), j + 1);
                  sum += multi;


                  if (sum == i)
                  eureka.add(i);

                  sum = 0;

                  return eureka;



                  Explanation:



                  1. You were not checking the second digit of the number.

                  2. Loop over each character of the String num.

                  3. There is no need of the digits arraylist, you can just use the numeric value of the char.





                  share|improve this answer















                  You can use the following:



                  static List<Integer> sumDigPow(int a, int b) 
                  List<Integer> eureka = new ArrayList<Integer>(0);
                  String num;
                  int sum = 0, multi;

                  for (int i = a; i <= b; i++)
                  num = String.valueOf(i);
                  for (int j = 0; j < num.length(); j++)
                  multi = (int) Math.pow(Character.getNumericValue(num.charAt(j)), j + 1);
                  sum += multi;


                  if (sum == i)
                  eureka.add(i);

                  sum = 0;

                  return eureka;



                  Explanation:



                  1. You were not checking the second digit of the number.

                  2. Loop over each character of the String num.

                  3. There is no need of the digits arraylist, you can just use the numeric value of the char.






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited May 11 at 13:12

























                  answered May 11 at 13:03









                  Nicholas KNicholas K

                  8,99571839




                  8,99571839























                      4














                      The problem were those lines :



                      num = String.valueOf(i);
                      digits.add(num);


                      You did not split your number into digits. You were just putting your whole numbers into digits list. Look at this code :



                      static List<Integer> sumDigPow(int a, int b) 
                      List<Integer> eureka = new ArrayList<Integer>();
                      List<String> digits;
                      String num;
                      int sum = 0, multi;

                      for (int i = a; i <= b; i++)
                      num = String.valueOf(i);
                      digits = Arrays.asList(num.split(""));

                      for (int j = 0; j < digits.size(); j++)
                      multi = (int) Math.pow(Integer.parseInt(digits.get(j)), j + 1);
                      sum += multi;


                      if (sum == i) eureka.add(i);

                      sum = 0;


                      return eureka;



                      I simply split your string number into digits using Arrays.asList(num.split("")). It outputs for a=1, b=100 the list :



                      1
                      2
                      3
                      4
                      5
                      6
                      7
                      8
                      9
                      89





                      share|improve this answer

























                      • That works, thank you. I used an arraylist to store the digits solely to use the .clear() method but I see it's not necessary.

                        – PrometeusH
                        May 11 at 13:06











                      • There are more optimizations that could be made but I tried not to modify your code that much :)

                        – michalk
                        May 11 at 13:07















                      4














                      The problem were those lines :



                      num = String.valueOf(i);
                      digits.add(num);


                      You did not split your number into digits. You were just putting your whole numbers into digits list. Look at this code :



                      static List<Integer> sumDigPow(int a, int b) 
                      List<Integer> eureka = new ArrayList<Integer>();
                      List<String> digits;
                      String num;
                      int sum = 0, multi;

                      for (int i = a; i <= b; i++)
                      num = String.valueOf(i);
                      digits = Arrays.asList(num.split(""));

                      for (int j = 0; j < digits.size(); j++)
                      multi = (int) Math.pow(Integer.parseInt(digits.get(j)), j + 1);
                      sum += multi;


                      if (sum == i) eureka.add(i);

                      sum = 0;


                      return eureka;



                      I simply split your string number into digits using Arrays.asList(num.split("")). It outputs for a=1, b=100 the list :



                      1
                      2
                      3
                      4
                      5
                      6
                      7
                      8
                      9
                      89





                      share|improve this answer

























                      • That works, thank you. I used an arraylist to store the digits solely to use the .clear() method but I see it's not necessary.

                        – PrometeusH
                        May 11 at 13:06











                      • There are more optimizations that could be made but I tried not to modify your code that much :)

                        – michalk
                        May 11 at 13:07













                      4












                      4








                      4







                      The problem were those lines :



                      num = String.valueOf(i);
                      digits.add(num);


                      You did not split your number into digits. You were just putting your whole numbers into digits list. Look at this code :



                      static List<Integer> sumDigPow(int a, int b) 
                      List<Integer> eureka = new ArrayList<Integer>();
                      List<String> digits;
                      String num;
                      int sum = 0, multi;

                      for (int i = a; i <= b; i++)
                      num = String.valueOf(i);
                      digits = Arrays.asList(num.split(""));

                      for (int j = 0; j < digits.size(); j++)
                      multi = (int) Math.pow(Integer.parseInt(digits.get(j)), j + 1);
                      sum += multi;


                      if (sum == i) eureka.add(i);

                      sum = 0;


                      return eureka;



                      I simply split your string number into digits using Arrays.asList(num.split("")). It outputs for a=1, b=100 the list :



                      1
                      2
                      3
                      4
                      5
                      6
                      7
                      8
                      9
                      89





                      share|improve this answer















                      The problem were those lines :



                      num = String.valueOf(i);
                      digits.add(num);


                      You did not split your number into digits. You were just putting your whole numbers into digits list. Look at this code :



                      static List<Integer> sumDigPow(int a, int b) 
                      List<Integer> eureka = new ArrayList<Integer>();
                      List<String> digits;
                      String num;
                      int sum = 0, multi;

                      for (int i = a; i <= b; i++)
                      num = String.valueOf(i);
                      digits = Arrays.asList(num.split(""));

                      for (int j = 0; j < digits.size(); j++)
                      multi = (int) Math.pow(Integer.parseInt(digits.get(j)), j + 1);
                      sum += multi;


                      if (sum == i) eureka.add(i);

                      sum = 0;


                      return eureka;



                      I simply split your string number into digits using Arrays.asList(num.split("")). It outputs for a=1, b=100 the list :



                      1
                      2
                      3
                      4
                      5
                      6
                      7
                      8
                      9
                      89






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited May 11 at 13:06

























                      answered May 11 at 13:00









                      michalkmichalk

                      1,141417




                      1,141417












                      • That works, thank you. I used an arraylist to store the digits solely to use the .clear() method but I see it's not necessary.

                        – PrometeusH
                        May 11 at 13:06











                      • There are more optimizations that could be made but I tried not to modify your code that much :)

                        – michalk
                        May 11 at 13:07

















                      • That works, thank you. I used an arraylist to store the digits solely to use the .clear() method but I see it's not necessary.

                        – PrometeusH
                        May 11 at 13:06











                      • There are more optimizations that could be made but I tried not to modify your code that much :)

                        – michalk
                        May 11 at 13:07
















                      That works, thank you. I used an arraylist to store the digits solely to use the .clear() method but I see it's not necessary.

                      – PrometeusH
                      May 11 at 13:06





                      That works, thank you. I used an arraylist to store the digits solely to use the .clear() method but I see it's not necessary.

                      – PrometeusH
                      May 11 at 13:06













                      There are more optimizations that could be made but I tried not to modify your code that much :)

                      – michalk
                      May 11 at 13:07





                      There are more optimizations that could be made but I tried not to modify your code that much :)

                      – michalk
                      May 11 at 13:07











                      4














                      Use char[] to split the numbers into digits as an array of characters (you are just adding the whole number as a single string to the list, not its individual digits):



                      ...
                      char[] digits;
                      ...
                      digits = String.valueOf(i).toCharArray();


                      Then if you subtract '0' from each char digit you automatically get the actual int value of the digit without having to invoke the Integer.parseInt method on a String, or any other parsing method:



                       (int)Math.pow(digits[j] - '0', j + 1);


                      The full code would look like this:



                      static List<Integer> sumDigPow(int a, int b) 
                      List<Integer> eureka = new ArrayList<Integer>();
                      int sum = 0;
                      char[] digits;

                      for (int i = a; i <= b; i++)
                      digits = String.valueOf(i).toCharArray();
                      for (int j = 0; j < num.length(); j++)
                      sum += (int)Math.pow(digits[j] - '0', j + 1);

                      if (sum == i) eureka.add(i);
                      sum = 0;

                      return eureka;






                      share|improve this answer





























                        4














                        Use char[] to split the numbers into digits as an array of characters (you are just adding the whole number as a single string to the list, not its individual digits):



                        ...
                        char[] digits;
                        ...
                        digits = String.valueOf(i).toCharArray();


                        Then if you subtract '0' from each char digit you automatically get the actual int value of the digit without having to invoke the Integer.parseInt method on a String, or any other parsing method:



                         (int)Math.pow(digits[j] - '0', j + 1);


                        The full code would look like this:



                        static List<Integer> sumDigPow(int a, int b) 
                        List<Integer> eureka = new ArrayList<Integer>();
                        int sum = 0;
                        char[] digits;

                        for (int i = a; i <= b; i++)
                        digits = String.valueOf(i).toCharArray();
                        for (int j = 0; j < num.length(); j++)
                        sum += (int)Math.pow(digits[j] - '0', j + 1);

                        if (sum == i) eureka.add(i);
                        sum = 0;

                        return eureka;






                        share|improve this answer



























                          4












                          4








                          4







                          Use char[] to split the numbers into digits as an array of characters (you are just adding the whole number as a single string to the list, not its individual digits):



                          ...
                          char[] digits;
                          ...
                          digits = String.valueOf(i).toCharArray();


                          Then if you subtract '0' from each char digit you automatically get the actual int value of the digit without having to invoke the Integer.parseInt method on a String, or any other parsing method:



                           (int)Math.pow(digits[j] - '0', j + 1);


                          The full code would look like this:



                          static List<Integer> sumDigPow(int a, int b) 
                          List<Integer> eureka = new ArrayList<Integer>();
                          int sum = 0;
                          char[] digits;

                          for (int i = a; i <= b; i++)
                          digits = String.valueOf(i).toCharArray();
                          for (int j = 0; j < num.length(); j++)
                          sum += (int)Math.pow(digits[j] - '0', j + 1);

                          if (sum == i) eureka.add(i);
                          sum = 0;

                          return eureka;






                          share|improve this answer















                          Use char[] to split the numbers into digits as an array of characters (you are just adding the whole number as a single string to the list, not its individual digits):



                          ...
                          char[] digits;
                          ...
                          digits = String.valueOf(i).toCharArray();


                          Then if you subtract '0' from each char digit you automatically get the actual int value of the digit without having to invoke the Integer.parseInt method on a String, or any other parsing method:



                           (int)Math.pow(digits[j] - '0', j + 1);


                          The full code would look like this:



                          static List<Integer> sumDigPow(int a, int b) 
                          List<Integer> eureka = new ArrayList<Integer>();
                          int sum = 0;
                          char[] digits;

                          for (int i = a; i <= b; i++)
                          digits = String.valueOf(i).toCharArray();
                          for (int j = 0; j < num.length(); j++)
                          sum += (int)Math.pow(digits[j] - '0', j + 1);

                          if (sum == i) eureka.add(i);
                          sum = 0;

                          return eureka;







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited May 11 at 16:41

























                          answered May 11 at 13:04









                          Marco R.Marco R.

                          398211




                          398211




















                              PrometeusH is a new contributor. Be nice, and check out our Code of Conduct.









                              draft saved

                              draft discarded


















                              PrometeusH is a new contributor. Be nice, and check out our Code of Conduct.












                              PrometeusH is a new contributor. Be nice, and check out our Code of Conduct.











                              PrometeusH is a new contributor. Be nice, and check out our Code of Conduct.














                              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%2f56090501%2fgetting-a-wrong-output-using-arraylists%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

                              Get product attribute by attribute group code in magento 2get product attribute by product attribute group in magento 2Magento 2 Log Bundle Product Data in List Page?How to get all product attribute of a attribute group of Default attribute set?Magento 2.1 Create a filter in the product grid by new attributeMagento 2 : Get Product Attribute values By GroupMagento 2 How to get all existing values for one attributeMagento 2 get custom attribute of a single product inside a pluginMagento 2.3 How to get all the Multi Source Inventory (MSI) locations collection in custom module?Magento2: how to develop rest API to get new productsGet product attribute by attribute group code ( [attribute_group_code] ) in magento 2

                              Category:9 (number) SubcategoriesMedia in category "9 (number)"Navigation menuUpload mediaGND ID: 4485639-8Library of Congress authority ID: sh85091979ReasonatorScholiaStatistics

                              Magento 2.3: How do i solve this, Not registered handle, on custom form?How can i rewrite TierPrice Block in Magento2magento 2 captcha not rendering if I override layout xmlmain.CRITICAL: Plugin class doesn't existMagento 2 : Problem while adding custom button order view page?Magento 2.2.5: Overriding Admin Controller sales/orderMagento 2.2.5: Add, Update and Delete existing products Custom OptionsMagento 2.3 : File Upload issue in UI Component FormMagento2 Not registered handleHow to configured Form Builder Js in my custom magento 2.3.0 module?Magento 2.3. How to create image upload field in an admin form