If I run my solution w/o verification it returns the arrays from the Solution input w/o a problem. However, whenever I run the validation, it states that it looks like I have an infinite loop. What gives?
package com.codegym.task.task20.task2025;
import java.util.Arrays;
import java.util.*;
/*
Number algorithms
*/
public class Solution {
public static long[] getNumbers(long N) {
if (N < 1) {
return null;
}
ArrayList<Long> array = new ArrayList<Long>();
for (Integer i = 1; i < N; i++) {
String[] nums = i.toString().split("");
long length = nums.length;
long sum = 0;
for (int j = 0; j < length; j++) {
sum += Math.pow(Long.parseLong(nums[j]), length);
}
if (sum == i) {
array.add(sum);
}
}
long[] result = new long[array.size()];
for (int s = 0; s < array.size(); s++) {
result[s] = array.get(s);
}
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);
}
}