Created
March 14, 2016 18:57
-
-
Save hoffination/ecdcbb0e60934175ecc9 to your computer and use it in GitHub Desktop.
Evaluate a pile of cubes
This file contains 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 characters
;; Link to KATA: http://www.codewars.com/kata/5592e3bd57b64d00f3000047/train/clojure | |
;; Your task is to construct a building which will be a pile of n cubes. The cube at the bottom will have a volume of n^3, the cube above will have volume of (n-1)^3 and so on until the top which will have a volume of 1^3. | |
;; You are given the total volume m of the building. Being given m can you find the number n of cubes you will have to build? | |
;; The parameter of the function findNb (find_nb, find-nb) will be an integer m and you have to return the integer n such as n^3 + (n-1)^3 + ... + 1^3 = m if such a n exists or -1 if there is no such n. | |
;; EXAMPLES | |
;; findNb(1071225) --> 45 | |
;; findNb(91716553919377) --> -1 | |
;; BREAKS ON: expected: (= (find-nb 10252519345963644753026N) -1) - actual: (not (= 450010 -1)) | |
;; My Solution | |
(defn find-nb [goal] | |
(loop [n 0] | |
(cond | |
(= (double (+' (*' n n) n)) (*' (Math/sqrt goal) 2)) | |
n | |
(> (+' (*' n n) n) (*' (Math/sqrt goal) 2)) | |
-1 | |
:else | |
(recur (+ n 1)) | |
) | |
) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment