After few hours on that I have to pass. Can anybody tell me what's wrong with my solution, apart that it simply do not pass? Please. Thanks in advance.
package com.codegym.task.task20.task2025;
import java.util.*;
/*
Number algorithms
*/
public class Solution {
public static ArrayList<Long> list; //synchronizacja
private static long[] pows = new long[10];
private static long minDecNumber;
private static long maxDecNumber;
private static long liczbaN;
private static boolean search() {
pows[2] = pows[2] * 2;
pows[3] = pows[3] * 3;
pows[4] = pows[4] * 4;
pows[5] = pows[5] * 5;
pows[6] = pows[6] * 6;
pows[7] = pows[7] * 7;
pows[8] = pows[8] * 8;
pows[9] = pows[9] * 9;
minDecNumber *= 10;
maxDecNumber *= 10;
if (maxDecNumber < liczbaN) {
for (long liczba = minDecNumber; liczba < maxDecNumber; liczba++) {
long x = liczba, suma = 0;
while (x > 0) {
suma += pows[(int) (x % 10)];
x /= 10;
}
if (suma == liczba)
list.add(suma);
}
return true;
} else {
for (long liczba = minDecNumber; liczba < liczbaN; liczba++) {
long x = liczba, suma = 0;
while (x > 0) {
suma += pows[(int) (x % 10)];
x /= 10;
}
if (suma == liczba) list.add(suma);
}
return false;
}
}
public static long[] getNumbers(long N) {
pows = new long[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
minDecNumber = 1;
maxDecNumber = 10;
list = new ArrayList<>();
if (N > 1) list.add(1l);
if (N > 2) list.add(2l);
if (N > 3) list.add(3l);
if (N > 4) list.add(4l);
if (N > 5) list.add(5l);
if (N > 6) list.add(6l);
if (N > 7) list.add(7l);
if (N > 8) list.add(8l);
if (N > 9) list.add(9l);
if (N > 10) {
liczbaN = N;
int i = 2;
while (search()) {}
Collections.sort(list);
}
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(8208)));
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(1_000_000)));
b = System.currentTimeMillis();
System.out.println("memory " + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / (8 * 1024));
System.out.println("time = " + (b - a) / 1000);
}
}