<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>The Philosophy and the Craft</title>
	<atom:link href="http://sidhantgodiwala.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://sidhantgodiwala.wordpress.com</link>
	<description>A blog about programming and trying out new things</description>
	<lastBuildDate>Sat, 19 Dec 2009 14:47:47 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='sidhantgodiwala.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>The Philosophy and the Craft</title>
		<link>http://sidhantgodiwala.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://sidhantgodiwala.wordpress.com/osd.xml" title="The Philosophy and the Craft" />
	<atom:link rel='hub' href='http://sidhantgodiwala.wordpress.com/?pushpress=hub'/>
		<item>
		<title>On Erlang</title>
		<link>http://sidhantgodiwala.wordpress.com/2009/12/19/on-erlang/</link>
		<comments>http://sidhantgodiwala.wordpress.com/2009/12/19/on-erlang/#comments</comments>
		<pubDate>Sat, 19 Dec 2009 14:47:47 +0000</pubDate>
		<dc:creator>Sidhant Godiwala</dc:creator>
				<category><![CDATA[1]]></category>

		<guid isPermaLink="false">http://sidhantgodiwala.wordpress.com/?p=174</guid>
		<description><![CDATA[I have a 6 month internship as part of my CS degree and while most of my classmates seize this opportunity to join a nice big MNC, I&#8217;ve taken the path less traveled and joined a startup. Not that I have a problem at all, this is what I&#8217;ve wanted to do ever since I [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sidhantgodiwala.wordpress.com&amp;blog=9047704&amp;post=174&amp;subd=sidhantgodiwala&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I have a 6 month internship as part of my CS degree and while most of my classmates seize this opportunity to join a nice big MNC, I&#8217;ve taken the path less traveled and joined a startup. Not that I have a problem at all, this is what I&#8217;ve wanted to do ever since I started my degree.</p>
<p><a href="http://hover.in">hover.in</a> is a in-context ad delivery platform, one of the interesting things about them is that they&#8217;re fans of functional programming. Their servers run <a href="http://en.wikipedia.org/wiki/Erlang_(programming_language)">erlang</a> which is a language which handles concurrency by being functional and having an easy to use message passing system.</p>
<p>I&#8217;m currently working through Joe Armstrong&#8217;s <a href="http://www.amazon.com/Programming-Erlang-Software-Concurrent-World/dp/193435600X">Programming Erlang</a> and while I feel the syntax could be friendlier , the concept of using 100s of concurrently running processes in your programs is an interesting experience.</p>
<p>Even if you never have a chance to use erlang in your day job, definitely check it out. With those 100 core processor&#8217;s we&#8217;re going to be using soon it&#8217;ll be worth it.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sidhantgodiwala.wordpress.com/174/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sidhantgodiwala.wordpress.com/174/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sidhantgodiwala.wordpress.com/174/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sidhantgodiwala.wordpress.com/174/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sidhantgodiwala.wordpress.com/174/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sidhantgodiwala.wordpress.com/174/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sidhantgodiwala.wordpress.com/174/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sidhantgodiwala.wordpress.com/174/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sidhantgodiwala.wordpress.com/174/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sidhantgodiwala.wordpress.com/174/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sidhantgodiwala.wordpress.com/174/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sidhantgodiwala.wordpress.com/174/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sidhantgodiwala.wordpress.com/174/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sidhantgodiwala.wordpress.com/174/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sidhantgodiwala.wordpress.com&amp;blog=9047704&amp;post=174&amp;subd=sidhantgodiwala&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://sidhantgodiwala.wordpress.com/2009/12/19/on-erlang/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9f07870310e74733d399a66e4beea43b?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Sidhant Godiwala</media:title>
		</media:content>
	</item>
		<item>
		<title>On Lisp &#8211; chapter 3</title>
		<link>http://sidhantgodiwala.wordpress.com/2009/12/19/on-lisp-chapter-3/</link>
		<comments>http://sidhantgodiwala.wordpress.com/2009/12/19/on-lisp-chapter-3/#comments</comments>
		<pubDate>Sat, 19 Dec 2009 13:32:23 +0000</pubDate>
		<dc:creator>Sidhant Godiwala</dc:creator>
				<category><![CDATA[On Lisp]]></category>

		<guid isPermaLink="false">http://sidhantgodiwala.wordpress.com/?p=169</guid>
		<description><![CDATA[Chapter 3 is a small chapter, it really talks about the wrong way to do things in Common Lisp. ;; Clojure is not the best Lisp dialect to implement ;; bad reverse. In fact its rather painful. While the ;; bad reverse example is great for what you ;; shouldn't do, i'd rather port the [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sidhantgodiwala.wordpress.com&amp;blog=9047704&amp;post=169&amp;subd=sidhantgodiwala&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Chapter 3 is a small chapter, it really talks about the wrong way to do things in Common Lisp.</p>
<pre>
;; Clojure is not the best Lisp dialect to implement
;; bad reverse. In fact its rather painful. While the
;; bad reverse example is great for what you
;; shouldn't do, i'd rather port the good ones.

;; page 30

(defn good-reverse [lst]
  (loop [lst lst acc '()]
    (if (empty? lst)
      acc
      (recur (rest lst) (conj acc (first lst))))))

;; Clojure does not support multiple return values,
;; however it does support destructuring

;; page 32

(defn powers [x]
  [x (Math/pow x 2)])

(let [[base square] (powers 4)]
  (list base square))
</pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sidhantgodiwala.wordpress.com/169/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sidhantgodiwala.wordpress.com/169/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sidhantgodiwala.wordpress.com/169/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sidhantgodiwala.wordpress.com/169/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sidhantgodiwala.wordpress.com/169/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sidhantgodiwala.wordpress.com/169/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sidhantgodiwala.wordpress.com/169/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sidhantgodiwala.wordpress.com/169/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sidhantgodiwala.wordpress.com/169/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sidhantgodiwala.wordpress.com/169/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sidhantgodiwala.wordpress.com/169/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sidhantgodiwala.wordpress.com/169/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sidhantgodiwala.wordpress.com/169/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sidhantgodiwala.wordpress.com/169/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sidhantgodiwala.wordpress.com&amp;blog=9047704&amp;post=169&amp;subd=sidhantgodiwala&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://sidhantgodiwala.wordpress.com/2009/12/19/on-lisp-chapter-3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9f07870310e74733d399a66e4beea43b?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Sidhant Godiwala</media:title>
		</media:content>
	</item>
		<item>
		<title>On Lisp &#8211; chapter 2</title>
		<link>http://sidhantgodiwala.wordpress.com/2009/11/18/on-lisp-chapter-2/</link>
		<comments>http://sidhantgodiwala.wordpress.com/2009/11/18/on-lisp-chapter-2/#comments</comments>
		<pubDate>Wed, 18 Nov 2009 04:21:57 +0000</pubDate>
		<dc:creator>Sidhant Godiwala</dc:creator>
				<category><![CDATA[On Lisp]]></category>

		<guid isPermaLink="false">http://sidhantgodiwala.wordpress.com/?p=164</guid>
		<description><![CDATA[Here&#8217;s the 2nd chapter of On Lisp translated to Clojure. I&#8217;m still learning Clojure and if you have any suggestions I would love to hear them. I am also working on the other chapters and will post them up as and when I finish them ;;page 10 (defn dbl [x] (* x 2)) (dbl 1) [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sidhantgodiwala.wordpress.com&amp;blog=9047704&amp;post=164&amp;subd=sidhantgodiwala&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s the 2nd chapter of On Lisp translated to Clojure. I&#8217;m still learning Clojure and if you have any suggestions I would love to hear them. I am also working on the other chapters and will post them up as and when I finish them</p>
<pre>
;;page 10

(defn dbl [x]
  (* x 2))

(dbl 1)

dbl

;; page 11

(= dbl (first (list dbl)))

;; There are 2 ways to create anonymous functions in Clojure,
;; the #(...) syntax creates a function with the %/%1 %2 %3 as
;; the args. "fn" also create anonymous functions but these
;; can be named as well as have nested anonymous functions.

#(* % 2)

(fn [x] (* x 2))

(fn dbl [x] (* x 2))

(dbl 3)

(#(* % 2) 3)

;; page 13

;; In clojure, which is a lisp-1, there is a single namespace for
;; variables and functions, there is also a single function resolve
;; which resolves a symbol

(resolve 'dbl)

(def x conj)

;; In this case, the result is true, since we're testing the same
;; object for equality

  (= x conj) 

;; In this case, the result is false, since 'x resolves to
;; user/x while 'conj resolves to clojure.core/conj. They're
;; different objects

  (= (resolve 'x) (resolve 'conj))

(defn dbl [x]
  (* x 2))

;; Clojure only allows you to bind to a symbol using
;; def, defn and let

(def dbl #(* % 2))

;; (def (symbol "dbl") #(* % 2)) is not valid clojure,
;; does anyone know an alternative.

(eval `(def ~(symbol "dbl") ~#(* % 2)))

;; page 13

(+ 1 2)

(apply + '(1 2))

(apply (resolve '+) '(1 2))

(apply #(+ %1 %2) '(1 2))

(apply + 1 2 '(3 4))

;; Clojure is a Lisp-1 so no need for funcall

;; page 14

(map #(+ % 10) '(1 2 3))

(map + '(1 2 3) '(10 100 1000))

(sort &lt; &#39;(1 4 2 5 6 7 3))

(remove even? &#39;(1 2 3 4 5 6 7))

;; page 15

(defn our-remove-if [f lst]
  (if (empty? lst)
    &#39;()
    (if (f (first lst))
      (recur f (rest lst))
      (conj (our-remove-if f (rest lst)) (first lst)))))

;; Not a true implementation, but condp is just so cool

(defn behave [animal]
  (condp = animal
    &#39;dog &quot;bark&quot;
    &#39;rat &quot;squeak&quot;
    &#39;cat &quot;scratch-carpet&quot;))

;; page 16

(let [y 7]
  (defn scope-test [x]
    (list x y)))

;; page 17

(let [y 5]
  (scope-test 3))

;; page 18

(defn list+ [lst n]
  (map #(+ % n) lst))

(list+ &#39;(1 2 3) 10)

;; Since clojure is purely functional, the only way to cause mutable
;; changes is to use atoms, refs and agents. Here I use atoms.

(let [counter (atom 0)]
  (defn new-id []
    (swap! counter inc))
  (defn reset-id []
    (reset! counter 0)))

(defn make-adder [n]
  #(+ % n))

;; page 19

(def add2 (make-adder 2))

(def add10 (make-adder 10))

(add2 5)

(add10 3)

(defn make-adderb [n]
  (let [n (atom n)]
    (fn [x &amp; change]
      (if (and (not (empty? change)) (first change))
	(reset! n x)
	(+ x @n)))))

(make-adderb 1)

(def addx (make-adderb 1))

(addx 3)

(addx 100 true)

(addx 3)

;; page 20

(defn make-dbms [db]
  (let [db (atom db)]
    {:lookup #(@db %)
     :insert (fn [k v] (swap! db #(assoc % k v)))
     :delete (fn [k] (swap! db #(dissoc % k)))}))

(def cities (make-dbms {:boston &#39;US :paris &#39;France}))

((cities :lookup) :boston)

((cities :insert) :london &#39;England)

((cities :lookup) :london)

(defn lookup [k db]
  ((db :lookup) k))

;; page 22

;; Clojure doesn&#39;t require labels, again because it&#39;s a Lisp-1

(let [incr #(inc %)]
  (incr 3))

;; In clojure let is let*, so y here will be 10,
;; however, let does not allow self referential
;; bindings

(let [x 10
      y x]
  y)

(defn count-instances [obj lsts]
  (let [instances-in (fn instances-in [lst]
		       (if (not (empty? lst))
			 (+ (if (= (first lst) obj) 1 0)
			    (instances-in (rest lst)))
			 0))]
    (map instances-in lsts)))

(count-instances &#39;a &#39;((a b c) (d a r p a) (d a r) (a a)))

;; The JVM does not have support for Tail Call Optimization (yet),
;; use the recur keyword for self recursion and trampoline for
;; mutual recursion

(defn our-length [lst]
  (if (empty? lst)
    0
    (inc (our-length (rest lst)))))

;; page 23

(defn our-find-if [f lst]
  (if (f (first lst))
    (first lst)
    (recur f (rest lst))))

(defn our-length [lst]
  (let [rec (fn [lst acc]
	      (if (empty? lst)
		acc
		(recur (rest lst) (inc acc))))]
    (rec lst 0)))

;; page 24

(defn triangle [n]
  (let [tri (fn [c n]
	      (if (zero? n)
		c
		(recur (+ n c) (dec n))))]
    (tri 0 n)))

(triangle 6)

;; All clojure code, before it is run, is transformed into JVM bytecode, there
;; is also the option of Ahead Of Time (AOT) compilation
</pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sidhantgodiwala.wordpress.com/164/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sidhantgodiwala.wordpress.com/164/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sidhantgodiwala.wordpress.com/164/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sidhantgodiwala.wordpress.com/164/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sidhantgodiwala.wordpress.com/164/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sidhantgodiwala.wordpress.com/164/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sidhantgodiwala.wordpress.com/164/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sidhantgodiwala.wordpress.com/164/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sidhantgodiwala.wordpress.com/164/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sidhantgodiwala.wordpress.com/164/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sidhantgodiwala.wordpress.com/164/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sidhantgodiwala.wordpress.com/164/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sidhantgodiwala.wordpress.com/164/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sidhantgodiwala.wordpress.com/164/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sidhantgodiwala.wordpress.com&amp;blog=9047704&amp;post=164&amp;subd=sidhantgodiwala&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://sidhantgodiwala.wordpress.com/2009/11/18/on-lisp-chapter-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9f07870310e74733d399a66e4beea43b?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Sidhant Godiwala</media:title>
		</media:content>
	</item>
		<item>
		<title>On Lisp</title>
		<link>http://sidhantgodiwala.wordpress.com/2009/11/02/on-lisp/</link>
		<comments>http://sidhantgodiwala.wordpress.com/2009/11/02/on-lisp/#comments</comments>
		<pubDate>Sun, 01 Nov 2009 18:51:32 +0000</pubDate>
		<dc:creator>Sidhant Godiwala</dc:creator>
				<category><![CDATA[Functional Programming]]></category>

		<guid isPermaLink="false">http://sidhantgodiwala.wordpress.com/?p=161</guid>
		<description><![CDATA[Another project i&#8217;m working on is translating Paul Graham&#8217;s On Lisp (which is available for free) into Clojure, well some of it anyway. It&#8217;s really fun and interesting to read a book by graham since his essays inspired me to learn Lisp in the first place. As with the project Euler sums, i&#8217;ll post more info [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sidhantgodiwala.wordpress.com&amp;blog=9047704&amp;post=161&amp;subd=sidhantgodiwala&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Another project i&#8217;m working on is translating Paul Graham&#8217;s O<a title="On Lisp" href="http://www.paulgraham.com/onlisp.html">n Lisp </a> (which is available for free) into Clojure, well some of it anyway.</p>
<p>It&#8217;s really fun and interesting to read a book by graham since his <a title="Essays" href="http://www.paulgraham.com/articles.html">essays </a>inspired me to learn Lisp in the first place. As with the project Euler sums, i&#8217;ll post more info as I discover it</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sidhantgodiwala.wordpress.com/161/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sidhantgodiwala.wordpress.com/161/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sidhantgodiwala.wordpress.com/161/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sidhantgodiwala.wordpress.com/161/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sidhantgodiwala.wordpress.com/161/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sidhantgodiwala.wordpress.com/161/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sidhantgodiwala.wordpress.com/161/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sidhantgodiwala.wordpress.com/161/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sidhantgodiwala.wordpress.com/161/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sidhantgodiwala.wordpress.com/161/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sidhantgodiwala.wordpress.com/161/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sidhantgodiwala.wordpress.com/161/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sidhantgodiwala.wordpress.com/161/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sidhantgodiwala.wordpress.com/161/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sidhantgodiwala.wordpress.com&amp;blog=9047704&amp;post=161&amp;subd=sidhantgodiwala&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://sidhantgodiwala.wordpress.com/2009/11/02/on-lisp/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9f07870310e74733d399a66e4beea43b?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Sidhant Godiwala</media:title>
		</media:content>
	</item>
		<item>
		<title>Problem 9: Pythagorean Triplet</title>
		<link>http://sidhantgodiwala.wordpress.com/2009/10/19/problem-9-pythagorean-triplet/</link>
		<comments>http://sidhantgodiwala.wordpress.com/2009/10/19/problem-9-pythagorean-triplet/#comments</comments>
		<pubDate>Mon, 19 Oct 2009 12:57:01 +0000</pubDate>
		<dc:creator>Sidhant Godiwala</dc:creator>
				<category><![CDATA[Project Euler]]></category>

		<guid isPermaLink="false">http://sidhantgodiwala.wordpress.com/?p=158</guid>
		<description><![CDATA[Find the only Pythagorean triplet which sums to get 1000 A Pythagorean triplet consists of 3 numbers say x, y and z such that x &#60; y &#60; z and x^2 + y^2 = z^2. The task is to find a triplet which satisfies the equation x + y + z = 1000 (defn sqr [x] (* [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sidhantgodiwala.wordpress.com&amp;blog=9047704&amp;post=158&amp;subd=sidhantgodiwala&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><strong>Find the only Pythagorean triplet which sums to get 1000</strong></p>
<p>A <a href="http://en.wikipedia.org/wiki/Pythagorean_triple">Pythagorean triplet</a> consists of 3 numbers say x, y and z such that x &lt; y &lt; z and x^2 + y^2 = z^2.</p>
<p>The task is to find a triplet which satisfies the equation x + y + z = 1000</p>
<pre>(defn sqr [x]
    (* x x))

(defn triplets [n]
    (loop [x 1 y 2 z (- n 3) acc '()]
	(cond
  	    (&gt;= x z) acc
	    (&gt;= y z) (recur (inc x) (+ 2 x) (- n (+ 3 x x)) acc)
	    :else (recur x (inc y) (dec z) (cons (list x y z) acc)))))</pre>
<p><em>triplets</em> generates all possible triplets (x, y, z) where x + y + z = 1000 and x &lt; y &lt; z</p>
<p>After this all that needs to be done is</p>
<pre>(apply * (first (filter #(let [[a b c] %]
				(= (+ (sqr a) (sqr b)) (sqr c)))
			    (triplets 1000)))</pre>
<p>The <em>let</em> construct in this case has new syntax</p>
<pre>(let [[a b c] '(1 2 3)]
    (...))</pre>
<p>This is known as <a href="http://clojure.org/special_forms">destructuring</a>, this syntax binds a, b and c to 1, 2 and 3 respectively.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sidhantgodiwala.wordpress.com/158/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sidhantgodiwala.wordpress.com/158/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sidhantgodiwala.wordpress.com/158/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sidhantgodiwala.wordpress.com/158/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sidhantgodiwala.wordpress.com/158/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sidhantgodiwala.wordpress.com/158/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sidhantgodiwala.wordpress.com/158/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sidhantgodiwala.wordpress.com/158/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sidhantgodiwala.wordpress.com/158/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sidhantgodiwala.wordpress.com/158/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sidhantgodiwala.wordpress.com/158/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sidhantgodiwala.wordpress.com/158/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sidhantgodiwala.wordpress.com/158/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sidhantgodiwala.wordpress.com/158/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sidhantgodiwala.wordpress.com&amp;blog=9047704&amp;post=158&amp;subd=sidhantgodiwala&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://sidhantgodiwala.wordpress.com/2009/10/19/problem-9-pythagorean-triplet/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9f07870310e74733d399a66e4beea43b?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Sidhant Godiwala</media:title>
		</media:content>
	</item>
		<item>
		<title>Problem 8: 1000 digit number</title>
		<link>http://sidhantgodiwala.wordpress.com/2009/10/18/problem-8-1000-digit-number/</link>
		<comments>http://sidhantgodiwala.wordpress.com/2009/10/18/problem-8-1000-digit-number/#comments</comments>
		<pubDate>Sun, 18 Oct 2009 06:57:45 +0000</pubDate>
		<dc:creator>Sidhant Godiwala</dc:creator>
				<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[1000 digits]]></category>
		<category><![CDATA[clojure]]></category>

		<guid isPermaLink="false">http://sidhantgodiwala.wordpress.com/?p=153</guid>
		<description><![CDATA[Find the largest product of 5 consecutive digits of a 1000 digit number This can be solved very elegantly in Clojure, the 1000 digit number can be found here. (def number-string "73167...") (def digits (map #(- (int %) 48) number-string)) (apply max (map * digits (drop 1 digits) (drop 2 digits) (drop 3 digits) (drop [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sidhantgodiwala.wordpress.com&amp;blog=9047704&amp;post=153&amp;subd=sidhantgodiwala&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><strong>Find the largest product of 5 consecutive digits of a 1000 digit number</strong></p>
<p>This can be solved very elegantly in Clojure, the 1000 digit number can be found <a title="1000 digit number" href="http://projecteuler.net/index.php?section=problems&amp;id=8">here</a>.</p>
<pre>(def number-string "73167...")

(def digits (map #(- (int %) 48) number-string))

(apply max (map * digits (drop 1 digits) (drop 2 digits) (drop 3 digits) (drop 4 digits)))</pre>
<p><em>number-string</em> stores the 1000 digit number as a string</p>
<p><em>digits</em> creates a list of digits from <em>number-string</em>, 48 is the ASCII value for the character &#8217;0&#8242;, 49 for &#8217;1&#8242; and so on. Subtracting 48 from the character gives the digit value.</p>
<p><em>map</em> runs only till one of the list arguments terminates so the last line is valid</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sidhantgodiwala.wordpress.com/153/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sidhantgodiwala.wordpress.com/153/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sidhantgodiwala.wordpress.com/153/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sidhantgodiwala.wordpress.com/153/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sidhantgodiwala.wordpress.com/153/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sidhantgodiwala.wordpress.com/153/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sidhantgodiwala.wordpress.com/153/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sidhantgodiwala.wordpress.com/153/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sidhantgodiwala.wordpress.com/153/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sidhantgodiwala.wordpress.com/153/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sidhantgodiwala.wordpress.com/153/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sidhantgodiwala.wordpress.com/153/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sidhantgodiwala.wordpress.com/153/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sidhantgodiwala.wordpress.com/153/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sidhantgodiwala.wordpress.com&amp;blog=9047704&amp;post=153&amp;subd=sidhantgodiwala&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://sidhantgodiwala.wordpress.com/2009/10/18/problem-8-1000-digit-number/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9f07870310e74733d399a66e4beea43b?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Sidhant Godiwala</media:title>
		</media:content>
	</item>
		<item>
		<title>Problem 7: Primes</title>
		<link>http://sidhantgodiwala.wordpress.com/2009/10/17/problem-7-primes/</link>
		<comments>http://sidhantgodiwala.wordpress.com/2009/10/17/problem-7-primes/#comments</comments>
		<pubDate>Fri, 16 Oct 2009 19:16:21 +0000</pubDate>
		<dc:creator>Sidhant Godiwala</dc:creator>
				<category><![CDATA[Project Euler]]></category>

		<guid isPermaLink="false">http://sidhantgodiwala.wordpress.com/?p=139</guid>
		<description><![CDATA[Find the 10001&#8242;st prime number The prime numbers are intriguing, there is no pattern and no direct formula for the nth term in the series of primes. While an nth term formula would be enough for this problem, it would be far more useful to have an infinite, lazy list of primes like the fibonacci [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sidhantgodiwala.wordpress.com&amp;blog=9047704&amp;post=139&amp;subd=sidhantgodiwala&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><strong>Find the 10001&#8242;st prime number</strong></p>
<p>The <a href="http://en.wikipedia.org/wiki/Prime_number">prime numbers</a> are intriguing, there is no pattern and no direct formula for the nth term in the series of primes. While an nth term formula would be enough for this problem, it would be far more useful to have an infinite, lazy list of primes like the fibonacci list in my <a title="Problem 2: Fibonacci" href="http://sidhantgodiwala.wordpress.com/2009/10/13/problem-2/">earlier post</a>.</p>
<p>The <a title="Prime Sieve" href="http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes">sieve of Eratosthenes</a> is the one of the fastest way to find primes up to a given number. Implementing the essence of the sieve to get an infinite list was the goal.</p>
<pre>Start of with an initial list of primes: ( 2 ) and a counter: n = 3
If none of the primes in the list divides n, add n to the list
increment n</pre>
<p>Using this algorithm, we can implement an infinite list of primes easily but this turned out to be slow, far slower than I expected.</p>
<p>While searching for inspiration I came across <a title="The Real Sieve" href="http://www.cs.hmc.edu/~oneill/papers/Sieve-JFP.pdf">this paper</a> and one basic conclusion, my original implementation was not the sieve.</p>
<p>I tried implementing a large number of different algorithms including the <a title="Sieve of Sundaram" href="http://en.wikipedia.org/wiki/Sieve_of_Sundaram">sieve of Sundaram</a> but none of them was fast enough. The sieve of Sundaram has an interesting method of finding primes. Instead of finding the primes themselves, it searches for the next odd composite.</p>
<p>This was the Eureka moment, I combined the concept of next odd composite with the Sieve of Eratosthenes and came up with this algorithm</p>
<pre>Start with lc: last composite = 3, and a map of key value pairs {(9, 6)}
Remove the element from the list with the smallest key [(9, 6)]
store the key as nc: next composite and value as increment [nc = 9, increment = 6]
All odd numbers between lc and nc are prime
For each odd number, n, between lc and the  nc, insert (sqr(n), 2*n) into the map [(25, 10) (49, 14)]
lc = nc [lc = 9]
nc = nc + increment [nc = 15]
while nc exists as key in the map
    nc = nc + increment
insert (nc, increment) into the map [(15, 6)]
repeat from step 1 for next set of primes</pre>
<p>Repeating the algorithm for 3 iterations</p>
<pre>lc = 3                              map: {(9, 6)}
lc = 9          primes: 5, 7        map: {(15, 6), (25, 10), (49, 14)}
lc = 15         primes: 11, 13      map: {(21, 6), (25, 10), (49, 14), (121, 22), (169, 26)}</pre>
<p>One thing which differentiates Clojure from other Lisps is the addition of powerful <a href="http://en.wikipedia.org/wiki/Data_structure">data structures</a> as primitives. Clojure includes implementations for Vectors, Maps and Sets besides the usual List. [<a href="http://clojure.org/data_structures">Clojure Data Structures</a>]</p>
<p>Clojure also provides sorted versions of its Map and Set data type which make implementing a priority queue a breeze.</p>
<pre>List           ()
Vector         []
Hash-Map       {}
Set           #{}</pre>
<p>So here&#8217;s the implementation</p>
<pre>(defn make-composite-cell [composite increment]
    [composite increment])

(defn get-composite [composite-cell]
    (first composite-cell))

(defn get-increment [composite-cell]
    (last composite-cell))</pre>
<p>What I&#8217;ve done here is use a vector to <em>abstract</em>/hide away the implementation details for a new data type, composite-cell. Composite-cell is made up of 2 parts, a composite and an increment.</p>
<pre>(defn insert-composite-cell [composite-cell composite-cell-queue]
    (let [composite (get-composite cell)
	  increment (get-increment cell)]
        (loop [new-composite composite]
	    (cond
		(contains? composite-cell-queue new-composite) (recur (+ new-composite increment))
		:else (assoc composite-cell-queue new-composite increment)))))</pre>
<p><em>insert-composite-cell</em> inserts a composite-cell into the priority queue, <em>contains?</em> checks to see if the key/priority already exists in the queue and <em>assoc</em> is used to add a new key-value pair to the queue.</p>
<pre>(defn make-prime-system [last-composite composite-cell-queue]
    {:last-composite last-composite
      :composite-cell-queue composite-cell-queue})

(defn get-last-composite [prime-system]
    (:last-composite prime-system))

(defn get-composite-cell-queue [prime-system]
    (:composite-cell-queue prime-system))</pre>
<p>Just like composite-cell, this uses the hash-table to implement a new data-type, prime-system. Prime system consists of the last-composite used and a list of composite-cells.</p>
<pre>(def initial-prime-system 3 (sorted-map 9 6))</pre>
<p>The initial prime system is the starting position for the algorithm</p>
<pre>(defn get-primes [prime-system]
    (let [last-composite (get-last-composite prime-system)]
  	   next-composite (get-composite (first (get-composite-cell-queue prime-system)))]
        (range (+ last-composite 2) next-composite 2)))</pre>
<p><em>get-primes</em> returns a list of primes given by the given prime-system</p>
<pre>(get-primes initial-prime-system)
=&gt; (5 7)</pre>
<p>And finally <em>next-prime-system</em></p>
<pre>(defn next-prime-system [prime-system]
    (let [primes (get-primes prime-system)
           queue (get-composite-cell-queue prime-system)
	   composite-cell (first queue)
	   composite (get-composite composite-cell)
	    increment (get-increment composite-cell)
	    queue (if (empty? primes)
	  		 queue
			 (apply assoc (cons queue (concat (map #(list (* 2 %) (* % %)) primes)))))]
	(make-prime-system composite
		           (insert-composite-cell (make-composite-cell (+ composite increment)
                                                                       increment)
		                                  (dissoc queue composite)))))</pre>
<p>Most of the <em>let</em> bindings in this function are to reduce calculations or improve readability. The last binding on queue performs an additional operation. If no primes are generated for the prime-system, the same queue is returned, otherwise the primes are added to the queue.</p>
<blockquote><p><em>assoc</em> appends key value pairs to a hash-map</p>
<pre>(assoc {:one 1} :two 2 :three 3)
=&gt; {:one 1 :two 2 :three 3}</pre>
<p><em>apply</em> applies a function on a list just like reduce, except the list is taken at once.</p>
<pre>(apply + '(1 2 3))
=&gt; 6</pre>
</blockquote>
<p>Just like <a title="Fibonacci" href="http://sidhantgodiwala.wordpress.com/2009/10/13/problem-2/">before</a>, for the fibonacci numbers, we&#8217;ll create an infinite list of primes</p>
<pre>(defn create-prime-list
    ([]
	(lazy-cat '(2 3) (create-prime-list initial-prime-system)))
    ([prime-system]
	(lazy-cat (get-primes prime-system) (create-prime-list (next-prime-system prime-system)))))

(def primes (create-prime-list))</pre>
<p>The solution to the problem, the 10001&#8242;st prime number</p>
<pre>(nth 10001 primes)</pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sidhantgodiwala.wordpress.com/139/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sidhantgodiwala.wordpress.com/139/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sidhantgodiwala.wordpress.com/139/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sidhantgodiwala.wordpress.com/139/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sidhantgodiwala.wordpress.com/139/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sidhantgodiwala.wordpress.com/139/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sidhantgodiwala.wordpress.com/139/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sidhantgodiwala.wordpress.com/139/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sidhantgodiwala.wordpress.com/139/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sidhantgodiwala.wordpress.com/139/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sidhantgodiwala.wordpress.com/139/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sidhantgodiwala.wordpress.com/139/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sidhantgodiwala.wordpress.com/139/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sidhantgodiwala.wordpress.com/139/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sidhantgodiwala.wordpress.com&amp;blog=9047704&amp;post=139&amp;subd=sidhantgodiwala&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://sidhantgodiwala.wordpress.com/2009/10/17/problem-7-primes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9f07870310e74733d399a66e4beea43b?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Sidhant Godiwala</media:title>
		</media:content>
	</item>
		<item>
		<title>Problem 6: Sums of Squares of Sums</title>
		<link>http://sidhantgodiwala.wordpress.com/2009/10/14/problem-6/</link>
		<comments>http://sidhantgodiwala.wordpress.com/2009/10/14/problem-6/#comments</comments>
		<pubDate>Wed, 14 Oct 2009 09:34:23 +0000</pubDate>
		<dc:creator>Sidhant Godiwala</dc:creator>
				<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[clojure]]></category>
		<category><![CDATA[summation]]></category>

		<guid isPermaLink="false">http://sidhantgodiwala.wordpress.com/?p=123</guid>
		<description><![CDATA[Difference between sum of squares and square of sums Find the difference between the sum of squares and the square of the sum of the integers 1 to 100 Using the functions map and reduce, this becomes trivial to implement (defn sqr [x] (* x x)) (let [terms (range 1 101)] (- (reduce + (map sqr [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sidhantgodiwala.wordpress.com&amp;blog=9047704&amp;post=123&amp;subd=sidhantgodiwala&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><strong>Difference between sum of squares and square of sums</strong></p>
<p>Find the difference between the sum of squares and the square of the sum of the integers 1 to 100</p>
<p>Using the functions <em>map</em> and <em>reduce</em>, this becomes trivial to implement</p>
<pre>(defn sqr [x]
    (* x x))

(let [terms (range 1 101)]
    (- (reduce + (map sqr terms)) (sqr (reduce + terms))))</pre>
<blockquote><p><em>map</em> takes a function and 1 or more lists and generates a new list of terms which are a result of the application of the function on each of the terms in the list/lists</p>
<pre>(map + '(1 2 3) '(1 2 3))
=&gt; (2 4 6)</pre>
</blockquote>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sidhantgodiwala.wordpress.com/123/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sidhantgodiwala.wordpress.com/123/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sidhantgodiwala.wordpress.com/123/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sidhantgodiwala.wordpress.com/123/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sidhantgodiwala.wordpress.com/123/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sidhantgodiwala.wordpress.com/123/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sidhantgodiwala.wordpress.com/123/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sidhantgodiwala.wordpress.com/123/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sidhantgodiwala.wordpress.com/123/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sidhantgodiwala.wordpress.com/123/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sidhantgodiwala.wordpress.com/123/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sidhantgodiwala.wordpress.com/123/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sidhantgodiwala.wordpress.com/123/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sidhantgodiwala.wordpress.com/123/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sidhantgodiwala.wordpress.com&amp;blog=9047704&amp;post=123&amp;subd=sidhantgodiwala&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://sidhantgodiwala.wordpress.com/2009/10/14/problem-6/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9f07870310e74733d399a66e4beea43b?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Sidhant Godiwala</media:title>
		</media:content>
	</item>
		<item>
		<title>Problem 5: Lowest Common Multiple</title>
		<link>http://sidhantgodiwala.wordpress.com/2009/10/14/problem-5/</link>
		<comments>http://sidhantgodiwala.wordpress.com/2009/10/14/problem-5/#comments</comments>
		<pubDate>Wed, 14 Oct 2009 08:48:24 +0000</pubDate>
		<dc:creator>Sidhant Godiwala</dc:creator>
				<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[clojure]]></category>
		<category><![CDATA[lcm]]></category>

		<guid isPermaLink="false">http://sidhantgodiwala.wordpress.com/?p=120</guid>
		<description><![CDATA[Find the smallest number divisible by each of the numbers 1 to 20 The requirement is to find the Lowest Common Multiple (LCM)of the numbers 1 to 20 We can very easily find the LCM using the  Greatest Common Divisor (GCD) using the formula lcm(x, y) = (x * y) / gcd(x, y) A very [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sidhantgodiwala.wordpress.com&amp;blog=9047704&amp;post=120&amp;subd=sidhantgodiwala&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><strong>Find the smallest number divisible by each of the numbers 1 to 20</strong></p>
<p>The requirement is to find the <a title="Lowest Common Multiple" href="http://en.wikipedia.org/wiki/Least_common_multiple">Lowest Common Multiple</a> (LCM)of the numbers 1 to 20</p>
<p>We can very easily find the LCM using the  <a title="Greatest Common Divisor" href="http://en.wikipedia.org/wiki/Greatest_common_divisor">Greatest Common Divisor</a> (GCD) using the formula</p>
<pre>lcm(x, y) = (x * y) / gcd(x, y)</pre>
<p>A very fast method to calculate GCD is using <a title="Euclid's Algorithm" href="http://en.wikipedia.org/wiki/Euclidean_algorithm">Euclid&#8217;s algorithm</a></p>
<pre>gcd(x, y) = gcd(y, x mod y)</pre>
<p>Implementing this in Clojure</p>
<pre>(defn gcd [x y]
    (cond
	(zero? x) y
	(zero? y) x
	:else (recur y (mod x y))))

(defn lcm [x y]
    (/ (* x y) (gcd x y)))

(reduce lcm (range 1 21))</pre>
<p><em>gcd</em> calculates the greatest common divisor of 2 integers</p>
<pre>(gcd 6 8 )
=&gt; 2</pre>
<p><em>lcm</em> calculates the lowest common multiple of 2 integers</p>
<pre>(lcm 6 8 )
=&gt; 24</pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sidhantgodiwala.wordpress.com/120/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sidhantgodiwala.wordpress.com/120/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sidhantgodiwala.wordpress.com/120/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sidhantgodiwala.wordpress.com/120/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sidhantgodiwala.wordpress.com/120/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sidhantgodiwala.wordpress.com/120/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sidhantgodiwala.wordpress.com/120/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sidhantgodiwala.wordpress.com/120/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sidhantgodiwala.wordpress.com/120/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sidhantgodiwala.wordpress.com/120/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sidhantgodiwala.wordpress.com/120/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sidhantgodiwala.wordpress.com/120/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sidhantgodiwala.wordpress.com/120/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sidhantgodiwala.wordpress.com/120/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sidhantgodiwala.wordpress.com&amp;blog=9047704&amp;post=120&amp;subd=sidhantgodiwala&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://sidhantgodiwala.wordpress.com/2009/10/14/problem-5/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9f07870310e74733d399a66e4beea43b?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Sidhant Godiwala</media:title>
		</media:content>
	</item>
		<item>
		<title>Problem 4: Numeric Palindromes</title>
		<link>http://sidhantgodiwala.wordpress.com/2009/10/14/problem-4/</link>
		<comments>http://sidhantgodiwala.wordpress.com/2009/10/14/problem-4/#comments</comments>
		<pubDate>Wed, 14 Oct 2009 05:31:06 +0000</pubDate>
		<dc:creator>Sidhant Godiwala</dc:creator>
				<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[clojure]]></category>
		<category><![CDATA[palindrome]]></category>

		<guid isPermaLink="false">http://sidhantgodiwala.wordpress.com/?p=116</guid>
		<description><![CDATA[Find the largest palindrome which is the product of 2, 3 digit numbers The challenge is to find a numeric palindrome which is the product of 2 numbers, both from 100 to 999. (defn reverse-int [n] (loop [m n acc 0] (cond (zero? ) acc :else (recur (quot m 10) (+ (* acc 10) (mod [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sidhantgodiwala.wordpress.com&amp;blog=9047704&amp;post=116&amp;subd=sidhantgodiwala&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><strong>Find the largest palindrome which is the product of 2, 3 digit numbers</strong></p>
<p>The challenge is to find a numeric palindrome which is the product of 2 numbers, both from 100 to 999.</p>
<pre>(defn reverse-int [n]
    (loop [m n acc 0]
	(cond
       	    (zero? ) acc
	    :else (recur (quot m 10) (+ (* acc 10) (mod m 1))))))

(defn palindrome? [n]
    (= n (reverse-int n)))

(defn euler-4 [lower-limit upper-limit]
    (loop [p upper-limit q upper-limit largest 0]
	(let [candidate (* p q)]
    	    (cond
		(&lt; p lower-limit) largest
		(&lt; candidate largest) largest
		(&lt; lower-limit q) (recur (dec p) (dec p) largest)
                (palindrome? candidate)
  	  	    (if (&gt; candidate largest)
			(recur p (dec q) candidate))
		:else (recur p (dec q) largest)))))

(euler-4 100 999)</pre>
<blockquote><p><em>let</em> like loop, forms a dynamic binding between the odd and even terms in the argument list for the expression in the body of the let.</p>
<pre>(let [a 3 b 4]
    (+ a b))
=&gt; 4</pre>
<p><em>if</em> takes 2 &#8211; 3 expressions as arguments, if the first one evaluates to be True, then the second (then) is evaluated, else the third (else) is evaluated. In this case there is no <em>else</em> clause, the control breaks out of the <em>if</em> statement</p>
<pre>(if (&lt; 2 3)
    4
    5)
=&gt; 4</pre>
</blockquote>
<p><em>reverse-int</em> takes an integer as argument and reverses it</p>
<pre>(reverse-int 234)
=&gt;432</pre>
<p><em>palindrome?</em> is a predicate which checks if an integer is a palindrome</p>
<pre>(palindrome? 121)
=&gt; True</pre>
<p>There are 2 optimizations in this solution</p>
<p>1&gt; Any 2 numbers are multiplied only once<br />
2&gt; If the largest palindrome found so far is larger than the current (* p q), return it</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sidhantgodiwala.wordpress.com/116/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sidhantgodiwala.wordpress.com/116/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sidhantgodiwala.wordpress.com/116/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sidhantgodiwala.wordpress.com/116/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sidhantgodiwala.wordpress.com/116/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sidhantgodiwala.wordpress.com/116/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sidhantgodiwala.wordpress.com/116/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sidhantgodiwala.wordpress.com/116/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sidhantgodiwala.wordpress.com/116/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sidhantgodiwala.wordpress.com/116/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sidhantgodiwala.wordpress.com/116/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sidhantgodiwala.wordpress.com/116/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sidhantgodiwala.wordpress.com/116/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sidhantgodiwala.wordpress.com/116/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sidhantgodiwala.wordpress.com&amp;blog=9047704&amp;post=116&amp;subd=sidhantgodiwala&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://sidhantgodiwala.wordpress.com/2009/10/14/problem-4/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9f07870310e74733d399a66e4beea43b?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Sidhant Godiwala</media:title>
		</media:content>
	</item>
	</channel>
</rss>
