<?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/"
	>

<channel>
	<title>Shaun Abram &#187; Uncategorized</title>
	<atom:link href="http://www.shaunabram.com/category/uncategorized/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.shaunabram.com</link>
	<description>Java and Technology weblog</description>
	<lastBuildDate>Tue, 27 Jul 2010 23:14:10 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Project Euler: Problem 6 (in Java)</title>
		<link>http://www.shaunabram.com/euler6/</link>
		<comments>http://www.shaunabram.com/euler6/#comments</comments>
		<pubDate>Wed, 31 Mar 2010 07:17:40 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[euler]]></category>

		<guid isPermaLink="false">http://www.shaunabram.com/?p=798</guid>
		<description><![CDATA[Problem 6 in Project Euler is as follows: The sum of the squares of the first ten natural numbers is, 1^(2) + 2^(2) + &#8230; + 10^(2) = 385 The square of the sum of the first ten natural numbers is, (1 + 2 + &#8230; + 10)^(2) = 55^(2) = 3025 Hence the difference [...]]]></description>
			<content:encoded><![CDATA[<p>Problem 6 in <a href="http://projecteuler.net/">Project Euler</a> is as follows:</p>
<blockquote><p>The sum of the squares of the first ten natural numbers is,<br />
1^(2) + 2^(2) + &#8230; + 10^(2) = 385</p>
<p>The square of the sum of the first ten natural numbers is,<br />
(1 + 2 + &#8230; + 10)^(2) = 55^(2) = 3025</p>
<p>Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 − 385 = 2640.</p>
<p>Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.
</p></blockquote>
<p><span id="more-798"></span></p>
<h4>Brute Force Approach</h4>
<p>This was an unusual problem for Project Euler as the brute force approach was both easy to code and ran well within the time constraints (6 milliseconds on my laptop). My first solution was as follows:</p>
<pre>
<code>	public int euler6(int n) {
		int sumOfSquares = 0;
		int squareOfSum = 0;
		for (int i=1; i&lt;=n; i++) {
			sumOfSquares +=  (i*i);
			squareOfSum += i;
		}
		squareOfSum *= squareOfSum;

		int ans = squareOfSum-sumOfSquares;
		return ans;
	}</code>
</pre>
<p>This brute force approach (with a <a href="http://en.wikipedia.org/wiki/Big_O_notation">Big-O analysis</a> of O(n)) took just 5 milliseconds on my laptop. Given that that time limit is 1 minute, there wasn&#8217;t a lot of motivation for finding a better solution.</p>
<p>None the less, I started analyzing the numbers, looking for patterns etc but I have to be honest, I couldn&#8217;t find much. So, I started Googling!</p>
<h4>Key 1</h4>
<p>First I came across the work of Gauss. I have to admit I have never heard of his work before, but clearly he was much smarted than me.</p>
<p>At the age of only 10, he figured out that the sum of all numbers up to n can be calculated at n(n+1)/2<br />
e.g. 1+2+3+4+5+6+7+8+9+10 = 10(5)/2=25<br />
There are a couple of good pages about this <a href="http://www.billthelizard.com/2009/01/gauss.html">here</a> and <a href="http://www.ugrad.math.ubc.ca/coursedoc/math101/notes/integration/sums.html">here</a>.</p>
<h4>Key 2</h4>
<p>Next, I found this very cool formula for the sum of squares:<br />
For a number n, 1^2 + 2^2 + 3^2 +&#8230;. n^2 =<br />
n(n + 1)(2n + 1)/6</p>
<p>e.g. the sum of squares<br />
=1+4+9+16+25+36+49+64+91+100=385<br />
Or<br />
=10(10+1)(20+1)/6<br />
=10(11)(21)/6<br />
=110(21)/6<br />
=2310/6<br />
=385</p>
<p>Magic!<br />
I found the best explanation of this <a href="http://www.mathisfunforum.com/viewtopic.php?id=2014">here</a>.</p>
<h4>Final Solution</h4>
<p>So, given those 2 formulae, we can rewrite out Project Euler #6 solution as:</p>
<pre>
<code>	public int euler6(int n) {
		int sumOfSquares = n* (n + 1) * (2*n + 1) /6;
		int sum = n*(n+1)/2;
		int squareOfSum = sum*sum;
		int ans = squareOfSum-sumOfSquares;
		return ans;
	}</code>
</pre>
<p>Which runs in constant time or O(1).<br />
I would never have figured out those formulae myself, but they are pretty cool to know!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.shaunabram.com/euler6/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Euler: Problem 5 (in Java)</title>
		<link>http://www.shaunabram.com/euler5/</link>
		<comments>http://www.shaunabram.com/euler5/#comments</comments>
		<pubDate>Mon, 15 Mar 2010 02:34:17 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[euler]]></category>

		<guid isPermaLink="false">http://www.shaunabram.com/?p=771</guid>
		<description><![CDATA[Problem 5 in Project Euler is as follows: 2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. What is the smallest number that is evenly divisible by all of the numbers from 1 to 20? As usual, I started with the brute force [...]]]></description>
			<content:encoded><![CDATA[<p>Problem 5 in <a href="http://projecteuler.net/">Project Euler</a> is as follows:</p>
<blockquote><p>2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.</p>
<p>What is the smallest number that is evenly divisible by all of the numbers from 1 to 20?
</p></blockquote>
<p><span id="more-771"></span><br />
As usual, I started with the brute force approach:</p>
<pre>
<code>	int max=20;
	long candidate = 1;
	boolean success = false;
	do {
		int i;
		for (i=1; i&lt;=max; i++) {
			if ((candidate % i) !=0) {
				//out.println(candidate + " not divisible by " + i);
				candidate=candidate+1;
				break;
			}
		}
		if (i&gt;max) success = true;
	} while (!success);
	out.println("Success: " + candidate);</code>
</pre>
<p>This is clearly a poor solution (and at ~25 seconds run time on my laptop, a very slow one).<br />
A few improvement points:<br />
1) The result has to be divisible by all numbers up to and including 20. So no point checking any number below 20. So we could change the starting candidate value to 20. But using the info given, we know that 2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. So the solution can&#8217;t be smaller than 2520. Let&#8217;s start there.<br />
2) Any number has to be divisible by 20, so we can increment candidate by 20 each time.<br />
3) If a candidate is divisible by 20, it is definitely divisible by 10, so don&#8217;t even bother checking 10. If a number is divisible by 18, it is definitely devisible by 9, so don&#8217;t bother checking 9. Continue this process and we find that we only need to check if a candidate number is divisible by 11 through 19 (we are starting with a multiple of 20 and incrementing by 20 each time, so 20 as a factor is a given).<br />
This leaves us with this slightly better solution:</p>
<pre>
<code>	int min = 11;
	int max=19;
	long candidate = 2520;
	boolean success = false;
	do {
		int i;
		for (i=min; i&lt;=max; i++) {
			if ((candidate % i) !=0) {
				//out.println(candidate + " not divisible by " + i);
				candidate=candidate+20;
				break;
			}
		}
		if (i&gt;max) success = true;
	} while (!success);
	out.println("Success: " + candidate);</code>
</pre>
<p>This give the same solution, but with about 400 milliseconds running time. Better, and well within the 1 minute Project Euler limit.<br />
I submitted my solution and confirmed it was correct.<br />
Great. But I knew my solution was still a brute force approach at heart.</p>
<p>As I was working through the algorithm, I began thinking about using a better starting point than 2520. For example, the smallest solution must be a number that is divisible by both 19 and 20. What is that? If I had a method that told me the lowest common multiple of 19 and 20?<br />
As I started reading through other solutions, I began thinking more about that. If I had a function getLowestCommonMultipler(a, b)<br />
I could apply that to the first 2 numbers (1 and 2).<br />
Then use the result for a subsequent call, e.g.  getLowestCommonMultipler(result, 3)<br />
Then use the result for a subsequent call, e.g.  getLowestCommonMultipler(result, 4)<br />
and so on.<br />
You would end with a solution like this</p>
<pre>
<code>	int max=20;
	long lowestCommonMultipler = 1;
	for (int i=2; i&lt;=max; i++) {
		lowestCommonMultipler = getLowestCommonMultipler(lowestCommonMultipler, i);
	}
	out.println("Success: " + lowestCommonMultipler);</code>
</pre>
<p>I admit that I had to get &#8216;inspiration&#8217; for the implementation of the getLowestCommonMultipler() method elsewhere (see <a href="http://basildoncoder.com/blog/2008/06/10/project-euler-problem-5/">here</a>, for example), so I won&#8217;t post the implementation as it is not my own work. But identifying the need for such a method is the big breakthrough I think. The above solution ran in 6ms for me. Cool in a very geeky, Project Euler kind of way.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.shaunabram.com/euler5/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Hiatus</title>
		<link>http://www.shaunabram.com/hiatus/</link>
		<comments>http://www.shaunabram.com/hiatus/#comments</comments>
		<pubDate>Mon, 15 Mar 2010 02:32:01 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.shaunabram.com/?p=780</guid>
		<description><![CDATA[It&#8217;s been a while since I posted last. Wedding planning (7 weeks to go&#8230;) and work have been getting the better of me. I have some ideas for future posts, but until I get the wedding out of the way, things are likely to be quieter. But I will post my next Project Euler solution [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s been a while since I posted last. Wedding planning (7 weeks to go&#8230;) and work have been getting the better of me. I have some ideas for future posts, but until I get the wedding out of the way, things are likely to be quieter. But I will post my next <a href="http://projecteuler.net/">Project Euler</a> solution soon at least&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.shaunabram.com/hiatus/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Euler: Problem 4 (in Groovy)</title>
		<link>http://www.shaunabram.com/euler4/</link>
		<comments>http://www.shaunabram.com/euler4/#comments</comments>
		<pubDate>Fri, 13 Nov 2009 09:30:35 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[euler]]></category>
		<category><![CDATA[groovy]]></category>

		<guid isPermaLink="false">http://www.shaunabram.com/?p=630</guid>
		<description><![CDATA[Problem 4 in Project Euler is as follows: A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99. Find the largest palindrome made from the product of two 3-digit numbers. I found this problem significantly easier than problem 3, thankfully. [...]]]></description>
			<content:encoded><![CDATA[<p>Problem 4 in <a href="http://projecteuler.net/">Project Euler</a> is as follows:</p>
<blockquote><p>A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.</p>
<p>Find the largest palindrome made from the product of two 3-digit numbers.
</p></blockquote>
<p>I found this problem significantly easier than problem 3, thankfully.</p>
<p>My first step was to write a helper method:</p>
<pre>
<code>	boolean isPalindrome(int num)</code>
</pre>
<p>My first attempt was, as usual, the brute force approach. Namely, go through every number from 100 to 999 and for each, multiply it by every number between 100 and 999. Check if the result of each of the roughly one million calculations is a palindrome &#8211; and store it if it is the biggest one yet.</p>
<p>My next approach was better and involved starting from the top (999 to 100) instead of the other way round. This means we can check if the largest palindrome we could possible find is smaller than what we have already found, thereby avoiding a large number of useless calculations.</p>
<p>This gave me a solution that runs in approx 500ms on my local PC.</p>
<p>Here is my final solution:</p>
<pre>
<code>int largestPalindrome = 0;
for (i in 999..100) {
	if ( (i*i) &lt; largestPalindrome) {
		//the largest possible number we can now find (i*i)
		//is smaller than the biggest palindrome already found
		break;
	}
	for (j in i..100) {
		int candidate = i*j;
		if ( candidate &lt; largestPalindrome) {
			//largest number we can now find in this inner loop
			//is smaller than the biggest palindrome already found
			break;
		}
		if (isPalindrome(candidate)) {
			if (candidate&gt;largestPalindrome) {
				largestPalindrome = candidate;
				break;//no point checking smaller numbers of j
			}
		}
	}
}
println("largestPalindrome);</code>
</pre>
<p>And finally, my isPalindrome method:</p>
<pre>
<code>public static boolean isPalindrome(Integer num) {
	boolean isPalindrome=true;
	char[] numChars = num.toString().toCharArray();
	int endPoint = numChars.length() - 1;
	int midPoint = numChars.length() / 2;
	for (i in 0..midPoint) {
		char a = numChars[i];
		char b = numChars[endPoint-i];
		if (a != b) {
			isPalindrome=false;
			break;
		}
	}
	return isPalindrome;
}</code>
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.shaunabram.com/euler4/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Euler: Problem 3</title>
		<link>http://www.shaunabram.com/euler3/</link>
		<comments>http://www.shaunabram.com/euler3/#comments</comments>
		<pubDate>Tue, 11 Aug 2009 01:16:17 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[euler]]></category>

		<guid isPermaLink="false">http://www.shaunabram.com/?p=509</guid>
		<description><![CDATA[Problem 3 in Project Euler is as follows: The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 600851475143 ? I noticed some performance issues with Groovy when I was working on this solution. I will blog about my findings later (Update: see here), suffice [...]]]></description>
			<content:encoded><![CDATA[<p>Problem 3 in <a href="http://projecteuler.net/">Project Euler</a> is as follows:</p>
<blockquote><p>The prime factors of 13195 are 5, 7, 13 and 29.<br />
What is the largest prime factor of the number 600851475143 ?</p></blockquote>
<p>I noticed some performance issues with Groovy when I was working on this solution. I will blog about my findings later (Update: see <a href="http://www.shaunabram.com/groovy-slow/">here</a>), suffice to say I am switching to straight Java for this solution.</p>
<p>After experimenting with a brute force approach, I finally came up with this solution. It is not the most elegant, but it works (in <100ms).</p>
<pre><code>long x = 600851475143L;
long max = x / 2;
long factor = x;
long lastFactor = x;
for (long i=2; i&lt;max; i++) {
	long remainder = x % i;
	lastFactor = factor;
	factor = x/i;
	if ( (remainder == 0) &amp;&amp; (eule3.isPrime(factor)) ) {
		printAndExit(factor);
	}			

	if (factor &gt;= lastFactor-1) {
		for (long j=factor; j&gt;2; j--) {
			i++;
			remainder = x % j;
			if (remainder == 0) {
				if (eule3.isPrime(j)) {
					printAndExit(j);
				}
			}
		}
	}
}</code></pre>
<p>There are 2 main parts to the solution. The first loop starts at x/2 (since no factor of x can be greater than x/2). Basically we are checking if x/2 (rounded) is a factor of x. Then 3, then 4 etc. And when we find a number that is a factor, we check if it is prime (if so, we stop obviously). So, for example, we check if 50, 33, 25 are factors of x.</p>
<p>This approach works great, until a certain point, which is when we switch into the second loop. Basically, it becomes more inefficient to start checking if sequential numbers are factors of x.</p>
<p>Again, not the most efficient algorithm, or very well explained! But I have spent enough time on this one.</p>
<p>On to <a href="http://projecteuler.net/index.php?section=problems&#038;id=4">Problem 4</a>...</p>
]]></content:encoded>
			<wfw:commentRss>http://www.shaunabram.com/euler3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Euler, Problem 2 (in Groovy/Java)</title>
		<link>http://www.shaunabram.com/euler2/</link>
		<comments>http://www.shaunabram.com/euler2/#comments</comments>
		<pubDate>Mon, 27 Jul 2009 00:26:58 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[euler]]></category>
		<category><![CDATA[groovy]]></category>

		<guid isPermaLink="false">http://www.shaunabram.com/?p=482</guid>
		<description><![CDATA[Problem 2 in Project Euler is as follows: 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, &#8230; Find the sum of all the even-valued terms in [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://projecteuler.net/index.php?section=problems&#038;id=2">Problem 2</a> in Project Euler is as follows:</p>
<blockquote><p>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:</p>
<p>1, 2, 3, 5, 8, 13, 21, 34, 55, 89, &#8230;</p>
<p>Find the sum of all the even-valued terms in the sequence which do not exceed four million.</p></blockquote>
<p>I found it fairly easy to code a basic solution to this problem:</p>
<pre>
<code>
	    def fibSeries = [0,1]
	    int sumOfEvenNums = 0
	    int i = 0
	    while (fibSeries[i]&lt;=4000000) {
	       i = fibSeries.size
	       fibSeries[i] = fibSeries[i-1]+fibSeries[i-2]
	       if (fibSeries[i]%2==0) {
	    	   sumOfEvenNums+=fibSeries[i]
	       }
	    }
	    println("Total = " + sumOfEvenNums)</code>
</pre>
<p>This ran relatively quickly (~100ms), so I submitted my answer on <a href="http://projecteuler.net">projecteuler.net</a> and it was confirmed as correct.</p>
<p>With heinsight, I realise that it is unnecessary for me to store the entire sequence of numbers (in fibSeries). I just need to store 3 numbers: the latest and the previous 2.</p>
<p>After checking the solution notes, there is of course a more &#8216;perfect&#8217; solution. Reaching it involves making 2 breakthroughs, which I admit I didn&#8217;t make on my own! </p>
<p><b>Breakthrough 1</b><br />
The first breakthrough is to see the pattern (how could I have missed it!) that only every 3rd number is even, meaning we can safely ignore the rest. The provided solution says that this is easy to prove (I have no idea how to &#8211; perhaps a step for another day). But assuming this to be true, the code can be rewritten to</p>
<pre>
<code>	    int a,b,c
	    b = 1 //1st seed value
	    c = 1 //2nd seed value
	    int sumOfEvenNums = 0
	    while (c&lt;=4000000) {
	       a=b+c
	       b=c+a
	       c=a+b
	       sumOfEvenNums+=a
	    }
	    println("Total = " + sumOfEvenNums)</code>
</pre>
<p><b>Breakthrough 2</b><br />
The second, and waaay more difficult breakthrough is to spot (and prove!) that there is a pattern in the even numbers. If you look at them,<br />
2, 8, 34, 144, 610, &#8230;<br />
They obey following recursive relation:<br />
E(n)=4*E(n-1)+E(n-2).<br />
e.g. 144=4(34) + 8<br />
and 34 = 4(8) + 2<br />
It can be proved that for the Fibonacci numbers, the formula F(n)=4*F(n-3)+F(n-6) holds true (I even managed to get the proof of this myself, after a little prodding).</p>
<p>So, this leaves the following &#8216;ultimate&#8217; solution:</p>
<pre>
<code>	    int a = 0
	    int b = 8 //1st seed value
	    int c = 2 //2nd seed value
	    int sumOfEvenNums = b+c
	    while (true) {
	       a=(4*b)+c
	       if (a&gt;4000000) break
	       sumOfEvenNums+=a
	       c=b
	       b=a
	    }
	    println("Total = " + sumOfEvenNums)</code>
</pre>
<p>I am sure this could even be rewritten in a more elegant form. Especially if Groovy supported a do..while loop.</p>
<p>Anyway, on to <a href="http://projecteuler.net/index.php?section=problems&#038;id=3">Problem#3</a>&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.shaunabram.com/euler2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Find files in Unix</title>
		<link>http://www.shaunabram.com/find-files-in-unix/</link>
		<comments>http://www.shaunabram.com/find-files-in-unix/#comments</comments>
		<pubDate>Thu, 23 Jul 2009 23:06:33 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[unix]]></category>

		<guid isPermaLink="false">http://www.shaunabram.com/?p=475</guid>
		<description><![CDATA[I frequently end up searching an entire directory for an elusive file and I can never remember the exact command structure, so posting here: find /dir/to/search -name "filename.ext" -print 2&#62;/dev/null And wildcards are allowed. e.g. find /dir/to/search -name "filename.*" -print 2&#62;/dev/null The command searches /dir/to/search AND all sub directories. The &#8217;2>/dev/null&#8217; avoids those annoying &#8220;find: [...]]]></description>
			<content:encoded><![CDATA[<p>I frequently end up searching an entire directory for an elusive file and I can never remember the exact command structure, so posting here:</p>
<pre><code>    find /dir/to/search -name "filename.ext" -print 2&gt;/dev/null</code></pre>
<p>And wildcards are allowed. e.g. </p>
<pre><code>    find /dir/to/search -name "filename.*" -print 2&gt;/dev/null</code></pre>
<p>The command searches /dir/to/search AND all sub directories.</p>
<p>The &#8217;2>/dev/null&#8217; avoids those annoying &#8220;find: cannot read dir &#8230;: Permission denied&#8221; errors.</p>
<pre><code>
</code></pre>
<p>Also, if you want to search for files containing specific text, try</p>
<pre><code>    find /dir/to/search -exec grep -il "txtToSearchFor" {} \;</code></pre>
<p>The &#8220;-il&#8221; means ignore case and print only the names of files  with matching lines (as opposed to the line contents).</p>
<pre><code> </code></pre>
<p>See also:<br />
<a href="http://en.wikipedia.org/wiki/Find">http://en.wikipedia.org/wiki/Find</a><br />
<a href="http://www.unix.com/unix-dummies-questions-answers/3152-find-files-containing-text.html">http://www.unix.com/unix-dummies-questions-answers/3152-find-files-containing-text.html</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.shaunabram.com/find-files-in-unix/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Euler, Problem 1 (in Groovy/Java)</title>
		<link>http://www.shaunabram.com/euler-2/</link>
		<comments>http://www.shaunabram.com/euler-2/#comments</comments>
		<pubDate>Sun, 19 Jul 2009 23:53:33 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[euler]]></category>
		<category><![CDATA[groovy]]></category>

		<guid isPermaLink="false">http://www.shaunabram.com/?p=437</guid>
		<description><![CDATA[I have been wanting to get up to speed with Groovy for while but hadn&#8217;t really found a good excuse. So when I came across Project Euler, I decided to try to solve the problems using Groovy. I managed to solve Problem#1 today. Note that I would describe the solutions below as being in Groovy/Java [...]]]></description>
			<content:encoded><![CDATA[<p>I have been wanting to get up to speed with <a href="http://groovy.codehaus.org/">Groovy</a> for while but hadn&#8217;t really found a good excuse. So when I came across <a href="http://projecteuler.net/">Project Euler</a>, I decided to try to solve the problems using Groovy.</p>
<p>I managed to solve <a href="http://projecteuler.net/index.php?section=problems&#038;id=1">Problem#1</a> today.</p>
<p>Note that I would describe the solutions below as being in Groovy/Java because I still fall back on my old Java habits rather than using all the Groovy language constructs available to me. Anyway&#8230;</p>
<p>The problem is:</p>
<blockquote><p>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.</p></blockquote>
<p>Unsurprisingly, I took the brute force approach first:</p>
<pre>
<code>int n = 999, total=0;
for (int i in 1..n) {
    if ( (i%3==0) || (i%5==0) ) {
        total=total+i;
    }
}
println("Total=" + total)</code>
</pre>
<p>A good first start, but far from perfect as it loops through every number between 1 and n. I could improve it slightly by tweaking the start and end points (e.g. starting at 3), but it would still roughly involve n iterations.<br />
My second attempt was this:</p>
<pre>
<code>int n = 999, total=0;
Set nums = new HashSet();
for (int i=3; i&lt;n+1; i=i+3) {
    if (nums.add(i)) total=total+i;
}
for (int i=5; i&lt;n+1; i=i+5) {
     if (nums.add(i)) total=total+i;
}
println("Total="+total)</code>
</pre>
<p>Note the use of the HashSet to avoid counting duplicates (i.e. numbers divisible by 3 <em>and</em> 5, like 15, 30 etc). Obviously the code could be tidied up but this reduced the number of loop iterations from 999 to 532 (and the run time from ~110ms to ~90ms).</p>
<p>However, the code/algorithm is still imperfect as although it doesn&#8217;t count numbers divisible by 3 <em>and</em> 5 twice, it still iterates over them twice. I couldn&#8217;t figure out a way to avoid this, so having met the requirements (I verified the correct answer on the Euler site and, at ~90ms, it runs well under one second), I submitted my answer and started reading the supplied solution.</p>
<p>The &#8216;perfect&#8217; solution involves 3 breakthroughs.<br />
The first is that the ideal solution involves using formulae, not iteration.<br />
The second is that in the example of the total all numbers up to 1000 divisible by 3<br />
i.e.<br />
3+6+9+ &#8230; +999<br />
= 3*(1+2+3+&#8230;+1000/3)<br />
where 1000 divided by 3 is rounded down.</p>
<p>The 3rd and final breakthrough required is that:<br />
1+2+3+&#8230;+y<br />
= (y/2)*(y+1)</p>
<p>I have to admit that I am not sure I would have ever made these break throughs myself!</p>
<p>But using these facts, the ideal solution is along the following lines:</p>
<pre>
<code>	static int max = 999;

	static void main(def args) {
	  int total = sumDivisibleBy(3)+sumDivisibleBy(5)-sumDivisibleBy(15)
	  println("\nTotal="+total)
	}

	static int sumDivisibleBy(n) {
	  int p=max / n
	  return n*(p*(p+1)) / 2
	}</code>
</pre>
<p>This solution reduced the run time from ~90ms to ~20ms.</p>
<p>Next up, <a href="http://www.shaunabram.com/euler-2/">Problem#2</a>&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.shaunabram.com/euler-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Spec&#8217;ing and Pricing Client Projects</title>
		<link>http://www.shaunabram.com/pricing-client-projects/</link>
		<comments>http://www.shaunabram.com/pricing-client-projects/#comments</comments>
		<pubDate>Sun, 12 Jul 2009 17:20:40 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[consulting]]></category>

		<guid isPermaLink="false">http://www.shaunabram.com/?p=405</guid>
		<description><![CDATA[One of the toughest tasks on a project can be coming up with estimates, so I was interested to see this blog post quoting some of Joel Spolsky&#8216;s thoughts on creating specifications and coming up with quotes for client work, which I think makes a lot of sense.]]></description>
			<content:encoded><![CDATA[<p>One of the toughest tasks on a project can be coming up with estimates, so I was interested to see <a href="http://blog.lylo.co.uk/2009/06/04/how-to-win-well-priced-client-work/">this blog post</a> quoting some of <a href="http://www.joelonsoftware.com/">Joel Spolsky</a>&#8216;s thoughts on creating specifications and coming up with quotes for client work, which I think makes a lot of sense.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.shaunabram.com/pricing-client-projects/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Back from Hiatus</title>
		<link>http://www.shaunabram.com/back-from-hiatus/</link>
		<comments>http://www.shaunabram.com/back-from-hiatus/#comments</comments>
		<pubDate>Mon, 22 Jun 2009 01:53:21 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.shaunabram.com/?p=310</guid>
		<description><![CDATA[It&#8217;s been a while since I have posted here; The last 2 months have been hectic. I started a new project at work (I&#8217;m &#8220;Tech Lead&#8221;, on site at a major bank here in California, using Java/XML/Spring), took a vacation in Europe, did a best man&#8217;s speech at a friends wedding, and completed the Escape [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s been a while since I have posted here; The last 2 months have been hectic. I started a new project at work (I&#8217;m &#8220;Tech Lead&#8221;, on site at a major bank here in California, using Java/XML/Spring), took a vacation in Europe, did a best man&#8217;s speech at a friends wedding, and completed the <a href="http://www.escapefromalcatraztriathlon.com">Escape From Alcatraz</a> triathlon. Been a busy time!</p>
<p>I have a few ideas for upcoming articles, and I am also thinking about taking the Spring Certification exam. If so, I will be sure to post about it here&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.shaunabram.com/back-from-hiatus/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
