not sure if i can pass this one, because it keeps timing out.
package com.codegym.task.task20.task2025;
import java.util.*;
/*
Number algorithms
*/
public class Solution {
public static long[] getNumbers(long n) {
if (n <= 1) return new long[0];
check(n);
List<Long> list = new ArrayList<>();
for (int i = 1; i < n; ++i) {
int m = (int) (Math.floor(Math.log10(i)) + 1);
long x = i, sum = 0;
while (x > 0) {
long r = x % 10;
sum += Math.pow(r, m);
x /= 10;
}
if (sum == i) list.add(sum);
}
return Arrays.stream(list.toArray(new Long[0])).mapToLong(Long::longValue).toArray();
}
private static void check(long n) {
int m = (int) (Math.floor(Math.log10(n)) + 1);
if (m >= 21) throw new IllegalArgumentException();
}
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);
}
}