Clojure solution to HackerRank Challenge 'Restaurant' under Number Theory My main concern is how messy the min-number-of-slices function is.  I would appreciate help finding good ways to clean that up since it has a lot of nesting/scope.

This is my first ever Clojure program, so I know this is probably not idiomatic Clojure, nor is it neat, nor probably fast, either.  Please give as much feedback as you would like on my code (especially regarding stuff that's bad about it).

The challenge is, given a list of 2D dimensions for pieces of bread, find the minimum number of perfect squares you could cut out of each piece without wasting any bread.
https://www.hackerrank.com/challenges/restaurant

(ns clojure-solution.core)
(require '[clojure.string :as string])

(defn parse-int [s]
  (Integer. s))

(defn parse-line [s]
  (map parse-int (string/split s #" ")))

(defn get-loaf-dimensions []
  (let [numLoaves (parse-int (read-line))]
    (map #(%1) (repeat numLoaves (comp parse-line read-line)))))

(def squares (map #(* % %) (range 1 1001)))

(defn min-number-of-slices [dims]
  (let [l              (first dims)
        b              (second dims)
        area           (* l b)]
    (letfn [(perfect-slice-dimension? [slice-area]
              (= 0 (+ (mod area slice-area)
                      (mod b (int (Math/sqrt slice-area)))
                      (mod l (int (Math/sqrt slice-area))))))]
      (let [largest-square (last (filter perfect-slice-dimension? (take (min l b) squares)))]
        (int (/ area largest-square))))))

(defn -main [&amp; args] (dorun (map println (map min-number-of-slices (get-loaf-dimensions)))))
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;