Skip to content

Instantly share code, notes, and snippets.

@chase-lambert
Last active May 17, 2023 00:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chase-lambert/c5347e3ad35509182977dc86af45c416 to your computer and use it in GitHub Desktop.
Save chase-lambert/c5347e3ad35509182977dc86af45c416 to your computer and use it in GitHub Desktop.
rendezvous with cassidoo challenge: 22.07.31
(ns num-of-ones
(:require [clojure.test :refer [deftest is]]))
(defn n->digits
"ex. (n->digits 14) => [1 4]"
[n]
(if (< n 10)
[n]
(conj (n->digits (quot n 10)) (rem n 10))))
(defn number-of-ones [n]
(->> (range (inc n))
(mapcat n->digits)
(filter #(= 1 %))
count))
(deftest number-of-ones-test
(is (= 7 (number-of-ones 14))))
@chase-lambert
Copy link
Author

chase-lambert commented Sep 22, 2022

Various transducer versions that run almost twice as fast for a large input number:

(defn number-of-ones-into [n]                                                                    
  (let [xf (comp (mapcat n->digits)                                                              
                 (filter #(= 1 %)))]                                                             
    (count                                                                                       
      (into [] xf (range (inc n))))))                                                            
                                                                                                 
;; (number-of-ones-into 14) ;; 7                                                                 
                                                                                                 
(defn number-of-ones-transduce [n]                                                               
  (let [xf (comp (mapcat n->digits)                                                              
                 (filter #(= 1 %)))]                                                             
    (count                                                                                       
      (transduce xf conj (range (inc n))))))                                                     
                                                                                                 
;; (number-of-ones-transduce 14) ;; 7                                                            
                                                                                                 
(defn number-of-ones-eduction [n]                                                                
  (count                                                                                         
    (into []                                                                                     
          (eduction (mapcat n->digits) (filter #(= 1 %)) (range (inc n))))))                     
                                                                                                 
;; (number-of-ones-eduction 14) ;; 7                                                             
                                                                                                 
;; (time (number-of-ones 10000000))           ;; Elapsed time: ~8.5 seconds                      
;; (time (number-of-ones-into 10000000))      ;; Elapsed time: ~4.5 seconds                      
;; (time (number-of-ones-transduce 10000000)) ;; Elapsed time: ~5 seconds                        
;; (time (number-of-ones-eduction 10000000))  ;; Elapsed time: ~4.6 seconds 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment