I don't really understand the result Guadalupe posted on Bill's question.
I found a resource here: https://github.com/shamily/ArmstrongNumbers that should be fast enough but I can't understand his process either.
Some guidance would be appreciated.
package com.codegym.task.task20.task2025;
import java.util.Arrays;
import java.util.List;
import java.util.ArrayList;
/*
Number algorithms
*/
public class Solution {
public static long[] getNumbers(long N) {
List<Long> list = new ArrayList<>();
long[] powers = {0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L};
int magnitude = 10;
//go through each number and add to list if meets criteria
for (long i = 0; i < N; i++) {
if (i == magnitude) {
magnitude *= 10;
for (int tenths = 1; tenths < 10; tenths++) {
powers[tenths] *= tenths;
}
}
long number = i, sum = 0;
while (number > 0) {
int mod = (int)number % 10;
sum += powers[mod];
if (sum > i) break; //break out of the loop early if the number is already too large to be equal
number /= 10;
}
if (sum == i) list.add(i);
}
//create the array and add each element from the list to it
int listSize = list.size();
long[] result = new long[listSize];
short i = 0;
for (long l : list) {
result[i++] = l;
}
return result;
}
public static void main(String[] args) {
long startTime = System.currentTimeMillis();
System.out.println(Arrays.toString(getNumbers(100_000_000L)));
long endTime = System.currentTimeMillis();
System.out.println("Time: " + (endTime - startTime));
}
}