Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (defn parse-int [s]
- (Integer. (re-find #"\d+" s )))
- (defn mult [x y]
- "Умножение в столбик двух чисел в виде строки. На выход: число в виде строки.
- Отрицательные числа пока не перемножаются"
- (let [x (map #(parse-int (str %)) x)
- y (map #(parse-int (str %)) (reverse y))
- leftover (atom 0)
- result (atom 0)
- summarize-it (atom [])
- multiple (fn [unit-of-y a] (+ (* unit-of-y (last a)) @leftover))]
- (loop [a x zero-digit []]
- (when-not (empty? a)
- (reset! leftover 0)
- (swap! summarize-it
- (fn [n] (conj n
- (reduce
- (fn [coll-sum unit-of-y]
- (reset! result (rem (multiple unit-of-y a) 10))
- (reset! leftover (quot (multiple unit-of-y a) 10))
- (if (= unit-of-y (last y))
- (into (vec (reverse (into (into coll-sum [@result]) [@leftover]))) zero-digit)
- (into coll-sum [@result])))
- [] y))))
- (recur (drop-last a) (conj zero-digit 0))))
- (str (reduce ; суммируем перемноженные числа
- (fn [n m] (+ n (parse-int (apply str m))))
- 0
- @summarize-it))))
- (mult "547" "83")
- (mult "78547" "583")
- (mult "1" "0")
- (mult "1" "1")
- (mult "1" "01")
- (mult "0" "01")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement