<?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>Server Side Guy &#187; Tim Barsness</title>
	<atom:link href="http://serversideguy.com/author/tim-barsness/feed/" rel="self" type="application/rss+xml" />
	<link>http://serversideguy.com</link>
	<description>Just another Barsness Solutions weblog</description>
	<lastBuildDate>Wed, 04 Apr 2012 21:37:41 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>How do I use PHP preg_replace_callback?</title>
		<link>http://serversideguy.com/2012/04/04/how-do-i-use-php-preg_replace_callback/</link>
		<comments>http://serversideguy.com/2012/04/04/how-do-i-use-php-preg_replace_callback/#comments</comments>
		<pubDate>Wed, 04 Apr 2012 21:37:41 +0000</pubDate>
		<dc:creator>Tim Barsness</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://serversideguy.com.mu.barsnesssolutions.com/?p=718</guid>
		<description><![CDATA[I&#8217;m working on a project that sends transactional emails from templates using variables in the %%varname%% format, but defines variables in another table as a lookup value, or query. So, I needed to match strings of type %%varname%% which screams regular expressions, but also needed varname to look up what to replace it with. So, [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m working on a project that sends transactional emails from templates using variables in the %%varname%% format, but defines variables in another table as a lookup value, or query.  So, I needed to match strings of type %%varname%% which screams regular expressions, but also needed varname to look up what to replace it with.  So, instead of using multiple functions, I was able to do this with a callback and preg_replace_callback.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$string</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;A complicated gentleman allow me to present,
Of all <span style="color: #009933; font-weight: bold;">%%</span>asdf<span style="color: #009933; font-weight: bold;">%%</span> the arts and faculties the terse embodiment,
He&amp;#039;s a great arithmetician who can demonstrate with ease <span style="color: #009933; font-weight: bold;">%%</span>name<span style="color: #009933; font-weight: bold;">%%</span>
That two and two are three, or five, or anything you please;
An eminent Logician who can make it clear to you
That black is white – when looked at from the proper point of view;
A marvelous Philologist who&amp;#039;ll undertake to show <span style="color: #009933; font-weight: bold;">%%</span>asdf<span style="color: #009933; font-weight: bold;">%%</span>
That &amp;#039;yes&amp;#039; is but another and a neater form of &amp;#039;no&amp;#039;.&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">preg_replace_callback</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span><span style="color: #666666; font-style: italic;">#039;(%%.*%%)&amp;#039;, 
</span>	<span style="color: #990000;">create_function</span><span style="color: #009900;">&#40;</span>
	    <span style="color: #339933;">&amp;</span><span style="color: #666666; font-style: italic;">#039;$matches&amp;#039;,
</span>	    <span style="color: #339933;">&amp;</span><span style="color: #666666; font-style: italic;">#039;return getValue($matches)&amp;#039;
</span>	<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	<span style="color: #000088;">$string</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> getValue<span style="color: #009900;">&#40;</span><span style="color: #000088;">$var</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
	<span style="color: #b1b100;">return</span> toupper<span style="color: #009900;">&#40;</span><span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span><span style="color: #666666; font-style: italic;">#039;%%&amp;#039;, &amp;#039;&amp;#039;, $var));
</span><span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/* Outputs:
A complicated gentleman allow me to present,
Of all ASDF the arts and faculties the terse embodiment,
He&amp;#039;s a great arithmetician who can demonstrate with ease NAME
That two and two are three, or five, or anything you please;
An eminent Logician who can make it clear to you
That black is white – when looked at from the proper point of view;
A marvelous Philologist who&amp;#039;ll undertake to show ASDF
That &amp;#039;yes&amp;#039; is but another and a neater form of &amp;#039;no&amp;#039;.
*/</span></pre></div></div>

<p>I use the function getValue to query the database and get the value of the variable.  This function is pretty slick; now, PHP just needs to support callbacks that aren&#8217;t created as strings and we&#8217;ll be all set.</p>
]]></content:encoded>
			<wfw:commentRss>http://serversideguy.com/2012/04/04/how-do-i-use-php-preg_replace_callback/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How do I grant a user execute permissions to stored procedures in SQL Server?</title>
		<link>http://serversideguy.com/2012/03/02/how-do-i-grant-a-user-execute-permissions-to-stored-procedures-in-sql-server/</link>
		<comments>http://serversideguy.com/2012/03/02/how-do-i-grant-a-user-execute-permissions-to-stored-procedures-in-sql-server/#comments</comments>
		<pubDate>Fri, 02 Mar 2012 13:39:07 +0000</pubDate>
		<dc:creator>Tim Barsness</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://serversideguy.com.mu.barsnesssolutions.com/?p=697</guid>
		<description><![CDATA[One thing that comes up fairly frequently with SQL Server permissions is allowing execute permissions to all user created programmability on a database. This frequently needs to be done when migrating servers, allowing the user connecting to execute stored procedures. One way to do this is to set explicit permissions for all securables for each [...]]]></description>
			<content:encoded><![CDATA[<p>One thing that comes up fairly frequently with SQL Server permissions is allowing execute permissions to all user created programmability on a database.  This frequently needs to be done when migrating servers, allowing the user connecting to execute stored procedures.  One way to do this is to set explicit permissions for all securables for each user.  This is effective with a limited amount of stored procedures.</p>
<p>In doing so however, as the number of stored procedures increases, the feasibility of setting explicit permissions decreases.  The above also requires that permissions be explicitly granted for newly created stored procedures.  </p>
<p>Another way of doing so is to create a role (which we&#8217;ll call db_executor) and granting execute permissions to all programmability (or a well-defined subset if you prefer) to said role, then adding desired users to the role.  Doing so looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">ROLE</span> db_executor
<span style="color: #0000FF;">GRANT</span> <span style="color: #0000FF;">EXECUTE</span> <span style="color: #0000FF;">TO</span> db_executor
<span style="color: #0000FF;">EXEC</span> <span style="color: #AF0000;">SP_ADDROLEMEMBER</span> <span style="color: #FF0000;">'db_executor'</span>, <span style="color: #FF0000;">'username'</span></pre></div></div>

<p>The above:</p>
<ol>
<li>Creates the role db_executor</li>
<li>Grants execute permissions to the role</li>
<li>Adds the desired user to the role</li>
</ol>
<p>Because roles exist within a database, this should be done in the context of the database desiring the programmability required.  </p>
]]></content:encoded>
			<wfw:commentRss>http://serversideguy.com/2012/03/02/how-do-i-grant-a-user-execute-permissions-to-stored-procedures-in-sql-server/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How do I use traits in PHP 5.4</title>
		<link>http://serversideguy.com/2012/01/31/how-do-i-use-traits-in-php-5-4/</link>
		<comments>http://serversideguy.com/2012/01/31/how-do-i-use-traits-in-php-5-4/#comments</comments>
		<pubDate>Tue, 31 Jan 2012 14:12:37 +0000</pubDate>
		<dc:creator>Tim Barsness</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://serversideguy.com.mu.barsnesssolutions.com/?p=670</guid>
		<description><![CDATA[The biggest change in PHP 5.4 is the addition of traits to the object-oriented programmability. Traits prevent code duplication by allowing multiple classes to include a collection of methods implemented as a trait. This means multiple classes can include the same method implementations without duplicating code. Traits improve on interfaces because they include not only [...]]]></description>
			<content:encoded><![CDATA[<p>The biggest change in PHP 5.4 is the addition of traits to the object-oriented programmability.  <a href="http://en.wikipedia.org/wiki/Trait_(computer_programming)" target="_blank">Traits</a> prevent code duplication by allowing multiple classes to include a collection of methods implemented as a trait.  This means multiple classes can include the same method implementations without duplicating code.</p>
<p>Traits improve on interfaces because they include not only the function definition, but also the implementation.  Traits can also use other traits, meaning that one trait could be a collection of others.  So, if you had traits Roll and Bounce, you could have trait Movements that used both Roll and Bounce.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">trait HelloWorld <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> sayHello<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Hello '</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> sayWorld<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'World!'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> MyClass <span style="color: #009900;">&#123;</span>
    use HelloWorld<span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> sayHelloWorld<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">sayhello</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">sayWorld</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$h</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyClass<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$h</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">sayHelloWorld</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>      <span style="color: #666666; font-style: italic;">// outputs &quot;Hello World!&quot;</span></pre></div></div>

<p>In implementing traits, the keywords trait and insteadof were added to PHP.  Usage of trait is outlined above.  The PHP keyword insteadof is used for conflict resolution.  Here&#8217;s an example of how to use insteadof:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">trait A <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> smallTalk<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'a'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> bigTalk<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'A'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
trait B <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> smallTalk<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'b'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> bigTalk<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'B'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Talker <span style="color: #009900;">&#123;</span>
    use A<span style="color: #339933;">,</span> B <span style="color: #009900;">&#123;</span>
        B<span style="color: #339933;">::</span><span style="color: #004000;">smallTalk</span> insteadof A<span style="color: #339933;">;</span>
        A<span style="color: #339933;">::</span><span style="color: #004000;">bigTalk</span> insteadof B<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Aliased_Talker <span style="color: #009900;">&#123;</span>
    use A<span style="color: #339933;">,</span> B <span style="color: #009900;">&#123;</span>
        B<span style="color: #339933;">::</span><span style="color: #004000;">smallTalk</span> insteadof A<span style="color: #339933;">;</span>
        A<span style="color: #339933;">::</span><span style="color: #004000;">bigTalk</span> insteadof B<span style="color: #339933;">;</span>
        B<span style="color: #339933;">::</span><span style="color: #004000;">bigTalk</span> <span style="color: #b1b100;">as</span> talk<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://serversideguy.com/2012/01/31/how-do-i-use-traits-in-php-5-4/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Project Euler Problem 17</title>
		<link>http://serversideguy.com/2011/12/27/project-euler-problem-17/</link>
		<comments>http://serversideguy.com/2011/12/27/project-euler-problem-17/#comments</comments>
		<pubDate>Wed, 28 Dec 2011 04:17:40 +0000</pubDate>
		<dc:creator>Tim Barsness</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://serversideguy.com/?p=639</guid>
		<description><![CDATA[I&#8217;ve been having some fun doing the first few problems of Project Euler and figured I&#8217;d share my solution to problem 17 here. The Problem If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + 5 + 4 + 4 = 19 [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been having some fun doing the first few problems of <a href="http://projecteuler.net/">Project Euler</a> and figured I&#8217;d share my solution to <a href="http://projecteuler.net/problem=17">problem 17</a> here.  </p>
<p><b>The Problem</b><br />
If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total.</p>
<p>If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters would be used?</p>
<p>NOTE: Do not count spaces or hyphens. For example, 342 (three hundred and forty-two) contains 23 letters and 115 (one hundred and fifteen) contains 20 letters. The use of &#8220;and&#8221; when writing out numbers is in compliance with British usage.</p>
<p><b>The Solution</b><br />
The reason I love this solution is I found a PHP library that writes out these numbers for you.  It comes from PECL 1.0 and is part of PHP 5.3 via that <a href="http://php.net/manual/en/class.numberformatter.php" target="_blank">NumberFormatter</a> class.  This class did most of the work for me.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?</span>
<span style="color: #000088;">$res</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$f</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> NumberFormatter<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;en&quot;</span><span style="color: #339933;">,</span> NumberFormatter<span style="color: #339933;">::</span><span style="color: #004000;">SPELLOUT</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span> format<span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">100</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">%</span> <span style="color: #cc66cc;">100</span> <span style="color: #339933;">!=</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$res</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'and'</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$res</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span></pre></div></div>

<p>My first submission was incorrect and I quickly figured out it was because the NumberFormatter class was missing the &#8220;and&#8221; after the hundreds.  I simply added that at the end since we&#8217;re only looking for a count and came up with the correct result.</p>
<p>Installing the NumberFormatter class in Debian squeeze was pretty easy.  I simply had to install the <a href="http://www.php.net/manual/en/intl.setup.php" target="_blank">PHP5 international compatibility</a> package:  sudo apt-get instal php5-intl</p>
]]></content:encoded>
			<wfw:commentRss>http://serversideguy.com/2011/12/27/project-euler-problem-17/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How do I perform integer division in PHP</title>
		<link>http://serversideguy.com/2011/12/27/how-do-i-perform-integer-division-in-php/</link>
		<comments>http://serversideguy.com/2011/12/27/how-do-i-perform-integer-division-in-php/#comments</comments>
		<pubDate>Wed, 28 Dec 2011 02:34:48 +0000</pubDate>
		<dc:creator>Tim Barsness</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://serversideguy.com.mu.barsnesssolutions.com/?p=633</guid>
		<description><![CDATA[PHP&#8217;s division operator returns a floating point result from the division operator unless both operands are integers and they are evenly divisible (the result is an integer). I was recently working on Project Euler Problem 13 and needed to do integer division, something I haven&#8217;t come across in PHP before. Integer division in PHP is [...]]]></description>
			<content:encoded><![CDATA[<p>PHP&#8217;s division operator returns a floating point result from the division operator unless both operands are integers and they are evenly divisible (the result is an integer).  I was recently working on Project Euler Problem 13 and needed to do integer division, something I haven&#8217;t come across in PHP before.  </p>
<p>Integer division in PHP is pretty straight forward.  Casting the result as an integer will yield an integer.  That is:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">var_dump</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">25</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">7</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>         <span style="color: #666666; font-style: italic;">// float(3.5714285714286) </span>
<span style="color: #990000;">var_dump</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>int<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">25</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">7</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// int(3)</span>
<span style="color: #990000;">var_dump</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">round</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">25</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">7</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// float(4)</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://serversideguy.com/2011/12/27/how-do-i-perform-integer-division-in-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Euler Problem 10</title>
		<link>http://serversideguy.com/2011/12/23/project-euler-problem-10/</link>
		<comments>http://serversideguy.com/2011/12/23/project-euler-problem-10/#comments</comments>
		<pubDate>Fri, 23 Dec 2011 18:12:42 +0000</pubDate>
		<dc:creator>Tim Barsness</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://serversideguy.com.mu.barsnesssolutions.com/?p=628</guid>
		<description><![CDATA[I&#8217;ve been having some fun doing the first few problems of Project Euler and figured I&#8217;d share my solution to problem 10 here. The Problem The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. Find the sum of all the primes below two million. The Solution My [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been having some fun doing the first few problems of <a href="http://projecteuler.net/">Project Euler</a> and figured I&#8217;d share my solution to <a href="http://projecteuler.net/problem=10">problem 10</a> here.  </p>
<p><b>The Problem</b><br />
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.<br />
Find the sum of all the primes below two million.</p>
<p><b>The Solution</b><br />
My solution is fairly simple, testing primality for all the numbers below 2,000,000.  If prime, adding them to a running total.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #ff0000; font-style: italic;">/**
 * Calculate the sum of all the primes below two million. 
 **/</span>
<span style="color: #339900;">#include </span>
<span style="color: #339900;">#include </span>
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">bool</span> isPrime<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">long</span> num<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>num<span style="color: #000080;">&lt;=</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span>
		<span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">long</span> i<span style="color: #000080;">=</span><span style="color: #0000dd;">2</span><span style="color: #008080;">;</span> i<span style="color: #000080;">&lt;=</span><span style="color: #0000dd;">sqrt</span><span style="color: #008000;">&#40;</span>num<span style="color: #000040;">*</span><span style="color:#800080;">1.0</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		<span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>num<span style="color: #000040;">%</span>i<span style="color: #000080;">==</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span>
			<span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
	<span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">int</span> main <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">long</span> upperlimit <span style="color: #000080;">=</span> <span style="color: #0000dd;">2000000</span>, sum <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">long</span> i <span style="color: #000080;">=</span> <span style="color: #0000dd;">2</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;</span> upperlimit<span style="color: #008080;">;</span> i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
		<span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>isPrime<span style="color: #008000;">&#40;</span>i<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
			sum <span style="color: #000040;">+</span><span style="color: #000080;">=</span> i<span style="color: #008080;">;</span>
			<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Prime found: &quot;</span> <span style="color: #000080;">&lt;&lt;</span> i <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
		<span style="color: #008000;">&#125;</span>
	<span style="color: #008000;">&#125;</span>
&nbsp;
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;The sum of primes below &quot;</span> <span style="color: #000080;">&lt;&lt;</span> upperlimit <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; is &quot;</span> <span style="color: #000080;">&lt;&lt;</span> sum <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000ff;">return</span> <span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>The interesting part of this problem for me was I updated my primality test to include the fact that if a number n has a factor greater than sqrt(n) it must also have one less than sqrt(n), so we only have to test up to sqrt(n) in our primality test:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">bool</span> isPrime<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">long</span> num<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>num<span style="color: #000080;">&lt;=</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span>
		<span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">long</span> i<span style="color: #000080;">=</span><span style="color: #0000dd;">2</span><span style="color: #008080;">;</span> i<span style="color: #000080;">&lt;=</span><span style="color: #0000dd;">sqrt</span><span style="color: #008000;">&#40;</span>num<span style="color: #000040;">*</span><span style="color:#800080;">1.0</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		<span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>num<span style="color: #000040;">%</span>i<span style="color: #000080;">==</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span>
			<span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
	<span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://serversideguy.com/2011/12/23/project-euler-problem-10/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Euler Problem 9</title>
		<link>http://serversideguy.com/2011/12/22/project-euler-problem-9/</link>
		<comments>http://serversideguy.com/2011/12/22/project-euler-problem-9/#comments</comments>
		<pubDate>Thu, 22 Dec 2011 21:37:04 +0000</pubDate>
		<dc:creator>Tim Barsness</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://serversideguy.com.mu.barsnesssolutions.com/?p=616</guid>
		<description><![CDATA[I&#8217;ve been having some fun doing the first few problems of Project Euler and figured I&#8217;d share my solution to problem 9 here. The Problem A Pythagorean triplet is a set of three natural numbers, a b c, for which,a^2 + b^2 = c^2 There exists exactly one Pythagorean triplet for which a + b [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been having some fun doing the first few problems of <a href="http://projecteuler.net/">Project Euler</a> and figured I&#8217;d share my solution to <a href="http://projecteuler.net/problem=9">problem 9</a> here.  </p>
<p><b>The Problem</b><br />
A Pythagorean triplet is a set of three natural numbers, a  b  c, for which,a^2 + b^2 = c^2<br />
There exists exactly one Pythagorean triplet for which a + b + c = 1000.  Find the product abc.</p>
<p><b>The Solution</b><br />
This is pretty basic and inefficient, but I decided to loop through a and b, calculating c from the difference.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include </span>
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">int</span> main <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
&nbsp;
        <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> a <span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span> a <span style="color: #000080;">&lt;</span> <span style="color: #0000dd;">1000</span><span style="color: #008080;">;</span> a<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
                <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> b <span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span> b <span style="color: #000080;">&lt;</span> <span style="color: #0000dd;">1000</span><span style="color: #008080;">;</span> b<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
                        <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;a: &quot;</span> <span style="color: #000080;">&lt;&lt;</span> a <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; b: &quot;</span> <span style="color: #000080;">&lt;&lt;</span> b <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; c: &quot;</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #008000;">&#40;</span><span style="color: #0000dd;">1000</span> <span style="color: #000040;">-</span> a <span style="color: #000040;">-</span> b<span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; a^2 + b^2: &quot;</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #008000;">&#40;</span>a <span style="color: #000040;">*</span> a <span style="color: #000040;">+</span> b <span style="color: #000040;">*</span> b<span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; c^2: &quot;</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #008000;">&#40;</span><span style="color: #0000dd;">1000</span> <span style="color: #000040;">-</span> a <span style="color: #000040;">-</span> b<span style="color: #008000;">&#41;</span> <span style="color: #000040;">*</span> <span style="color: #008000;">&#40;</span><span style="color: #0000dd;">1000</span> <span style="color: #000040;">-</span> a <span style="color: #000040;">-</span> b<span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
                        <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>a <span style="color: #000040;">*</span> a <span style="color: #000040;">+</span> b <span style="color: #000040;">*</span> b <span style="color: #000080;">==</span> <span style="color: #008000;">&#40;</span><span style="color: #0000dd;">1000</span> <span style="color: #000040;">-</span> a <span style="color: #000040;">-</span> b<span style="color: #008000;">&#41;</span> <span style="color: #000040;">*</span> <span style="color: #008000;">&#40;</span><span style="color: #0000dd;">1000</span> <span style="color: #000040;">-</span> a <span style="color: #000040;">-</span> b<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
                                <span style="color: #0000ff;">return</span> <span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
                        <span style="color: #008000;">&#125;</span>
                <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span>
        <span style="color: #0000ff;">return</span> <span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>There you have it, the program stops at a^2 + b^2 = c^2 where c = (1000 &#8211; a &#8211; b).  I then took a*b*c to get the answer.</p>
]]></content:encoded>
			<wfw:commentRss>http://serversideguy.com/2011/12/22/project-euler-problem-9/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Euler Problem 7</title>
		<link>http://serversideguy.com/2011/12/15/project-euler-problem-7/</link>
		<comments>http://serversideguy.com/2011/12/15/project-euler-problem-7/#comments</comments>
		<pubDate>Thu, 15 Dec 2011 18:32:25 +0000</pubDate>
		<dc:creator>Tim Barsness</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://serversideguy.com/?p=607</guid>
		<description><![CDATA[I mentioned yesterday, I’ve been doing the first few problems of Project Euler and figured I’d share my solution to problem 7 here. The Problem By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13. What is the 10,001st prime number? The [...]]]></description>
			<content:encoded><![CDATA[<p>I mentioned yesterday, I’ve been doing the first few problems of <a href="http://projecteuler.net">Project Euler</a> and figured I’d share my solution to <a href="http://projecteuler.net/problem=7">problem 7</a> here.</p>
<p><strong>The Problem</strong><br />
By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.<br />
What is the 10,001st prime number?</p>
<p><strong>The Solution</strong><br />
Let&#8217;s define a function isPrime(num) (granted, could be more efficient) and loop through the natural numbers, counting those that are prime.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #ff0000; font-style: italic;">/**
 * Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.
 **/</span>
<span style="color: #339900;">#include</span>
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">bool</span> isPrime <span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">long</span> num <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
        <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i <span style="color: #000080;">=</span> num <span style="color: #000040;">-</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&gt;</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span> i<span style="color: #000040;">--</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
                <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>num <span style="color: #000040;">%</span> i <span style="color: #000080;">==</span> <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
                        <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #008080;">;</span>
                <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span>
        <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">int</span> main <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
        <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">long</span> num <span style="color: #000080;">=</span> <span style="color: #0000dd;">2</span>, primeCount <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
&nbsp;
        <span style="color: #0000ff;">while</span> <span style="color: #008000;">&#40;</span> primeCount <span style="color: #000080;">&lt;</span> <span style="color: #0000dd;">10001</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
                <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span> isPrime <span style="color: #008000;">&#40;</span>num<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
                        primeCount<span style="color: #000040;">++</span><span style="color: #008080;">;</span>
                        <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Prime &quot;</span> <span style="color: #000080;">&lt;&lt;</span> primeCount <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; &quot;</span> <span style="color: #000080;">&lt;&lt;</span> num <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; is prime&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
                <span style="color: #008000;">&#125;</span>
                num<span style="color: #000040;">++</span><span style="color: #008080;">;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #0000ff;">return</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://serversideguy.com/2011/12/15/project-euler-problem-7/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Euler Problem 6</title>
		<link>http://serversideguy.com/2011/12/14/project-euler-problem-6/</link>
		<comments>http://serversideguy.com/2011/12/14/project-euler-problem-6/#comments</comments>
		<pubDate>Thu, 15 Dec 2011 02:26:46 +0000</pubDate>
		<dc:creator>Tim Barsness</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://serversideguy.com.mu.barsnesssolutions.com/?p=603</guid>
		<description><![CDATA[I mentioned yesterday, I&#8217;ve been doing the first few problems of Project Euler and figured I’d share my solution to problem 6 here. The Problem Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum. The Solution This one is pretty straight forward. [...]]]></description>
			<content:encoded><![CDATA[<p>I mentioned yesterday, I&#8217;ve been doing the first few problems of <a href="http://projecteuler.net/">Project Euler</a> and figured I’d share my solution to <a href="http://projecteuler.net/problem=6">problem 6</a> here.</p>
<p><b>The Problem</b><br />
Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.</p>
<p><b>The Solution</b><br />
This one is pretty straight forward.  Find a data structure capable of holding the square of the sum of numbers 1 to 100 and subtract the squares of the first 100 natural numbers.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include </span>
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">int</span> main <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
        <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">long</span> sumsquares <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span>, sums <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
        <span style="color: #0000ff;">int</span> n<span style="color: #008080;">;</span>
&nbsp;
        <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&gt;</span> n<span style="color: #008080;">;</span>
&nbsp;
        <span style="color: #0000ff;">while</span> <span style="color: #008000;">&#40;</span>n <span style="color: #000080;">&lt;</span> <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
                <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&gt;</span> n<span style="color: #008080;">;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i <span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;=</span> n<span style="color: #008080;">;</span> i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
                <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;i: &quot;</span> <span style="color: #000080;">&lt;&lt;</span> i <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
                sumsquares <span style="color: #000040;">+</span><span style="color: #000080;">=</span> i <span style="color: #000040;">*</span> i<span style="color: #008080;">;</span>
                <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;sumsquares: &quot;</span> <span style="color: #000080;">&lt;&lt;</span> sumsquares <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
                sums <span style="color: #000040;">+</span><span style="color: #000080;">=</span> i<span style="color: #008080;">;</span>
                <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;sums: &quot;</span> <span style="color: #000080;">&lt;&lt;</span> sums <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;The sum of squares from 1 to &quot;</span> <span style="color: #000080;">&lt;&lt;</span> n <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;: &quot;</span> <span style="color: #000080;">&lt;&lt;</span> sumsquares <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
        <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;The square of sums from 1 to &quot;</span> <span style="color: #000080;">&lt;&lt;</span> n <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;: &quot;</span> <span style="color: #000080;">&lt;&lt;</span> sums <span style="color: #000040;">*</span> sums <span style="color: #000080;">&lt;&lt;</span> endl <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
        <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;The difference: &quot;</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #008000;">&#40;</span>sums <span style="color: #000040;">*</span> sums <span style="color: #000040;">-</span> sumsquares<span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
&nbsp;
        <span style="color: #0000ff;">return</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>As you can see, there is a loop creating the sum and the sum of squares.  From there, it subtracts the square of the sum and the sum of squares to get the answer.  </p>
]]></content:encoded>
			<wfw:commentRss>http://serversideguy.com/2011/12/14/project-euler-problem-6/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Euler Problem 5</title>
		<link>http://serversideguy.com/2011/12/13/project-euler-problem-5/</link>
		<comments>http://serversideguy.com/2011/12/13/project-euler-problem-5/#comments</comments>
		<pubDate>Wed, 14 Dec 2011 00:50:07 +0000</pubDate>
		<dc:creator>Tim Barsness</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://serversideguy.com.mu.barsnesssolutions.com/?p=601</guid>
		<description><![CDATA[I&#8217;ve been having some fun doing the first few problems of Project Euler and figured I&#8217;d share my solution to problem 5 here. The Problem 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 positive number that is evenly [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been having some fun doing the first few problems of <a href="http://projecteuler.net/">Project Euler</a> and figured I&#8217;d share my solution to <a href="http://projecteuler.net/problem=5">problem 5</a> here.  </p>
<p><b>The Problem</b><br />
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 positive number that is evenly divisible by all of the numbers from 1 to 20?</p>
<p><b>The Solution</b><br />
This problem, worded another way, asks what the least common multiple of all the integers between 1 and 20 is.  Since multiplication is commutative, the least common multiple of a series is the same as computing the least common multiple of all the numbers before it and the current.  So, LCM(1&#8230;6) is the same as LCM(6, LCM(5, LCM(4, LCM(3, LCM(2, 1))))).</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include </span>
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">long</span> gcd<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">long</span> a, <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">long</span> b<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>b <span style="color: #000080;">==</span> <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span>
		<span style="color: #0000ff;">return</span> a<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">return</span> gcd<span style="color: #008000;">&#40;</span>b, a<span style="color: #000040;">%</span>b<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">long</span> lcm<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">long</span> a, <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">long</span> b<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">return</span> a <span style="color: #000040;">*</span> b <span style="color: #000040;">/</span> gcd<span style="color: #008000;">&#40;</span>a,b<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">int</span> main <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">long</span> answer <span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">long</span> i <span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;=</span> <span style="color: #0000dd;">20</span><span style="color: #008080;">;</span> i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
		<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;i: &quot;</span> <span style="color: #000080;">&lt;&lt;</span> i<span style="color: #008080;">;</span>
		answer <span style="color: #000080;">=</span> lcm<span style="color: #008000;">&#40;</span>answer, i<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; lcm: &quot;</span> <span style="color: #000080;">&lt;&lt;</span> answer <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
&nbsp;
	<span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>My solution uses an algorithm to compute the least common multiple that involves finding the greatest common divisor as follows:  lcm(a, b) = a*b / gcd(a, b).  I used the Euclid method of finding the greatest common divisor:  gcd(a,0) = a; gcd(a, b) = gcd(b, a mod b).</p>
]]></content:encoded>
			<wfw:commentRss>http://serversideguy.com/2011/12/13/project-euler-problem-5/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

