Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting a wrong output using arraylists

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.

like image 940
PrometeusH Avatar asked May 11 '19 12:05

PrometeusH


People also ask

Are ArrayLists fixed?

ArrayList's size and capacity are not fixed. The logical size of the list changes based on the insertion and removal of elements in it.

Are ArrayLists efficient?

ArrayList is a highly optimized and very efficient wrapper on top of a plain Java array. A small timing overhead comes from copying array elements, which happens when the allocated size is less than required number of elements.

Do ArrayLists take up more memory?

So ArrayList requires more memory consumption than simple Arrays, but you can continue to use then in small programs that wont make much of a difference but when dealing with large ammout of data and performance issues, if you can go with simple arrays dont use ArrayList as Arrays are much faster.

Are ArrayLists less efficient than arrays?

An array is faster and that is because ArrayList uses a fixed amount of array. However when you add an element to the ArrayList and it overflows. It creates a new Array and copies every element from the old one to the new one.


2 Answers

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.
like image 183
Nicholas Kurian Avatar answered Oct 14 '22 22:10

Nicholas Kurian


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;
}
like image 36
Marco R. Avatar answered Oct 14 '22 23:10

Marco R.