fork(1) download
  1. (use 'clojure.string)
  2. (defn f [n]
  3. (let [s (str n)
  4. i (map (fn [x] (- (int x) 48)) (str(Math/abs n)))]
  5. (+
  6. (reduce + (cons (* (first i) (if (< n 0) -1 1)) (rest i)))
  7. n
  8. (count s))))
  9. (defn g [n]
  10. (let [s (str (Math/abs n))
  11. c (count (str n))
  12. digits-str (str (if (< n 0) "-" "") (join "+" s))
  13. ints (map (fn [x] (- (int x) 48)) s)
  14. digits (cons (* (first ints) (if (< n 0) -1 1)) (rest ints))
  15. sum (reduce + digits)]
  16. (str n "+" c "+(" digits-str ")=" (+ sum c n))))
  17.  
  18. (println (f 99))
  19. (println (f -123))
  20. (println (f 0))
  21. (println (f 1))
  22. (println (g 99))
  23. (println (g -123))
  24. (println (g 0))
  25. (println (g 1))
Success #stdin #stdout #stderr 1.74s 335488KB
stdin
Standard input is empty
stdout
119
-115
1
3
99+2+(9+9)=119
-123+4+(-1+2+3)=-115
0+1+(0)=1
1+1+(1)=3
stderr
WARNING: reverse already refers to: #'clojure.core/reverse in namespace: user, being replaced by: #'clojure.string/reverse
WARNING: replace already refers to: #'clojure.core/replace in namespace: user, being replaced by: #'clojure.string/replace