My solution seemingly produces correct results, but it is not accepted because of "time out".
If I look at the reference solution my only question is: how on earth am I supposed to EVER or
in FINITE TIME get to such a solution?
I am not a graduated student of Information Sciences, I'm just a beginner and I cannot imagine
how to google myself into the right direction - or even come close to the reference solution.
As I'm advancing this kind of unsolvable tasks happen more and more often, and my only
chance is to look up the reference solution. Of which I learn... not very much, sorry.
I could as well look up the solution right away if I encounter a "hard" task. How much will I learn?
Or is *this* the way of CodeGym to tell me I should quit trying/struggling to become a programmer?
Frustration is one thing, constant failure is another.
package de.codegym.task.task20.task2025;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/*
Zahlenalgorithmen
*/
public class Solution {
public static long[] getNumbers(long N) {
List<Long> results = new ArrayList<>();
for (long i = 2; i < N; i++) {
int cnt = countCiphers(i);
long sum = 0;
long cipher;
long divisionResult = i;
for (int j = 0; j < cnt; j++) {
cipher = divisionResult % 10;
sum += (long) Math.pow(cipher, cnt);
divisionResult /= 10;
}
if (i == sum) {
results.add(i);
}
}
long[] result = new long[results.size()];
for (int i = 0; i < results.size(); i++) {
result[i] = results.get(i);
}
return result;
}
private static int countCiphers(long n) {
return (int) Math.ceil(Math.log10(n));
}
public static void main(String[] args) {
/*System.out.println(countCiphers(1));
System.out.println(countCiphers(10));
System.out.println(countCiphers(999));*/
long a = System.currentTimeMillis();
System.out.println(Arrays.toString(getNumbers(1000)));
long b = System.currentTimeMillis();
System.out.println("Speicherplatz " + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / (8 * 1024));
System.out.println("Zeit = " + (b - a) / 1000);
a = System.currentTimeMillis();
System.out.println(Arrays.toString(getNumbers(1000000)));
b = System.currentTimeMillis();
System.out.println("Speicherplatz " + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / (8 * 1024));
System.out.println("Zeit = " + (b - a) / 1000);
}
}