Числа найдены, но валидатор уверен что не выполнен последний пункт.
Вывод программы:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407]
memory 368
time = 0
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407, 1634, 8208, 9474, 54748, 92727, 93084, 548834]
memory 17523
time = 0
package com.javarush.task.task20.task2025;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
/*
Алгоритмы-числа
*/
public class Solution {
public static long[] getNumbers(long N) {
ArrayList<Long> list = new ArrayList<>();
long max = N;
for (long a = 0; a < max; a++) {
String number = String.valueOf(a);
long length = number.length();
char[] strToArray = number.toCharArray();
long sum = 0;
for (int b = 0; b < length; b++) {
long chr = pow(Long.parseLong(String.valueOf(strToArray[b])), length);
if (sum == 0) {
sum = chr;
} else {
sum = sum + chr;
}
}
if (sum == a) {
list.add(a);
}
}
long[] result = new long[list.size()];
for (int i = 0; i < list.size(); i++) {
result[i] = list.get(i);
}
return result;
}
public static void main(String[] args) {
long a = System.currentTimeMillis();
System.out.println(Arrays.toString(getNumbers(1000)));
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);
}
public static int pow(long value, long powValue) {
return (int) Math.pow(value, powValue);
}
}