(ns test-p.core
  (:require [clojure.math.numeric-tower:as math])
  (:require [clojure.data.int-map:as i]))

(defn solve-power-sum [max-value max-sols]
  " Finds solutions by using method approach of EchoLisp
    Large difference is we store a dictionary of all combinations 
    of y^5 - x^5 with the x, y value so we can simply lookup rather than have to search "
  (let [pow5 (mapv #(math/expt % 5) (range 0 (* 4 max-value)))                  
        y5-x3 (into (i/int-map) (for [x (range 1 max-value)                     
                                      y (range (+ 1 x) (* 4 max-value))]        
                                  [(- (get pow5 y) (get pow5 x)) [x y]])) 
        solutions-found (atom 0)]

    (for [x0 (range 1 max-value)                                    
          x1 (range 1 x0)
          x2 (range 1 x1)
         :when (< @solutions-found max-sols)
         :let [sum (apply + (map pow5 [x0 x1 x2]))]         
         :when (contains? y5-x3 sum)]                       
      (do
        (swap! solutions-found inc)                          
        (concat [x0 x1 x2] (get y5-x3 sum))))))              


                                                            
(println (into #{} (map sort (solve-power-sum 250 1))))   
(println (into #{} (map sort (solve-power-sum 1000 1000))))