Commits

Anonymous committed f3ee0c4

Adding initial batch of solved Project Euler problems.

Comments (0)

Files changed (11)

+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+	<classpathentry kind="src" path="src"/>
+	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
+	<classpathentry kind="lib" path="/home/jpaulett/bin/packages/eclipse3.4/plugins/clojure_0.0.0.20090404_r1338/clojure.jar"/>
+	<classpathentry kind="lib" path="/home/jpaulett/bin/packages/eclipse3.4/plugins/clojurecontrib_0.0.0.20090404_r633/clojure-contrib.jar"/>
+	<classpathentry kind="lib" path="classes"/>
+	<classpathentry kind="output" path="bin"/>
+</classpath>
+syntax: glob
+*.class
+classes/
+bin/
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+	<name>euler-clojure</name>
+	<comment></comment>
+	<projects>
+	</projects>
+	<buildSpec>
+		<buildCommand>
+			<name>clojuredev.builder</name>
+			<arguments>
+			</arguments>
+		</buildCommand>
+		<buildCommand>
+			<name>org.eclipse.jdt.core.javabuilder</name>
+			<arguments>
+			</arguments>
+		</buildCommand>
+	</buildSpec>
+	<natures>
+		<nature>org.eclipse.jdt.core.javanature</nature>
+		<nature>clojuredev.nature</nature>
+	</natures>
+</projectDescription>
+;; 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
+;; 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)
+;; 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
+;; 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
+;; 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
+;; 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
+;; 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
+;; util
+;; John Paulett - June 8, 2009
+;;
+(ns util)
+(defn sum-digits [number]
+	(reduce + (for [c (str number)] (new Integer (str c)))))