Clojure solution to HackerRank Challenge 'Restaurant' under Number Theory
You've implemented a brute-force solution, trying every square dimension from 1 up to the minimum of l, b, and 1000.

The optimal square size is simply the greatest common divisor of l and b.  A very simple algorithm to calculate the GCD is the Euclidean algorithm.

(defn gcd [a b]
      (if (= b 0) 
          a 
          (gcd b (mod a b))))


Calculating the minimum number of slices follows easily.

(defn min-number-of-slices [l b]
      (let [side (gcd l b)]
           (/ (* l b) (* side side))))




You can try calling min-number-of-slices in this ClojureScript demo:



.code { font-family: Monaco, monospace; font-size: 10pt; } #log { height: 10em; overflow: auto; white-space: pre-wrap; } #input { width: 100%; height: 12em; }
&lt;script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"&gt;&lt;/script&gt;&lt;script type="text/javascript" src="https://kanaka.github.io/clojurescript/web/vendor/jq-console/jqconsole.min.js"&gt;&lt;/script&gt;&lt;!-- script type="text/javascript" src="https://cdn.rawgit.com/google/closure-library/master/closure/goog/base.js"&gt;&lt;/script --&gt;&lt;script type="text/javascript" src="https://kanaka.github.io/clojurescript/web/out/goog/base.js"&gt;&lt;/script&gt;&lt;script type="text/javascript" src="https://kanaka.github.io/clojurescript/web/webrepl.js"&gt;&lt;/script&gt;&lt;script type="text/javascript"&gt;goog.require('webrepl');&lt;/script&gt;
&lt;div id="log" class="code"&gt;&lt;/div&gt;&lt;textarea id="input" class="code"
&gt; (defn gcd [a b]
        (if (= b 0) 
            a             
            (gcd b (mod a b))))

  (defn min-number-of-slices [l b]  
        (let [side (gcd l b)]             
             (/ (* l b) (* side side))))

  (min-number-of-slices 6 9)
&lt;/textarea&gt;