Git

Git Immersion

Got GitI’ve begun tackling my 2011 technology resolutions. It was as if the good folks at EdgeCase read my blog and deigned to help a brotha out. They’ve recently launched Git Immersion, which does for Git what their Koans did for the masses. That is, it offers a simple, step-by-step way for someone to learn a technology.

I already learned something. Doing Lab 3, it says to issue this command

git commit -m 'First Commit'

When I do, I get this error

error: pathspec 'Commit'' did not match any file(s) known to git.

A little of my Google-Fu and I find that my windows command prompt is anti me using the ‘ here. I change the command to

git commit -m "First Commit"

and I get the right result

[master (root-commit) d23d927] First Commit
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 hello.rb

Another thing I learned. When I typed “git commit” with no -m (Lab 8), it brought up VIM. Guess what? I’m such a n00b that I couldn’t figure out how to save my freaking file. Thank goodness for StackOverflow here to tell me to hit Esc then type :wq and then the enter key.

That’s it of my revelations so far. As I’m writing this, I am through the first 10 labs and will do some more after I post this. I’m really looking forward to learning Git because I like the way that it encourages you to work. On to some more learning…

Goals

2011 Technology Resolutions

ResolutionsI try to learn at least one new thing every year in the technology field. In 2010, I learned (and build a production application from) Asp.Net MVC 2.0. I also learned Windows Phone 7 development and even got an app in the Marketplace called Nerd News. You can see a little more about it here or by searching for Nerd News in the Marketplace.

In 2011, I’m beginning the year with the goal of learning more about Node.js, Socket.IO, and Git. I had hoped that CodeMash might have some Node stuff this year, but it doesn’t. Maybe I can find or start an open space about it…

Do any of you try to learn a new technology every year? If so, what are yours for 2011?

Project Euler

Project Euler Problem 4

TACOCAT is a palindrome!Continuing on from my previous adventures in Euler, we now come to problem 4. Problem 4 asks you to find the largest palindrome that is the product of two 3 digit numbers.

My solution is brute force, and originally, I started two loops both at 999 and counted down, figuring the first palindrome found would be the largest. I was wrong, however, and was required (sticking to brute force) to check all products for palindromes and just keep the largest. The “count backwards” code returned “Using 995 and 583, the max palindrome is 580085”, which is incorrect. My correct code is as follows:

using System;

namespace ProjectEuler
{
	/// <summary>
	/// A palindromic number reads the same both ways. The largest
	/// palindrome made from the product of two 2-digit numbers is 
	///  9009 = 91 x 99.
	/// Find the largest palindrome made from the product of two 3-digit numbers.
	///  http://projecteuler.net/index.php?section=problems&id=43
	///
	/// The answer is 906609
	/// </summary>
	public class Problem4
	{
		public static void Main(string[] args)
		{
			var max = 0;
			var theI = 0;
			var theK = 0;
			
			for (var i = 100; i <= 999; i++)
			{
				for (var k=100; k <= 999; k++)
				{
					var product = i * k;
					if (IsPalindrome(product) && product > max)
					{
						theI = i;
						theK = k;
						max = product;
					}
				}
			}

			Console.WriteLine("Using {0} and {1}, the max palindrome is {2}", theI, theK, max);
		}

		public static bool IsPalindrome(int number)
		{
			var forward = number.ToString();
			var reverse = forward.ToCharArray();
			Array.Reverse(reverse);

			return forward == new string(reverse);
		}
	}
}

That returns “Using 913 and 993, the max palindrome is 906609”.

After you put in the correct answer on the Project Euler site, you are allowed to then view the forums to discuss your answers. I found some interesting math inside that made other algorithms much more efficient. Here is what I gleaned:

A six digit palindrome would be in the format abccba. If we take our real answer of 906609, that could also be written as 9(100000) + 0(10000) + 6(1000) + 6(100) + 0(10) + 9(1). The same way, our generic answer can be written as 100000a+10000b+1000c+100c+10b+1a. Simplified again, that is 100001a+10010b+1100c. You can factor 11 out of that, leaving you with 11(9091a + 910b + 100c). That means that the product would have to be easily divisible by 11 (saving you the lengthy – by comparison – palindrome check). My original algorithm ran in 9.219022 seconds on my Mac Mini running Mono. When I add in the “divide by 11 check” to short-circuit every palindrome comparison, the algorithm now runs in 1.63679 seconds. That is a HECK of an improvement. Math… I think this might just catch on!

Project Euler

Project Euler Problem Three

Optimus PrimeIt has been almost two years since I tackled Project Euler Problems One and Two. I really wanted to get back into it, so there really is no time like the present to do the work. I’m on my Mac Mini right now, so I developed this in C#.Net using MonoDevelop.

This problem needs us to find the largest prime factor of a very large number. Obviously, to do this, you need to be able to generate a set of prime numbers to work with. I have an IsPrime method that uses a very brute force method (with the shortcut of only checking as high as the square root of the number). I Googled around for ways to generate primes and looked into the Sieve of Eratosthenes, but it was a little complicated for the time I had allotted myself to work on this problem (basically the ten minutes until I had to put my son to bed). Plus, it turns out that this entire piece of code runs in under a second on my several year old Mac Mini, so there was no need to optimize yet. I know that later Project Euler problems also deal in primes, so I may need to break it out then.

Once I had that method in place, it was really a simple matter of working up to the square root of the number in the problem, 600851475143, using the same shortcut. If the number in my loop was prime and evenly divided into our number, I stored it as the current largest number. When I was done, whatever number was currently in that variable was our champion. Pretty simple logic.


using System;

namespace ProjectEuler
{
	/// <summary>
	/// The prime factors of 13195 are 5, 7, 13 and 29.
	///  What is the largest prime factor of the number 600851475143 ?
	/// http://projecteuler.net/index.php?section=problems&id=3
	///
	/// The answer is 6857
	/// </summary>
	public class Problem3
	{
		protected static double number = 600851475143;
		
		public static void Main(string[] args)
		{
			double limit = Math.Floor(Math.Sqrt(number));
			double currentLargestPrime = 0;

			for (double i = 2; i <= limit; i++)
			{
				if (IsPrime(i) && (number % i == 0))
				{
					currentLargestPrime = i;
				}
			}			
			
			Console.WriteLine(currentLargestPrime);
		}

		public static bool IsPrime(double n)
		{
			if (n%2 == 0) return false;

			var upperLimit = Math.Floor(Math.Sqrt(n));

			for (double i = 3; i <= upperLimit; i++)
			{
				if (n % i == 0) return false;
			}

			return true;
		}
	}
}

Have you attempted this problem yet? What is your solution? I’d love to see them. You can post it in the comments or post a link if you’ve blogged it already.

Windows Phone 7

Windows Phone 7 Launch Event

I’m excited about the new Windows Phone 7 device, especially developing for it. I’ve written two blogs posts about it as of this date (Filed here) and I plan on writing many more.

I also plan on buying a Windows Phone 7 when it launches and getting as many apps as I can in the marketplace.

Microsoft has long shown that it is very focused on developers and always does launch events up big. As such, the launch events that are available here:

Windows Phone 7 Launch Event - Click for Details (Edit: Link removed, since it was taken down)

are sure to be awesome.

I’ve also added a small banner in my sidebar to stay up while the events are going off for two reasons. 1) There is a contest and 2) My hope is that a lot of developers will get excited and get tons of fantastic apps in the marketplace.

Go download the tools and let’s make this platform great!