So i thought it works for me but it doesn't let me pass.
Looking at various internet sides etc. it seems that some people do not think 3-9 are so called Armstrong numbers:
https://pages.mtu.edu/~shene/COURSES/cs201/NOTES/chap04/arms.html
My output:
4
[1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407]
memory 491
time = 0
7
[1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407, 1634, 8208, 9474, 54748, 92727, 93084, 548834]
memory 3115
time = 0
But for single digits (>0) it's always x^1 = x.
What am i missing here?
package com.codegym.task.task20.task2025;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
/*
Number algorithms
*/
public class Solution {
public static long[] getNumbers(long N) {
ArrayList<Long> chosen = new ArrayList<>();
for(long count = 1; count < N; count++){
String s = Long.toString(count);
char[] digits = s.toCharArray();
long total =0;
for(char entry : digits){
if(total >count){break;}
int temp = Character.getNumericValue(entry);
if(temp != 0){
total += Math.pow(temp, digits.length);
}
}
if(total == count){
chosen.add(count);
}
}
long[] result = new long[chosen.size()];
for(int i = 0; i< chosen.size(); i++){
result[i] = chosen.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);
}
}