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