Commits

committed e2f2081

Generalizing project for Project Euler solutions in languages other than Clojure.

• Participants
• Parent commits f3ee0c4

File clojure/eratosthenes.clj

`+;; Sieve of Eratosthenes`
`+;; http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes`
`+`
`+;(defn sieve [coll & curr] `
`+;	(if (nil? curr)`
`+;		(sieve coll 2)`
`+;		(cons curr (sieve (remove) ()))`
`+;(defn sieve [coll] `
`+;	(let [divisible? (fn [x] (= 0 (mod x (first coll))))] `
`+;	(cons (first coll) (sieve (remove divisible? coll) ))))`
`+`
`+;(defn sieve [coll] `
`+;	(loop [remaining]`
`+;		(if (empty? remaining)`
`+;			nil`
`+;			`
`+;			(let [divisible? (fn [x] (= 0 (mod x (first coll))))] `
`+;			(cons (first coll) (recur(remove divisible? coll)))))`
`+`
`+`
`+(defn sieve [coll] `
`+	(loop [result  ()`
`+				 remaining (seq coll)]`
`+		(let [divisible? (fn [x] (= 0 (mod x (first remaining))))] `
`+		(if (empty? remaining)`
`+			result`
`+			(recur (cons (first remaining) result) (remove divisible? (rest remaining)))))))`
`+`
`+(println (count (sieve (range 2 10000))))`
`+;; FIXME this blows up for big numbers`

File clojure/problem1.clj

`+;; problem1`
`+;; John Paulett - June 6, 2009`
`+;;`
`+;; If we list all the natural numbers below 10 that are multiples of 3 or 5, we`
`+;; get 3, 5, 6 and 9. The sum of these multiples is 23.`
`+;; Find the sum of all the multiples of 3 or 5 below 1000.`
`+`
`+(defn mod? [num div] (= 0 (mod num div)))`
`+(defn mod3or5? [num] (or (mod? num 3) (mod? num 5)))`
`+(defn prob1 [max] (reduce + (filter mod3or5? (range 0 max))))`
`+`
`+(assert (= 23 (prob1 10)))`
`+println (prob1 1000)`

File clojure/problem10.clj

`+;; problem10`
`+;; John Paulett - June 8, 2009`
`+;;`
`+;; The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.`
`+;; Find the sum of all the primes below two million.`
`+`
`+(use '[clojure.contrib.lazy-seqs :only (primes)])`
`+`
`+(defn sum-primes [max] `
`+	(reduce + (for [p primes :while (> max p)] p)))`
`+	`
`+;; tests`
`+(assert (= 17 (sum-primes 10)))`
`+`
`+(println (sum-primes 2000000)) `
`+;; 142913828922`

File clojure/problem16.clj

`+;; problem16`
`+;; John Paulett - June 8, 2009`
`+;;`
`+;; 2^(15) = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26.`
`+;; What is the sum of the digits of the number 2^(1000)?`
`+(use '[clojure.contrib.math :only (expt)])`
`+(use '[util :only (sum-digits)])`
`+`
`+(defn sum-exp-digits [base exp]`
`+	(sum-digits (expt base exp)))`
`+`
`+	`
`+;; tests`
`+(assert (= 26 (sum-exp-digits 2 15)))`
`+`
`+(println (sum-exp-digits 2 1000))`
`+;; 1366`

File clojure/problem2.clj

`+;; problem2`
`+;; John Paulett - June 8, 2009`
`+;;`
`+;; Each new term in the Fibonacci sequence is generated by adding the previous `
`+;; two terms. By starting with 1 and 2, the first 10 terms will be:`
`+;; 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...`
`+;; Find the sum of all the even-valued terms in the sequence which do not `
`+;; exceed four million.`
`+`
`+;(defn fib [maxval coll] `
`+;	(if (zero? coll)`
`+;		(fib (maxval (1 2)`
`+;	((reverse coll))`
`+;	)`
`+`
`+(use '[clojure.contrib.lazy-seqs :only (fibs)])`
`+`
`+(defn sum-even-fibs [max] `
`+	(reduce + (for [i fibs :while (<= i max) :when (even? i)] i)))`
`+	`
`+;; tests`
`+(assert (= 2 (sum-even-fibs 2)))	`
`+(assert (= 44 (sum-even-fibs 89)))`
`+`
`+(println (sum-even-fibs 4000000))`
`+;; 4613732`

File clojure/problem20.clj

`+;; problem20`
`+;; John Paulett - June 8, 2009`
`+;;`
`+;; n! means n × (n − 1) × ... × 3 × 2 × 1`
`+;; Find the sum of the digits in the number 100!`
`+(use '[util :only (sum-digits)])`
`+`
`+(defn factorial-series [n]`
`+	(if (= 1 n)`
`+		[1]`
`+		(cons n (factorial-series (dec n))))) ;; TODO may blow the stack `
`+`
`+(defn factorial [n]`
`+	(reduce * (factorial-series n)))`
`+	`
`+(println (sum-digits (factorial 100)))`
`+;; 648`

File clojure/problem7.clj

`+;; problem7`
`+;; John Paulett - June 8, 2009`
`+;;`
`+;; By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6^(th) prime is 13.`
`+;; What is the 10001^(st) prime number?`
`+`
`+(use '[clojure.contrib.lazy-seqs :only (primes)])`
`+(defn get-prime [idx] (nth primes (dec idx)))`
`+`
`+;; tests`
`+(assert (= 2 (get-prime 1)))`
`+(assert (= 13 (get-prime 6)))`
`+`
`+;; get the 10001st prime`
`+(println (get-prime 10001))`
`+;; 104743`

File clojure/util.clj

`+;; util`
`+;; John Paulett - June 8, 2009`
`+;;`
`+(ns util)`
`+(defn sum-digits [number]`
`+	(reduce + (for [c (str number)] (new Integer (str c)))))`

File src/eratosthenes.clj

`-;; Sieve of Eratosthenes`
`-;; http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes`
`-`
`-;(defn sieve [coll & curr] `
`-;	(if (nil? curr)`
`-;		(sieve coll 2)`
`-;		(cons curr (sieve (remove) ()))`
`-;(defn sieve [coll] `
`-;	(let [divisible? (fn [x] (= 0 (mod x (first coll))))] `
`-;	(cons (first coll) (sieve (remove divisible? coll) ))))`
`-`
`-;(defn sieve [coll] `
`-;	(loop [remaining]`
`-;		(if (empty? remaining)`
`-;			nil`
`-;			`
`-;			(let [divisible? (fn [x] (= 0 (mod x (first coll))))] `
`-;			(cons (first coll) (recur(remove divisible? coll)))))`
`-`
`-`
`-(defn sieve [coll] `
`-	(loop [result  ()`
`-				 remaining (seq coll)]`
`-		(let [divisible? (fn [x] (= 0 (mod x (first remaining))))] `
`-		(if (empty? remaining)`
`-			result`
`-			(recur (cons (first remaining) result) (remove divisible? (rest remaining)))))))`
`-`
`-(println (count (sieve (range 2 10000))))`
`-;; FIXME this blows up for big numbers`

File src/problem1.clj

`-;; problem1`
`-;; John Paulett - June 6, 2009`
`-;;`
`-;; If we list all the natural numbers below 10 that are multiples of 3 or 5, we`
`-;; get 3, 5, 6 and 9. The sum of these multiples is 23.`
`-;; Find the sum of all the multiples of 3 or 5 below 1000.`
`-`
`-(defn mod? [num div] (= 0 (mod num div)))`
`-(defn mod3or5? [num] (or (mod? num 3) (mod? num 5)))`
`-(defn prob1 [max] (reduce + (filter mod3or5? (range 0 max))))`
`-`
`-(assert (= 23 (prob1 10)))`
`-println (prob1 1000)`

File src/problem10.clj

`-;; problem10`
`-;; John Paulett - June 8, 2009`
`-;;`
`-;; The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.`
`-;; Find the sum of all the primes below two million.`
`-`
`-(use '[clojure.contrib.lazy-seqs :only (primes)])`
`-`
`-(defn sum-primes [max] `
`-	(reduce + (for [p primes :while (> max p)] p)))`
`-	`
`-;; tests`
`-(assert (= 17 (sum-primes 10)))`
`-`
`-(println (sum-primes 2000000)) `
`-;; 142913828922`

File src/problem16.clj

`-;; problem16`
`-;; John Paulett - June 8, 2009`
`-;;`
`-;; 2^(15) = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26.`
`-;; What is the sum of the digits of the number 2^(1000)?`
`-(use '[clojure.contrib.math :only (expt)])`
`-(use '[util :only (sum-digits)])`
`-`
`-(defn sum-exp-digits [base exp]`
`-	(sum-digits (expt base exp)))`
`-`
`-	`
`-;; tests`
`-(assert (= 26 (sum-exp-digits 2 15)))`
`-`
`-(println (sum-exp-digits 2 1000))`
`-;; 1366`

File src/problem2.clj

`-;; problem2`
`-;; John Paulett - June 8, 2009`
`-;;`
`-;; Each new term in the Fibonacci sequence is generated by adding the previous `
`-;; two terms. By starting with 1 and 2, the first 10 terms will be:`
`-;; 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...`
`-;; Find the sum of all the even-valued terms in the sequence which do not `
`-;; exceed four million.`
`-`
`-;(defn fib [maxval coll] `
`-;	(if (zero? coll)`
`-;		(fib (maxval (1 2)`
`-;	((reverse coll))`
`-;	)`
`-`
`-(use '[clojure.contrib.lazy-seqs :only (fibs)])`
`-`
`-(defn sum-even-fibs [max] `
`-	(reduce + (for [i fibs :while (<= i max) :when (even? i)] i)))`
`-	`
`-;; tests`
`-(assert (= 2 (sum-even-fibs 2)))	`
`-(assert (= 44 (sum-even-fibs 89)))`
`-`
`-(println (sum-even-fibs 4000000))`
`-;; 4613732`

File src/problem20.clj

`-;; problem20`
`-;; John Paulett - June 8, 2009`
`-;;`
`-;; n! means n × (n − 1) × ... × 3 × 2 × 1`
`-;; Find the sum of the digits in the number 100!`
`-(use '[util :only (sum-digits)])`
`-`
`-(defn factorial-series [n]`
`-	(if (= 1 n)`
`-		[1]`
`-		(cons n (factorial-series (dec n))))) ;; TODO may blow the stack `
`-`
`-(defn factorial [n]`
`-	(reduce * (factorial-series n)))`
`-	`
`-(println (sum-digits (factorial 100)))`
`-;; 648`

File src/problem7.clj

`-;; problem7`
`-;; John Paulett - June 8, 2009`
`-;;`
`-;; By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6^(th) prime is 13.`
`-;; What is the 10001^(st) prime number?`
`-`
`-(use '[clojure.contrib.lazy-seqs :only (primes)])`
`-(defn get-prime [idx] (nth primes (dec idx)))`
`-`
`-;; tests`
`-(assert (= 2 (get-prime 1)))`
`-(assert (= 13 (get-prime 6)))`
`-`
`-;; get the 10001st prime`
`-(println (get-prime 10001))`
`-;; 104743`

File src/util.clj

`-;; util`
`-;; John Paulett - June 8, 2009`
`-;;`
`-(ns util)`
`-(defn sum-digits [number]`
`-	(reduce + (for [c (str number)] (new Integer (str c)))))`