In order to figure what is wrong I would probably have to do it by hand or wth ?
Here are my results after compilation
[1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407]
memory 256
time = 0
[1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407, 1634, 8208, 9474, 54748, 92727, 93084, 548834]
memory 4458
time = 0
package com.codegym.task.task20.task2025;
import java.util.ArrayList;
import java.util.Arrays;
/*
Number algorithms
*/
public class Solution {
public static long[] getNumbers(long N) {
long[] result = null;
ArrayList<Integer> digits = new ArrayList<>();
ArrayList<Long> correctNumbers = new ArrayList<>();
//get the number of digits
for (long i = 1; i < N; i++) {
char[] number = String.valueOf(i).toCharArray();
//get each individual digit and add it to the array, backwards
for (int j = 0; j < number.length; j++) {
digits.add(Character.getNumericValue(number[j]));
}
// now i need to check if the number is equal to the sum of all of
// its digits to the power of the number of digits
long sumOfDigit;
long finalSum = 0;
for (int h = 0; h < digits.size(); h++) {
sumOfDigit = 1;
for (int j = 0; j < digits.size(); j++) {
sumOfDigit = sumOfDigit * digits.get(h);
}
finalSum += sumOfDigit;
}
if (i == finalSum) correctNumbers.add(i);
digits.clear();
}
result = new long[correctNumbers.size()];
for (int i = 0; i < correctNumbers.size(); i++){
result[i] = correctNumbers.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);
}
}