it grabs the correct Armstrong numbers when you run it, but times out on verification.
[1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407]
memory 512
time = 0
[1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407, 1634, 8208, 9474, 54748, 92727, 93084, 548834]
memory 19184
time = 0

package com.codegym.task.task20.task2025;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
/*
Number algorithms
*/
public class Solution {
public static long[] getNumbers(long N) {
List<Long> longsList = new ArrayList<>();
for (long i = N; i > 0; i--) {
long S = 0;
String strNum = Long.toString(i);
int M = strNum.length();
String[] longArray = strNum.split("(?<=\\d)");
for (String x : longArray) {
S += (long) Math.pow(Double.parseDouble(x), M);
}
if (S == i) {
longsList.add(S);
}
}
Collections.sort(longsList);
return longsList.stream().mapToLong(Long::longValue).toArray();
}
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);
}
}

#1working with Strings is very slow. You should immediately convert to a number and only use numbers when doing math.#2 line 21: you get a new value ofMfor every single number, however the numbers 1-9 M will be 1 - the numbers 10-99 M will be 2 - the number 100-999 M will be 3. That means you need to change the value of M only three times for the numbers 1-999 instead of nine hundred and ninety nine times. If you went to the max of a long you would only need to change N 25 times.#3 lines 23-25: every time M changes there are only 10 possible outcomes for line 24. For example, if you had a number that was 4 digits long the 10 possible outcomes (actually 8 because the digit 0 is still 0 and 1 is still 1 no matter what M is) for line 24 would be: 0: 0 1: 1 2: 16 3: 81 4: 256 5: 625 6: 1296 7: 2401 8: 4096 9: 6561 Right now your code gets each number, breaks it into each digit, and uses Math.pow() on each of those. By just saving the results ofMath.pow(x, M)(where x is the each number 0-9) then recalling the saved value for each digit saves a TON of time. I will include some example code to break this down.startNumberto be whatever positive multiple of 10 you want (eg, 1, 10, 100, 1000 etc) to see different results Lines 7-16: This is your abrreviated aglorithm that turns each number to a String, gets a value for M, gets each digit, converts back to a number, does Math.pow(digit, m). Lines 22-29: This algorithm works the same but doesn't use Strings, and also doesn't get a new value of M for each new number. Lines 40-47: And finally this uses all the tips that I gave. The results when I run this code are:Original algorithm Time taken with original algorithm - 7832ms Working with a number instead of Strings Time taken to use Math.pow() - 3032ms Now using Hashed values [0, 1, 128, 2187, 16384, 78125, 279936, 823543, 2097152, 4782969] Time taken using hashed values - 94msBy not using Strings it doubles the speed of your algorithm. Hashing values instead of calling Math.pow() on every single digit makes it roughly 83 times faster.