Actually I am getting sharp result. It works fast and doesn't take much memory, but during the verification it hangs - "The program ran too long and was closed!"

package com.codegym.task.task20.task2025;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/*
Number algorithms
*/
public class Solution {
public static long[] getNumbers(long N) {
long[] result;
List<Integer> digits = new ArrayList<>();
List<Long> results = new ArrayList<>();
for (long i = 0; i < N; i++) {
long toSplit = i;
int count = 0;
while (toSplit > 0) {
digits.add((int) toSplit % 10); //split into digits and store them into List
toSplit /= 10;
count++; //get number of digits and needed power number
}
long sum = 0;
for (int j = 0; j < count; j++) {
sum += Math.pow(digits.get(j), count); // make this : 3*3*3 + 7*7*7 + 0*0*0
}
if (sum == i) {
results.add(i); // check if this is true 370 == 3*3*3 + 7*7*7 + 0*0*0 and add it to List
}
digits.clear();
}
result = new long[results.size()];
for (int i = 0; i < result.length; i++) {
result[i] = results.get(i);
}
return result;
}
public static void main(String[] args) {
//getNumbers(10000);
long a = System.currentTimeMillis();
System.out.println(Arrays.toString(getNumbers(9223372L)));
long b = System.currentTimeMillis();
System.out.println("memory " + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / (8 * 1024));
System.out.println("time = " + (b - a) / 1000);
a = System.currentTimeMillis();
System.out.println(Arrays.toString(getNumbers(1000000)));
b = System.currentTimeMillis();
System.out.println("memory " + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / (8 * 1024));
System.out.println("time = " + (b - a) / 1000);
}
}

{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}numbers 10-99 hold each digits pow(#, 2){0, 1, 4, 9, 16, 25, 36, 49, 64, 81}numbers 100-999 each digits pow(#,3){0, 1, 8, 27, 64, 125, 216, 343, 512, 729}etc... Always getting the digit,using pow(), adding the results, then checking the number is slow. If you only use pow() one time then just keep reusing the stored results the algorithm speeds up considerably #2 I have a second long Array that starts at length one, then resizes to add one index every time the number goes up a digit. I store the running results of each digit of the current number in this array, with the left most index matching up to the left most digit in the test number. So take 370 as an example. This array stores: index 0 index 1 index2 {pow(3,3) = 27, index[0] + pow(7,3) = 370, index[1] + pow(0,3) = 370} The right most index will store the total result. Now, the thing about this is when we move to 371 all you have to do is replace the right most index with the result of index[i-1] + pow(1,3). By only doing new math on the new digits in the number effectively caches the results and saves a ton of cpu cycles. On numbers you could either do 1 math operation per 1 number increase or number.length math operations per 1 number increase.