Last active
December 30, 2015 17:38
-
-
Save iamkevinlowe/23688f2223def21c7ec6 to your computer and use it in GitHub Desktop.
Revisions
-
iamkevinlowe revised this gist
Dec 30, 2015 . 1 changed file with 17 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -13,4 +13,21 @@ def decompose(n, area = n**2, squares = "", results = []) end end results end # Solution 2 def decompose(n) squares = (1..n-1).map { |i| i**2 }.reverse find_squares(squares, squares.length - 1, n**2,) end def find_squares(squares, index, sum, solution = "", results = []) if sum == 0 results << solution.strip.split(' ').map(&:to_i) elsif index >= 0 && sum > 0 find_squares(squares, index-1, sum, solution, results) find_squares(squares, index-1, sum -= squares[index], "#{solution} #{Math.sqrt(squares[index])}", results) end results end -
iamkevinlowe revised this gist
Dec 30, 2015 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,5 @@ # http://www.codewars.com/kata/54eb33e5bc1a25440d000891/train/ruby def decompose(n, area = n**2, squares = "", results = []) if area == 0 results << squares.strip.split(' ').map(&:to_i) -
iamkevinlowe renamed this gist
Dec 30, 2015 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
iamkevinlowe created this gist
Dec 30, 2015 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,14 @@ def decompose(n, area = n**2, squares = "", results = []) if area == 0 results << squares.strip.split(' ').map(&:to_i) elsif x = Math.sqrt(area).floor x -= 1 while x >= n x.downto(1) do |i| next if squares.strip.split(' ').map(&:to_i).include? i decompose(i, area - i**2, "#{i} #{squares}", results) end end results end