Memory taken is 28mb max
Time is 0s...
My code provides the correct answer (based on the correct armstrong number)
package com.codegym.task.task20.task2025;
import java.util.*;
/*
Number algorithms
*/
public class Solution {
public static long[] getNumbers(long N) {
long[] result = null;
ArrayList<Long> list = new ArrayList();
Map<Integer, Long[]> map = getMap(N);
String curr = "";
long total = 0;
int base;
Long[] temp = null;
for(long i = 1; i < N; i++){
curr = Long.toString(i);
for(int j = 0; j < curr.length(); j++){
base = Character.getNumericValue(curr.charAt(j));
temp = map.get(base);
total += (long) temp[curr.length()];
}
if(total == i){
list.add(i);
}
total = 0;
}
result = new long[list.size()];
for(int z = 0; z < list.size(); z++){
result[z] = list.get(z);
}
return result;
}
// 9
public static Map<Integer,Long[]> getMap(long n){
Map<Integer, Long[]> map = new HashMap();
// For every number from 0 - 9
// { 0 : [] }
int power = Long.toString(n).length();
// 370 => 3
for(int i = 0; i <= 9; i++){
Long[] temp = new Long[power + 1];
for(int j = 1; j <= power; j++){
temp[j] = new Long(Math.round(Math.pow(i, new Long(j))));
}
map.put(i, temp);
}
return map;
}
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(String.format("%d, %d", a, b));
// only 28 MB !!!
// System.out.println(String.format("%d", Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()));
System.out.println("memory " + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / (8 * 1024));
System.out.println("time = " + (b - a) / 1000);
}
}