Post Reply 
Little explorations with HP calculators (no Prime)
12-28-2018, 04:04 AM
Post: #326
RE: Little explorations with HP calculators (no Prime)
(12-28-2018 01:38 AM)Albert Chan Wrote:  
Code:
def match_count(thelist):
    return sum(sum(thelist[i:i+70]) == 50 for i in range(71))

Much better. I wasn't happy with the nested generator expressions.

Here's my attempt using Clojure:
Code:
(defn sum [list]
  (apply + list))

(defn match [value]
  (= value 50))

(defn match-count [the-list]
  (->> the-list
       (partition 70 1)
       (map sum)
       (filter match)
       (count)))

(defn -main [n]
  (let [blue (repeat 100 1)
        white (repeat 40 0)
        the-list (concat blue white)]
    (->> (repeatedly n #(match-count (shuffle the-list)))
         (frequencies)
         (into (sorted-map)))))

(-main 100000)

Thanks to the threading macro ->> it reads like a recipe or pipeline.

This is a typical result:
Code:
{1 6092, 2 6052, 3 5967, 4 5922, 5 5705, 6 5669, 7 5560, 8 5332, 9 5097, 10 4820, 11 4703, 12 4333, 13 3988, 14 3816, 15 3463, 16 3096, 17 2920, 18 2514, 19 2232, 20 2034, 21 1763, 22 1525, 23 1315, 24 1186, 25 928, 26 787, 27 681, 28 512, 29 452, 30 379, 31 282, 32 196, 33 180, 34 128, 35 115, 36 73, 37 59, 38 41, 39 34, 40 20, 41 10, 42 10, 43 4, 44 1, 46 2, 47 2}

Cheers
Thomas
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: Little explorations with HP calculators (no Prime) - Thomas Klemm - 12-28-2018 04:04 AM



User(s) browsing this thread: 4 Guest(s)