Created
October 13, 2010 21:06
-
-
Save gar3thjon3s/624920 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 clojure-scratch.gettysburg | |
(:require [clojure.contrib.lazy-seqs :as ls] | |
[clojure.contrib.combinatorics :as comb])) | |
;; level-one | |
(def level-one "FourscoreandsevenyearsagoourfaathersbroughtforthonthiscontainentanewnationconceivedinzLibertyanddedicatedtothepropositionthatallmenarecreatedequalNowweareengagedinagreahtcivilwartestingwhetherthatnaptionoranynartionsoconceivedandsodedicatedcanlongendureWeareqmetonagreatbattlefiemldoftzhatwarWehavecometodedicpateaportionofthatfieldasafinalrestingplaceforthosewhoheregavetheirlivesthatthatnationmightliveItisaltogetherfangandproperthatweshoulddothisButinalargersensewecannotdedicatewecannotconsecratewecannothallowthisgroundThebravelmenlivinganddeadwhostruggledherehaveconsecrateditfaraboveourpoorponwertoaddordetractTgheworldadswfilllittlenotlenorlongrememberwhatwesayherebutitcanneverforgetwhattheydidhereItisforusthelivingrathertobededicatedheretotheulnfinishedworkwhichtheywhofoughtherehavethusfarsonoblyadvancedItisratherforustobeherededicatedtothegreattdafskremainingbeforeusthatfromthesehonoreddeadwetakeincreaseddevotiontothatcauseforwhichtheygavethelastpfullmeasureofdevotionthatweherehighlyresolvethatthesedeadshallnothavediedinvainthatthisnationunsderGodshallhaveanewbirthoffreedomandthatgovernmentofthepeoplebythepeopleforthepeopleshallnotperishfromtheearth") | |
(defn palindrone? [w] | |
(= w (apply str (reverse w)))) | |
(defn longest-palindrone-in [s] | |
(loop [l 2 | |
winner ""] | |
(if (= l (count s)) | |
winner | |
(let [words (map #(apply str %) (partition l 1 s)) | |
palindrones (filter palindrone? words)] | |
(recur (inc l) | |
(or (first palindrones) | |
winner)))))) | |
(time | |
(println (longest-palindrone-in level-one))) ;; answer = ranynar | |
;; level-two | |
(defn prime? [n] | |
(loop [i (int (Math/sqrt n))] | |
(if (<= i 1) | |
true | |
(if (= 0 (mod n i)) | |
false | |
(recur (dec i)))))) | |
(defn prime-fibs [] | |
(filter prime? (ls/fibs))) | |
(time (println (first (filter #(> % 227000) (prime-fibs))))) ;; 514229 | |
(defn prime-divisors [n] | |
(distinct (filter (fn [p] | |
(= (mod n p) 0)) | |
(take-while (fn [p] (<= (* p p) n)) | |
ls/primes)))) | |
(time (println (apply + (prime-divisors 514230)))) ;; 352 | |
;; level-three | |
(def numbers [3 4 9 14 15 19 28 37 47 50 54 56 59 61 70 73 78 81 92 95 97 99]) | |
(defn largest-is-sum-of-others? [l] | |
(let [l (reverse (sort l)) | |
largest (first l) | |
others (rest l)] | |
(= largest (apply + others)))) | |
(defn solve [n] | |
(loop [res [] | |
i 3] | |
(if (> i (count n)) | |
(count res) | |
(recur (into res | |
(filter largest-is-sum-of-others? | |
(comb/combinations n i))) | |
(inc i))))) | |
(time println (solve numbers)) ;; 179 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment