Last active
April 8, 2021 11:18
-
-
Save sw1nn/09f9600e677eb246490fd1f721e3ebf2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns euler-fizz-buzz) | |
;; Inspired by http://philcrissman.net/posts/eulers-fizzbuzz/ | |
(defn mod-pow [b e m] (long (.modPow (biginteger b) (biginteger e) (biginteger m)))) | |
(defn fizz-buzz [] (map #({6 "Fizz" 10 "Buzz" 0 "FizzBuzz"} (mod-pow % 4 15) %) (range 1 100))) | |
;;https://en.wikipedia.org/wiki/Euler%27s_totient_function | |
;;https://oeis.org/A000010 | |
(defn totient[n] | |
(get [1 1 2 2 4 2 6 4 6 4 10 4 12 6 8 8 16 6 18 8 12 10 22 8 20 12 18 12 28 8 30 16 20 16 24 12 36 18 24 16 40 12 42 20 24 22 46 16 42 20 32 24 52 18 40 24 36 28 58 16 60 30 36 32 48 20 66 32 44] (dec n))) | |
;; https://rosettacode.org/wiki/Least_common_multiple#Clojure | |
;; available in clojure.math.numeric-tower | |
(defn gcd [a b] | |
(if (zero? b) | |
a | |
(recur b, (mod a b)))) | |
(defn lcm [a b] | |
(/ (* a b) (gcd a b))) | |
(defn foo-bar [m] | |
(assert (= 2 (count m)) "map should contain 2 entries") | |
(let [[keys vals] ((juxt keys vals) m) | |
div (reduce * keys) | |
lcm (->> keys (map totient) (reduce lcm)) | |
m (-> #(mod-pow % lcm div) | |
(map keys) | |
(zipmap vals) | |
(assoc 0 (reduce str vals)))] | |
(->> (range 1 100) | |
(map #(m (mod-pow % lcm div) %))))) | |
(comment | |
(fizz-buzz) | |
(foo-bar {7 "foo" 11 "bar"}) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment