package com.codewars;

import java.util.LinkedHashMap;
import java.util.Map;

public class SumSquaredDivisors {

    public static void main(String[] args) {
        //listSquared(1, 250) --> [[1, 1], [42, 2500], [246, 84100]]
        //listSquared(42, 250) --> [[42, 2500], [246, 84100]]

        System.out.println(listSquared(1, 250));
    }

    public static String listSquared(long m, long n) {
        Map<Long, Long> squaredDivisors = new LinkedHashMap<>();
        StringBuilder s = new StringBuilder("[");

        for (long i = m; i < n; i++) {

            double divisorSum = 0;
            for (long j = 1; j < i+1; j++) {
                if (i % (j) == 0) {
                    divisorSum += Math.pow(j, 2);
                }
            }

            if (Math.sqrt(divisorSum) % 1 == 0) {
                squaredDivisors.put(i, (long) divisorSum);
            }

        }

        if (squaredDivisors.size() == 0) {
            return "[]";
        }

        Long[] divisors = squaredDivisors.keySet().toArray(new Long[0]);
        for (int i = 0; i < squaredDivisors.size() - 1; i++) {
            s.append("["+ divisors[i]+", "+squaredDivisors.get(divisors[i]) + "], ");
        }
        s.append("["+ divisors[divisors.length-1]+", "+squaredDivisors.get(divisors[divisors.length-1]) + "]");
        return s.toString() + "]";
    }
}
