В списке значений не хватает 3 чисел в конце списка(причем не подряд), не могу понять почему их пропускает.
[1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407, 1634, 8208, 9474, 54748, 92727, 93084, 548834, 1741725, 4210818, 9800817, 9926315, 24678050, 24678051, 88593477, 146511208, 472335975, 534494836, 912985153, 4679307774, 32164049650, 32164049651, 40028394225, 42678290603, 44708635679, 49388550606, 82693916578, 94204591914, 28116440335967, 4338281769391370, 4338281769391371, 21897142587612075, 35641594208964132, 35875699062250035, 4498128791164624869]
memory 3633
time = 20
Недостающие значения:
.. 1517841543307505039L, 3289582984443187032L, .., 4929273885928088826L
package com.javarush.task.task20.task2025;
import java.util.*;
import java.util.stream.Collectors;
/*
Алгоритмы-числа
*/
public class Solution {
static long[][] stepen;
public static long[] getNumbers(long N) {
long[] result = null;
TreeSet list=new TreeSet();
if(N<=0) return new long[0] ;
String number = String.valueOf(N);
int m=number.length();
stepen=new long[10][20];
for (int i = 0; i <= 9; i++) {
long p=1;
for (int j = 0; j <= 19; j++) {
stepen[i][j]=p;
p*=i;
}
}
int[] chislo=new int[m];
Arrays.fill(chislo,9);
long temp=0;
int k=0;
while(chislo[m-1]>0) {
while (chislo[k] >0) {
temp = stephenResult(arraytolong(chislo), (String.valueOf(arraytolong(chislo))).length());
if(temp>N||temp<0) {
break;
}
long chislotempresdult = stephenResult(temp, String.valueOf(temp).length());
if (temp == chislotempresdult)
list.add(temp);
if (chislo[k] != 0) chislo[k]--;
else break;
}
long nulls= Arrays.stream(chislo).filter(x->x==0).count();
for (int i = m-1-(int)nulls; i <= m-1; i++) {
temp = stephenResult(arraytolong(chislo), i);
if(temp>N||temp<0) {
continue;
}
long chislotempresdult = stephenResult(temp, String.valueOf(temp).length());
if (temp == chislotempresdult&&temp>0)
list.add(temp);
}
for (int i = 0; i < m; i++) {
if(chislo[i]>0){
Arrays.fill(chislo, 0, i + 1, chislo[i] - 1);
k=0;
break;
}
}
}
result=new long[list.size()];
for (int i = 0; i < result.length; i++) {
result[i] = (long) list.first();
list.remove(result[i]);
}
return result;
}
public static void main(String[] args) {
long a = System.currentTimeMillis();
System.out.println(Arrays.toString(getNumbers(Long.MAX_VALUE)));
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(10000)));
b = System.currentTimeMillis();
System.out.println("memory " + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / (8 * 1024));
System.out.println("time = " + (b - a) / 1000);
}
public static long stephenResult(long lo,int l){
if(lo<0) return -1;
int[]n=spisok(lo);
long temp=0;
for (int c: n
) {
temp+=stepen[c][l];
if(temp<0) return -1;
}
return temp;
}
public static int[] spisok(long n){
String temps=String.valueOf(n);
int[] chisloTemp=new int[temps.length()];
for (int c=0; c<temps.length(); c++)
{
chisloTemp[c]=Integer.parseInt(""+temps.charAt(c));
}
return chisloTemp;
}
public static long arraytolong(int[] n){
long templo=0;
String temps="";
for (int i: n
) {
temps+=i;
}
try{
templo=Long.valueOf(temps);
}
catch (NumberFormatException e){return -1;}
return templo;
}
}