<?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>James McMinn &#187; Computer Science</title>
	<atom:link href="http://jamesmcminn.com/category/computer-science/feed/" rel="self" type="application/rss+xml" />
	<link>http://jamesmcminn.com</link>
	<description>My little piece of the web</description>
	<lastBuildDate>Tue, 18 May 2010 19:13:58 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>An introduction to Binary</title>
		<link>http://jamesmcminn.com/2010/01/25/an-introduction-to-binary/</link>
		<comments>http://jamesmcminn.com/2010/01/25/an-introduction-to-binary/#comments</comments>
		<pubDate>Mon, 25 Jan 2010 23:00:38 +0000</pubDate>
		<dc:creator>James McMinn</dc:creator>
				<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[University]]></category>

		<guid isPermaLink="false">http://jamesmcminn.com/?p=263</guid>
		<description><![CDATA[The Binary, or base-2 number system,  is represented by the symbols 1 and 0. It&#8217;s used in most digital electronic circuits and all desktop computers in the world. Because of this, a basic understanding of binary is extremely important to a computer scientist.
Why Binary and not Decimal?
Everyone&#8217;s familiar with Decimal. It&#8217;s the number system we [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignright size-full wp-image-336" src="http://jamesmcminn.com/wp-content/uploads/2010/01/binary21.png" alt="Some blurry binary" width="250" height="200" />The Binary, or base-2 number system,  is represented by the symbols 1 and 0. It&#8217;s used in most digital electronic circuits and all desktop computers in the world. Because of this, a basic understanding of binary is extremely important to a computer scientist.</p>
<h2>Why Binary and not Decimal?</h2>
<p>Everyone&#8217;s familiar with Decimal. It&#8217;s the number system we all use every day &#8211; and for good reason &#8211; we have 10 fingers and 10 toes. For a humans it just makes sense.</p>
<p><span id="more-263"></span>When working with electronics however, 10 different states isn&#8217;t easy to represent, especially when working with low voltages. Noise is  huge problem, as is deciding which voltage represents each state. Try to implement a digital circuit which uses more than 2 states and you&#8217;ll get yourself into a whole lot of bother. Binary is perfect for digital circuits, very low voltages correspond to 0 and high voltages 1.</p>
<p><a href="http://jamesmcminn.com/wp-content/uploads/2010/01/binary11.png"><img class="aligncenter size-full wp-image-325" title="binary1" src="http://jamesmcminn.com/wp-content/uploads/2010/01/binary11.png" alt="" width="585" height="333" /></a>In this graph, anything above the top dotted line represents a 1 and everything below the bottom line represents a 0.</p>
<p>The use of binary makes circuits and hardware much more stable and less prone to noise. It makes them simpler too, without the use of binary, digital circuits would be nowhere near as advanced as they are today.</p>
<p>We can use very simple building blocks with simple rules to take billions of  transistors and create insanely powerful hardware that&#8217;s fast, stable and cheap.</p>
<h2>How do I convert from Decimal to Binary?</h2>
<p>So lets recap. Binary is a base-2 number system, that means it only has 2 symbols, 1 and 0, to represent every number. This sounds insane but its actually very simple and doing maths with it is similar and can be done in the same way you were first taught how to add and subtract decimal numbers when you were young.</p>
<p>Let&#8217;s start of by converting from a decimal number, such as 14, to binary. It&#8217;s actually fairly simple. Binary is read from right to left and every digit represent a power of 2. You&#8217;re probably familiar with the powers of 2, they&#8217;re very recognisable and crop up all the time in computers, the first few are 1, 2, 4, 8, 16, 32, 64 and 128.</p>
<p>There are a number of ways to convert from decimal to binary, some are faster than others but they all work. We&#8217;ll start with the simplest one first. It involves dividing the number by 2 and taking the remainder as the binary digit. Every time we divide by 2 we are increasing the power of 2. This method is best given as an example:</p>
<pre class="brush: plain;">
MATHS                           BINARY NUMBER SO FAR
14 ÷ 2 = 7 remainder 0          0
 7 ÷ 2 = 3 remainder 1          10
 3 ÷ 2 = 1 remainder 1          110
 1 ÷ 2 = 0 remainder 1          1110
</pre>
<p>So starting with 14 and dividing by 2 we get 7 with 0 remaining. It the remainder that is important here as it&#8217;s our binary digit. Note it down and repeat the steps again with 7. This time we have a remainder of 1. This is our second binary digit and it goes to the left of the 0 giving us 10. Remember, when using binary we read and write from right to left. Doing the same again with 3 gives us another 1 (110) and finally repeating the process again gives us 1 which means that 14 in binary is 1110.</p>
<h2>Converting from Binary to Decimal</h2>
<p>Converting from binary to decimal is very simple. My favorite method for doing so is to write out the binary number and above each digit write the powers of 2. I then sum all of the powers which have a 1 below them to get the decimal value.</p>
<pre class="brush: plain;">
Powers of 2:   16   8   4   2   1
Binary:         1   1   0   0   1
Decimal:       16 + 8 + 0 + 0 + 1 = 25
</pre>
<p>We can use this to check that the binary value we got for 14 earlier is correct.</p>
<pre class="brush: plain;">
Powers of 2:   8   4   2   1
Binary:        1   1   1   0
Decimal:       8 + 4 + 2 + 0 = 14
</pre>
<p>I&#8217;ve included a table with the decimal numbers from 0 to 31 and their binary counterparts below.</p>
<pre class="brush: plain;">
Binary    Decimal   |   Binary    Decimal
000000       0      |   010000      16
000001       1      |   010001      17
000010       2      |   010010      18
000011       3      |   010011      19
000100       4      |   010100      20
000101       5      |   010101      21
000110       6      |   010110      22
000111       7      |   010111      23
001000       8      |   011000      24
001001       9      |   011001      25
001010      10      |   011010      26
001011      11      |   011011      27
001100      12      |   011100      28
001101      13      |   011101      29
001110      14      |   011110      30
001111      15      |   011111      31
</pre>
]]></content:encoded>
			<wfw:commentRss>http://jamesmcminn.com/2010/01/25/an-introduction-to-binary/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Euler</title>
		<link>http://jamesmcminn.com/2009/08/19/project-euler/</link>
		<comments>http://jamesmcminn.com/2009/08/19/project-euler/#comments</comments>
		<pubDate>Wed, 19 Aug 2009 11:45:21 +0000</pubDate>
		<dc:creator>James McMinn</dc:creator>
				<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[Bruteforce]]></category>
		<category><![CDATA[Maths]]></category>

		<guid isPermaLink="false">http://james.ohthebanter.com/?p=57</guid>
		<description><![CDATA[I discovered Project Euler a few weeks ago while searching for something to keep my mind (and programming skills) busy during the holidays. Its proven itself to be exactly what I was looking for — if a bit more challenging than I was expecting.
Project Euler isn&#8217;t for everyone. If you don&#8217;t have a fairly decent [...]]]></description>
			<content:encoded><![CDATA[<p>I discovered <a title="Project Euler" href="http://projecteuler.net" target="_blank">Project Euler</a> a few weeks ago while searching for something to keep my mind (and programming skills) busy during the holidays. Its proven itself to be exactly what I was looking for — if a bit more challenging than I was expecting.</p>
<p>Project Euler isn&#8217;t for everyone. If you don&#8217;t have a fairly decent grasp of maths and the ability to do a bit of coding then you will struggle with the vast majority of the 250+ problems. <strong>These are not easy.</strong> Of course if you&#8217;re willing to learn and have enough determination then the first few should be well within your reach. </p>
<p><span id="more-57"></span>One of the fantastic things about Project Euler is that many of the concepts you learn can be used later on as part of a solution to a much more difficult problem. This is a great learning experience and something well worth putting some time into if you&#8217;re interested in this sort of thing.</p>
<h2>Problem 1</h2>
<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.</p>
<p>Find the sum of all the multiples of 3 or 5 below 1000.
</p></blockquote>
<p>A very simple first problem to get us started.</p>
<p>Arguably the most obvious solution is a bruteforce algorithm which looks at the numbers 1-999, checks whether they meet the desired criteria (divides evenly by 3 or 5) and returns the sum of the valid results.</p>
<p>Below is my implementation of the algorithm in Python:</p>
<pre class="brush: python;">
sum = 0
for i in xrange(1000):
    if ((i % 3) == 0) or ((i % 5) == 0):
        sum += i
print sum
</pre>
<p>Or in much less (readable) code:</p>
<pre class="brush: python;">print sum(x for x in xrange(1000) if (x % 3) == 0 or (x % 5) == 0)</pre>
<p>Even using bruteforce it computes the answer comfortably inside Project Euler&#8217;s &#8220;one-minute rule&#8221;, it takes only 0.035s to run only my 2.2Ghz laptop.</p>
<p>Of course there&#8217;s more than one way to skin a cat. Another solution is to sum all the multiples of 3 and 5 between 1 and 999 and subtract the sum of the duplicates, in this case all multiples of 15. This method is an example of the <a href="http://en.wikipedia.org/wiki/Inclusion-exclusion_principle">Inclusion-exclusion principle</a>. </p>
<p>Python makes this very easy to do thanks to the range() function (or xrange if your working with larger numbers and have the memory to spare):</p>
<pre class="brush: python;">
print sum(xrange(0,1000,3)) + sum(xrange(0,1000,5)) - sum(xrange(0,1000,15))
</pre>
<p>This runs in approximately the same time as my bruteforce algorithm, there are much more efficient ways of calculating the sum of a series of numbers but I&#8217;ll leave you to discover them for yourself.</p>
]]></content:encoded>
			<wfw:commentRss>http://jamesmcminn.com/2009/08/19/project-euler/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Welcome to my new Blog!</title>
		<link>http://jamesmcminn.com/2009/08/07/welcome-to-my-new-blog/</link>
		<comments>http://jamesmcminn.com/2009/08/07/welcome-to-my-new-blog/#comments</comments>
		<pubDate>Fri, 07 Aug 2009 12:31:33 +0000</pubDate>
		<dc:creator>James McMinn</dc:creator>
				<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Personal]]></category>
		<category><![CDATA[University]]></category>

		<guid isPermaLink="false">http://james.ohthebanter.com/?p=3</guid>
		<description><![CDATA[I&#8217;ve been mulling over the idea of a blog covering Computer Science, the University of Glasgow and anything else I happen to find interesting for a while now.
As you can see, I decided to go ahead with it.
I&#8217;ll be posting about Project Euler when I find time and hope to write about my first year [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been mulling over the idea of a blog covering Computer Science, the University of Glasgow and anything else I happen to find interesting for a while now.</p>
<p>As you can see, I decided to go ahead with it.</p>
<p>I&#8217;ll be posting about <a title="Project Euler" href="http://projecteuler.net" target="_blank">Project Euler</a> when I find time and hope to write about my first year at <a href="http://gla.ac.uk" target="_blank">the University of Glasgow</a> (hint: I loved it) sometime soon.</p>
<p>I hope to be able to showcase some of my web design work here too. I&#8217;m always looking to earn a bit of extra money doing web design or programming work so if you think you have something to offer then please get in touch.</p>
]]></content:encoded>
			<wfw:commentRss>http://jamesmcminn.com/2009/08/07/welcome-to-my-new-blog/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
