Looks like the calculation part is ok but it won't finish in time (or to much memory).
Some other way to bypass looking at all the numbers I didn't see?
package com.codegym.task.task20.task2025;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
/*
Number algorithms
*/
public class Solution {
public static HashMap<String, Double> hm = new HashMap<>();
public static long[] getNumbers(long N) {
ArrayList<Long> al = new ArrayList<>();
try
{
for (long i = 0; i <= N; i++)
{
String s = String.valueOf(i);
int partSum = 0;
for (Character ch : s.toCharArray())
{
Long l = Long.valueOf(ch.toString());
partSum += power(l, s.length());
if (partSum > i)
{
break;
}
}
if (partSum == i)
{
//System.out.println("Yes " + partSum);
al.add(i);
}
else
{
//System.out.println("No " + partSum);
}
}
}
catch (NumberFormatException e)
{
e.printStackTrace();
}
System.out.print(al);
return al.stream().mapToLong(l -> l).toArray();
}
static double power(double d, int n){
String key = d + " " + n;
if (hm.containsKey(key))
{
return hm.get(key);
}
double p=1;
for(int i=0;i<n;i++)
p=p*d;
hm.put(key, p);
return(p) ;
}
public static void main(String[] args) {
getNumbers(375);
//getNumbers(0);
//getNumbers(372);
//getNumbers(8208);
}
}