Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
|
|
@ -0,0 +1,37 @@
|
|||
(ns async-example.core
|
||||
(:require [clojure.math.numeric-tower :as math])
|
||||
(:use [criterium.core])
|
||||
(:gen-class))
|
||||
(defn sum-sqr [digits]
|
||||
" Square sum of list of digits "
|
||||
(let [digits-sqr (fn [n]
|
||||
(apply + (map #(* % %) digits)))]
|
||||
(digits-sqr digits)))
|
||||
|
||||
(defn get-digits [n]
|
||||
" Converts a digit to a list of digits (e.g. 545 -> ((5) (4) (5)) (used for squaring digits) "
|
||||
(map #(Integer/valueOf (str %)) (String/valueOf n)))
|
||||
|
||||
(defn -isNot89 [x]
|
||||
" Returns nil on 89 "
|
||||
(cond
|
||||
(= x 0) 0
|
||||
(= x 89) nil
|
||||
(= x 1) 0
|
||||
(< x 10) (recur (* x x))
|
||||
:else (recur (sum-sqr (get-digits x)))))
|
||||
|
||||
;; Cached version of isNot89 (i.e. remembers prevents inputs, and returns result by looking it up when input repeated)
|
||||
(def isNot89 (memoize -isNot89))
|
||||
|
||||
(defn direct-method [ndigits]
|
||||
" Simple approach of looping through all the numbers from 0 to 10^ndigits - 1 "
|
||||
(->>
|
||||
(math/expt 10 ndigits)
|
||||
(range 0) ; 0 to 10^ndigits
|
||||
(filter #(isNot89 (sum-sqr (get-digits %)))) ; filters out 89
|
||||
(count) ; count non-89
|
||||
(- (math/expt 10 ndigits)))) ; count 89 (10^ndigits - (count 89))
|
||||
|
||||
|
||||
(time (println (direct-method 8)))
|
||||
|
|
@ -0,0 +1,47 @@
|
|||
(def DIGITS (range 0 10))
|
||||
|
||||
(defn -factorial [n]
|
||||
(apply * (take n (iterate inc 1))))
|
||||
; Cached version of factorial
|
||||
(def factorial (memoize -factorial))
|
||||
|
||||
(defn -combinations [coll k]
|
||||
" From http://rosettacode.org/wiki/Combinations_with_repetitions#Clojure "
|
||||
(when-let [[x & xs] coll]
|
||||
(if (= k 1)
|
||||
(map list coll)
|
||||
(concat (map (partial cons x) (-combinations coll (dec k)))
|
||||
(-combinations xs k)))))
|
||||
; Cached version of combinations
|
||||
(def combinations (memoize -combinations))
|
||||
|
||||
(defn comb [n r]
|
||||
" count of n items select r "
|
||||
(/ (/ (factorial n) (factorial r)) (factorial (- n r))))
|
||||
|
||||
(defn count-digits [digit-list]
|
||||
" count nunmber of occurences of digit in list "
|
||||
(reduce (fn [m v] (update-in m [v] (fnil inc 0))) {} digit-list))
|
||||
|
||||
(defn count-patterns [c]
|
||||
" Count of number of patterns with these digits "
|
||||
(->>
|
||||
c
|
||||
(count-digits)
|
||||
(reduce (fn [accum [k v]]
|
||||
(* accum (factorial v)))
|
||||
1)
|
||||
(/ (factorial (count c)))))
|
||||
|
||||
(defn itertools-comb [ndigits]
|
||||
(->>
|
||||
ndigits
|
||||
(combinations DIGITS)
|
||||
(filter #(is89 (sum-sqr %))) ; items which are not 89 (i.e. 1 since lower count)
|
||||
(reduce (fn [acc c]
|
||||
(+ acc (count-patterns c)))
|
||||
0)
|
||||
(- (math/expt 10 ndigits))))
|
||||
|
||||
(println (itertools-comb 8))
|
||||
;; Time obtained using benchmark library (i.e. (bench (itertools-comb 8)) )
|
||||
Loading…
Add table
Add a link
Reference in a new issue