Hello guys,
I need your help once again. I successfully execute my code but when attemting to validate it, I get a "program ran too long" message. I need your help to debug my code.
Thanks in advance.
package fr.codegym.task.task20.task2025;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/*
Algorithmes numériques
*/
public class Solution {
public static long[] getNumbers(long N) {
if(N < 1) throw new IllegalArgumentException();
int i = String.valueOf(N).length();
byte[] digits = new byte[i];
List<Long> temp = new ArrayList<>();
for (int j = 1; j < N; j++) {
int k = 0;
int l = j;
while (l > 0) {
digits[k++] = (byte) (l % 10);
l = l / 10;
}
long som = 0;
for (i = 0; i < k; i++) {
som += (long)Math.pow(digits[i], k);
}
if (som == j) {
temp.add(som);
}
}
long[] result = new long[temp.size()];
i = 0;
for (long x : temp) {
result[i++] = x;
}
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("mémoire " + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / (8 * 1024));
System.out.println("temps = " + (b - a) / 1000);
a = System.currentTimeMillis();
System.out.println(Arrays.toString(getNumbers(1000000)));
b = System.currentTimeMillis();
System.out.println("mémoire " + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / (8 * 1024));
System.out.println("temps = " + (b - a) / 1000);
}
}