Skip to content

Instantly share code, notes, and snippets.

@bhenry
Created May 5, 2010 19:26
Show Gist options
  • Save bhenry/391302 to your computer and use it in GitHub Desktop.
Save bhenry/391302 to your computer and use it in GitHub Desktop.
(ns student.learning
(:refer-clojure :exclude [get])
(:use [clojure.contrib.str-utils :only [str-join]]
[clojure.contrib.math :only [sqrt lcm]]
[clojure.contrib.lazy-seqs :only [primes]]))
(defn babyize [s] "replace l and r with w if first letter of each word"
(let [b-word (fn [[first-letter & word]]
(let [new-first (cond (some #{first-letter} [\l \r]) \w
(some #{first-letter} [\L \R]) \W
:else first-letter)]
(apply str new-first word)))]
(str-join " " (map b-word (.split s " ")))))
;;(babyize "The little bunny went to the rail road for lunch with a rabbit.")
;; returns "The wittle bunny went to the wail woad for wunch with a
;; wabbit."
;;;;;;;Project Euler
;;problem 1 sum of multiples of 3 or 5 less than 1000
(defn divides? "returns true if x/y has no remainder"
[x y]
(zero? (rem x y)))
(defn div-any [& nums]
(fn [arg]
(boolean (some #(divides? arg %) nums))))
(defn sum-mult-3-5 "sums all the multiples of 3 and five up to upper"
[upper]
(apply + (filter (div-any 3 5) (range upper))))
;;problem 2 sum even fibonacci #s up to 4000000
(defn fib-seq []
(let [step (fn [pair]
[(last pair) (+ (first pair) (last pair))])]
(map first (iterate step [0 1]))))
(def fibs (fib-seq))
;;(apply + (filter even? (take-while #(<= % 4000000) fibs)))
;;problem 3 largest prime factor of 600851475143
(defn prime? [x]
(if (>= x 2)
(zero?
(count
(filter #(divides? x %)
(range 2 (inc (int (sqrt x)))))))
false))
(defn get-factors [x]
(let [lower-factors (filter #(zero? (rem x %))
(range 1 (inc (int (sqrt x)))))]
(concat lower-factors
(reverse (map #(/ x %)
(remove #(= % (sqrt x))
lower-factors))))))
(defn get-max-prime-factor [x]
(apply max (filter prime? (get-factors x))))
;;(time (get-max-prime-factor 600851475143))
;;(time (last (filter prime? (get-factors 600851475143))))
;;(time (filter prime? (get-factors 600851475143)))
;;problem 4 biggest palindrome made by product of 2 three digit #s
(defn palindrome? [x]
(= (reverse (str x))
(seq (str x))))
(defn biggest-palindrome []
(apply max (for [x (range 100 1000)
y (range 100 1000)
:when (palindrome?
(* x y))]
(* x y))))
;;(time (biggest-palindrome))
;;problem 5
;;(reduce lcm (range 1 21))
;; problem 6 difference of the square of the sums and the sum of the
;; squares
(defn dif-sum-squares [x]
(let [sqr #(* % %)
sum-squares (apply + (map sqr (range (inc x))))
square-sums (sqr (apply + (range (inc x))))]
(- square-sums sum-squares)))
;;(dif-sum-squares 100)
;;problem 7 nth prime 10001th prime
(defn nth-prime [n]
(second (take-nth (dec n) primes)))
;;(nth-prime 10001)
;;problem 8 find the biggest product of 5 consecutive digits in...
(def prob8 (str
"73167176531330624919225119674426574742355349194934
96983520312774506326239578318016984801869478851843
85861560789112949495459501737958331952853208805511
12540698747158523863050715693290963295227443043557
66896648950445244523161731856403098711121722383113
62229893423380308135336276614282806444486645238749
30358907296290491560440772390713810515859307960866
70172427121883998797908792274921901699720888093776
65727333001053367881220235421809751254540594752243
52584907711670556013604839586446706324415722155397
53697817977846174064955149290862569321978468622482
83972241375657056057490261407972968652414535100474
82166370484403199890008895243450658541227588666881
16427171479924442928230863465674813919123162824586
17866458359124566529476545682848912883142607690042
24219022671055626321111109370544217506941658960408
07198403850962455444362981230987879927244284909188
84580156166097919133875499200524063689912560717606
05886116467109405077541002256983155200055935729725
71636269561882670428252483600823257530420752963450"))
(def digits (map #(Integer. (str %)) (remove #(= % \newline) (seq prob8))))
(defn list-prods-of-5 [coll]
(if (< 4 (count coll))
(let [prod (apply * (take 5 coll))]
(concat [prod] (list-prods-of-5 (rest coll))))))
;;(apply max (list-prods-of-5 digits))
;;################################################
;;################################################
;;################################################
;;################################################
;;################################################
;;################################################
;;atoms and such
(def counter (atom 0))
@counter
(swap! counter inc)
(def language (atom {:name "Clojure" :age 2}))
(swap! language update-in [:age] inc)
@language
(swap! language update-in [:name] #(.toUpperCase %))
(defn create-1 []
(atom {}))
(defn get
[cache key]
(@cache key))
(defn put-1 [cache key value]
(swap! cache assoc key value))
(let [c (create-1)]
(put-1 c :greeting "hello")
(get c :greeting))
(defn create
([] (create {}))
([initial-value] (atom initial-value)))
(defn put
([cache value-map]
(swap! cache merge value-map))
([cache key value]
(swap! cache assoc key value)))
(let
[grades (create {:A 100, :B 90})]
(put grades {:C 80 :D 70})
grades)
(def counter (ref 0))
(dosync (alter counter inc))
(defn create-r
([] (create-r {}))
([initial-value] (ref initial-value)))
(defn get-r
[cache key]
(@cache key))
(defn put-r
([cache value-map]
(dosync
(alter cache merge value-map)))
([cache key value]
(dosync
(alter cache assoc key value))))
(let
[colors (create-r {:hulk "green" :aquaman "orange"})]
(put-r colors {:flash "red" :sinestro "yello"})
colors)
(let [[colors powers] (repeatedly create-r)]
(dosync
(put-r colors :hulk "green")
(put-r powers :sinestro "fear-powered space flight"))
{:colors colors :powers powers})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment