<?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>No Permission &#187; programming</title>
	<atom:link href="http://www.nopermission.com/category/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.nopermission.com</link>
	<description>The best way to predict the future is to invent it - Alan Kay</description>
	<lastBuildDate>Tue, 12 Apr 2011 06:17:22 +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>Project Euler, Problem 10, In Erlang  &#8211; Sieveless Edition</title>
		<link>http://www.nopermission.com/2009/09/project-euler-problem-10-in-erlang-sieveless-edition/</link>
		<comments>http://www.nopermission.com/2009/09/project-euler-problem-10-in-erlang-sieveless-edition/#comments</comments>
		<pubDate>Tue, 08 Sep 2009 20:16:26 +0000</pubDate>
		<dc:creator>Rod Ramsey</dc:creator>
				<category><![CDATA[erlang]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[project euler]]></category>

		<guid isPermaLink="false">http://www.nopermission.com/?p=151</guid>
		<description><![CDATA[Problem 10, more primes.  Sum the all primes under 2,000,000.
Probably meant to force you out of brute force trial division.  
Probably want you to use the Sieve Of Eratosthenes.  
And  I thought about it.  
Then tried trial division for fun.  
Exactly 1 minute run time.  
Done!
Heh.
Looking at future [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://projecteuler.net/index.php?section=problems&#038;id=10">Problem 10</a>, more primes.  Sum the all primes under 2,000,000.</p>
<p>Probably meant to force you out of brute force trial division.  </p>
<p>Probably want you to use the Sieve Of Eratosthenes.  </p>
<p>And  I thought about it.  </p>
<p>Then tried trial division for fun.  </p>
<p>Exactly 1 minute run time.  </p>
<p>Done!</p>
<p>Heh.</p>
<p>Looking at future problems, this is probably the last time I can take the easy way out.</p>
<p>Lazy code -</p>
<pre>
 -module(euler10).
 -import(math).
 -export([sumprimes/1]).

sumprimes(Max) ->
   Sum = sum(Max,2,0),
   Sum.

sum(Max,Start,Sum) ->
   if Start =< Max ->
    MaxFactor = math:sqrt(Start),
    Prime = isprime(Start,2,MaxFactor),
    if Prime == true ->
      Newsum = Sum + Start,
      sum(Max,Start+1,Newsum);
    true ->
      sum(Max,Start+1,Sum)
    end;
   true ->
    Sum
   end.

isprime(N,PFactor,Max)->
   if PFactor =< Max ->
          if  N rem PFactor == 0 ->
              false;
          true ->
              isprime(N,PFactor+1,Max)
          end;
    true ->
          true
    end.     
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.nopermission.com/2009/09/project-euler-problem-10-in-erlang-sieveless-edition/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Euler, Problem 9, in Erlang</title>
		<link>http://www.nopermission.com/2009/09/project-euler-problem-9-in-erlang/</link>
		<comments>http://www.nopermission.com/2009/09/project-euler-problem-9-in-erlang/#comments</comments>
		<pubDate>Tue, 08 Sep 2009 20:07:22 +0000</pubDate>
		<dc:creator>Rod Ramsey</dc:creator>
				<category><![CDATA[erlang]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[project euler]]></category>

		<guid isPermaLink="false">http://www.nopermission.com/?p=145</guid>
		<description><![CDATA[Problem 9, and more brute-forcey goodness for the solution.
&#8220;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 + c = 1000.
Find the product abc."
I'm not totally happy with this one.  I'm positive [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://projecteuler.net/index.php?section=problems&#038;id=9">Problem 9</a>, and more brute-forcey goodness for the solution.</p>
<p>&#8220;A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,<br />
a^(2) + b^(2) = c^(2)<br />
There exists exactly one Pythagorean triplet for which a + b + c = 1000.<br />
Find the product abc."</p>
<p>I'm not totally happy with this one.  I'm positive there is a better approach, and I'm particularly not happy with using a return value of "0" as a flag value.  Feels wrong. I'll probably revisit this one.</p>
<p>Brute force, only optimization is on boundaries of the brute force test.  Since A< B< C and A+B+C = 1000,  A has to be less than 334 and B has to be less than 500.  </p>
<p>Forceful code - </p>
<pre>
 -module(euler9).
 -export([triplet/0]).

triplet() ->
  Product = iteratea(333),
  Product.

iteratea(A)->
    if A >=1 ->
         Product = iterateb (A,499),
            if Product == 0 ->
               iteratea(A-1);
            true ->
               Product
            end;
    true ->
        0
    end.    

iterateb(A,B) ->
   if B > 1 ->
     C = 1000 &#8211; (A+B),
     if A*A + B*B == C*C ->
        io:format(&#8220;A ~w B ~w C ~w ~n&#8221;, [A,B,C]),
        A*B*C;
     true ->
        iterateb(A,B-1)
     end;
    true ->
       0
    end.
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.nopermission.com/2009/09/project-euler-problem-9-in-erlang/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Euler, Problem 8, in Erlang</title>
		<link>http://www.nopermission.com/2009/09/project-euler-problem-8-in-erlang/</link>
		<comments>http://www.nopermission.com/2009/09/project-euler-problem-8-in-erlang/#comments</comments>
		<pubDate>Tue, 08 Sep 2009 19:50:23 +0000</pubDate>
		<dc:creator>Rod Ramsey</dc:creator>
				<category><![CDATA[erlang]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[project euler]]></category>

		<guid isPermaLink="false">http://www.nopermission.com/?p=141</guid>
		<description><![CDATA[Problem 8 is another one of those problems that&#8217;s pretty simple to approach.  The only even minor twist is that it&#8217;s probably best to use a string in this one.  But easily enough done.
Some on the P.E. discussion boards even did this one with no programming, just a visual scan of the number.
&#8220;Find [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://projecteuler.net/index.php?section=problems&amp;id=8">Problem 8 </a>is another one of those problems that&#8217;s pretty simple to approach.  The only even minor twist is that it&#8217;s probably best to use a string in this one.  But easily enough done.</p>
<p>Some on the P.E. discussion boards even did this one with no programming, just a visual scan of the number.</p>
<p>&#8220;Find the greatest product of five consecutive digits in the 1000-digit number.&#8221;</p>
<p>Strung out code -</p>
<pre> -module(euler8).
 -export([greatestproduct/0]).

greatestproduct() -&gt;
   Source  = "731671765313306 [etc, really long number] 52963450",
   N = 1,
   InitialGreatest = 0,
   Greatest = teststring(Source,N, InitialGreatest),
   Greatest.

teststring(Source, N,Greatest) -&gt;
   if N &lt; (length(Source)-5) -&gt;
         Anum = list_to_integer(string:substr(Source,N,1)),
         Bnum = list_to_integer(string:substr(Source,N+1,1)),
         Cnum = list_to_integer(string:substr(Source,N+2,1)),
         Dnum = list_to_integer(string:substr(Source,N+3,1)),
         Enum = list_to_integer(string:substr(Source,N+4,1)),
         Product = Anum*Bnum*Cnum*Dnum*Enum,
         if Product &gt; Greatest -&gt;
            teststring(Source, N+1, Product);
         true -&gt;
            teststring(Source, N+1, Greatest)
         end;
   true -&gt;
         Greatest
   end.</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.nopermission.com/2009/09/project-euler-problem-8-in-erlang/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Euler, Problem 7, in Erlang</title>
		<link>http://www.nopermission.com/2009/09/project-euler-problem-7-in-erlang/</link>
		<comments>http://www.nopermission.com/2009/09/project-euler-problem-7-in-erlang/#comments</comments>
		<pubDate>Sat, 05 Sep 2009 19:53:24 +0000</pubDate>
		<dc:creator>Rod Ramsey</dc:creator>
				<category><![CDATA[erlang]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[project euler]]></category>

		<guid isPermaLink="false">http://www.nopermission.com/?p=119</guid>
		<description><![CDATA[Ah, Problem 7, the one we knew was coming.  
&#8220;What is the 10001st prime number?&#8221;
The basics are simple enough.  Brute force a primality test via trial division and use the test to find 10001 prime number.  So I did that, got the answer.  
But it took 2:24 on my netbook, and [...]]]></description>
			<content:encoded><![CDATA[<p>Ah, <a href="http://projecteuler.net/index.php?section=problems&#038;id=7">Problem 7</a>, the one we knew was coming.  </p>
<p>&#8220;What is the 10001st prime number?&#8221;</p>
<p>The basics are simple enough.  Brute force a primality test via trial division and use the test to find 10001 prime number.  So I did that, got the answer.  </p>
<p>But it took 2:24 on my netbook, and the rule of Project Euler is that it should take less than 1:00. </p>
<p>So it was time to optimize!  I started with the simplest optimization, limiting the upper bound of the trial division in the square root of the number being tested.  If you&#8217;ve made it that far without finding a factor, you&#8217;re not going to (other than the number itself, of course), so it&#8217;s prime.   </p>
<p>Once I added that, the time dropped to under 4 seconds, which surprised me.  I didn&#8217;t expect that dramatic a drop from such a basic optimization.  Lesson learned there.  Sometimes it&#8217;s worth just a little more effort to make things much, much faster.</p>
<p>It&#8217;s also worth noting, however, that there a number of other simple optimizations I&#8217;m not adding in since I&#8217;m already well under the goal. <img src='http://www.nopermission.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>Called by &#8220;euler7:nthprime(10001,2)&#8221;  I&#8217;m seeding it with the first prime there.  In most of these I could eliminate one of my seed parameters by having alternate versions of the functions for the base case, but I do it this way to simplify the code itself. </p>
<p>Prime Grade A Code-   </p>
<pre>
 -module(euler7).
 -import(math).
 -export([nthprime/2]).

nthprime(N,Prevprime) ->
   if N >= 2 ->
        NthPrime = findprime(Prevprime+1),
        nthprime(N-1,NthPrime);
   true ->
       Prevprime
   end.     

findprime(Start) ->
   Max = math:sqrt(Start),
   Prime = isprime(Start,2,Max),
   if Prime == true ->
      Start;
   true ->
          findprime(Start+1)
   end.

isprime(N,PFactor,Max)->
   if PFactor =< Max ->
          if  N rem PFactor == 0 ->
              false;
          true ->
              isprime(N,PFactor+1,Max)
          end;
    true ->
          true
    end.
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.nopermission.com/2009/09/project-euler-problem-7-in-erlang/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Euler, Problem 6, in Erlang</title>
		<link>http://www.nopermission.com/2009/09/project-euler-problem-6-in-erlang/</link>
		<comments>http://www.nopermission.com/2009/09/project-euler-problem-6-in-erlang/#comments</comments>
		<pubDate>Sat, 05 Sep 2009 19:40:55 +0000</pubDate>
		<dc:creator>Rod Ramsey</dc:creator>
				<category><![CDATA[erlang]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[project euler]]></category>

		<guid isPermaLink="false">http://www.nopermission.com/?p=116</guid>
		<description><![CDATA[Problem 6 was surprisingly straight-forward.  
&#8220;Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.&#8221;
Really, just a straight-forward calculation, easily implemented in Erlang recursive-speak.  No real comments on this one.

-module(euler6).
 -export([difference/1]).

 difference(N) ->
    SumSquare = sumofsquares(0,N),
   [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://projecteuler.net/index.php?section=problems&#038;id=6">Problem 6</a> was surprisingly straight-forward.  </p>
<p>&#8220;Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.&#8221;</p>
<p>Really, just a straight-forward calculation, easily implemented in Erlang recursive-speak.  No real comments on this one.</p>
<pre>
-module(euler6).
 -export([difference/1]).

 difference(N) ->
    SumSquare = sumofsquares(0,N),
    Sum = squareofsum(0,N),
    SquareSum = Sum * Sum,
    io:format("Square ~w Sum ~w ~n", [SquareSum, SumSquare]),
    Difference = SquareSum - SumSquare,
    Difference.  

sumofsquares(Sum,N) ->
    if N >= 1 ->
       NewSum = (N*N) + sumofsquares(Sum,N-1);
    true ->
       NewSum = Sum
    end,
    NewSum.

squareofsum(Sum, N) ->
     if N >= 1 ->
       NewSum =  N + squareofsum(Sum, N-1);
     true ->
       NewSum = 0
      end,
     NewSum.
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.nopermission.com/2009/09/project-euler-problem-6-in-erlang/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Euler, Problem 4, in Erlang</title>
		<link>http://www.nopermission.com/2009/09/project-euler-problem-4-in-erlang/</link>
		<comments>http://www.nopermission.com/2009/09/project-euler-problem-4-in-erlang/#comments</comments>
		<pubDate>Sat, 05 Sep 2009 19:34:57 +0000</pubDate>
		<dc:creator>Rod Ramsey</dc:creator>
				<category><![CDATA[erlang]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[project euler]]></category>

		<guid isPermaLink="false">http://www.nopermission.com/?p=112</guid>
		<description><![CDATA[Problem 4 is an interesting one.  &#8220;A palindromic number reads the same both ways. Find the largest palindrome made from the product of two 3-digit numbers.&#8221;
I thought about this for a few minutes and decided the best way to approach it would be to build candidate palindromic numbers and and then test them to [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://projecteuler.net/index.php?section=problems&#038;id=4">Problem 4</a> is an interesting one.  &#8220;A palindromic number reads the same both ways. Find the largest palindrome made from the product of two 3-digit numbers.&#8221;</p>
<p>I thought about this for a few minutes and decided the best way to approach it would be to build candidate palindromic numbers and and then test them to see if they were the product of two 3 digit numbers. </p>
<p>It&#8217;s easy to see that the product of two 3 digit numbers could at most be 6 digits, so I decided that I would start building my candidate palindromes by counting down from 999 and adding the mirror of the number as a suffix.  Example &#8211; take 998 and add 899 to the end for 998899.  Then I would test each candidate by dividing by all possible three digit numbers and seeing if I could find a three digit cofactor for any even divisions.  For example, if a candidate was evenly divisible by 123, was the cofactor also a three digit number?</p>
<p>So this problem was mostly centered in the approach.  Once I&#8217;d settled on that, the Erlang code came easily.   A little more work than I&#8217;d like building the palindromes due to single assignment, but nothing too bad.  I can see how single assignment will be a pain in some situations though.</p>
<p>The code &#8211; - edoc ehT</p>
<pre>
 -module(euler4).
 -import(lists).
 -export([greatestpalindrome/1]).

 greatestpalindrome(N) ->
     Greatest = palindrome(N),
     io:format("Greatest is ~w", [Greatest]).

 palindrome(N) ->
    if N >= 100 ->
      Candidate = buildpalindrome(N),
      Success = testpalindrome(Candidate,999),
      if Success == true ->
         Candidate;
      true ->
         palindrome(N-1)
      end;
    true ->
      io:format("no match found~n")
    end.  

 buildpalindrome(N) ->
    Nstring = integer_to_list(N),
    ReverseN = lists:reverse(Nstring),
    Palindrome = Nstring ++ ReverseN,
    Npalindrome = list_to_integer(Palindrome),
    Npalindrome.

testpalindrome(N,Testfactor) ->
    if Testfactor >= 100 ->
       if N rem Testfactor == 0 ->
          Cofactor = round(N / Testfactor),
            if Cofactor >= 100, Cofactor =< 999 ->
                true;
            true ->
                false
            end;
       true ->
            testpalindrome(N,Testfactor - 1)
       end;
     true ->
       false
    end.
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.nopermission.com/2009/09/project-euler-problem-4-in-erlang/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Project Euler, Problem 5, in dirty dirty Erlang</title>
		<link>http://www.nopermission.com/2009/09/project-euler-problem-5-in-dirty-dirty-erlang/</link>
		<comments>http://www.nopermission.com/2009/09/project-euler-problem-5-in-dirty-dirty-erlang/#comments</comments>
		<pubDate>Fri, 04 Sep 2009 22:02:17 +0000</pubDate>
		<dc:creator>Rod Ramsey</dc:creator>
				<category><![CDATA[erlang]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[project euler]]></category>

		<guid isPermaLink="false">http://www.nopermission.com/?p=108</guid>
		<description><![CDATA[I did complete Problem 4 already, but the write up will take a bit of time.  So I went ahead and did Problem 5, in a very quick and dirty way.  
Problem 5 &#8211; &#8220;What is the smallest number that is evenly divisible by all of the numbers from 1 to 20?&#8221;
The total [...]]]></description>
			<content:encoded><![CDATA[<p>I did complete Problem 4 already, but the write up will take a bit of time.  So I went ahead and did <a href="http://projecteuler.net/index.php?section=problems&#038;id=5">Problem 5</a>, in a very quick and dirty way.  </p>
<p>Problem 5 &#8211; &#8220;What is the smallest number that is evenly divisible by all of the numbers from 1 to 20?&#8221;</p>
<p>The total elapsed time from reading the problem to having the answer was less than 10 minutes.  The first instinct is to just brute force the thing, and that&#8217;s what I did. </p>
<p> I did make some small optimizations, things that occurred to me in the couple of minutes I spent thinking about it.</p>
<p> 1 &#8211; There&#8217;s no point in testing divisibility by numbers 1-10, since anything that&#8217;s divisible by 11-20 will of necessity by divisible by 1-10. (ie each of the numbers 1-10 are factors of at least one of the numbers 11-20).</p>
<p>2- There&#8217;s no point in testing divisibility by 20, since we&#8217;re starting with 20 and incrementing by 20.  All candidates will be divisible by 20.</p>
<p>I have no doubt that this can be optimized considerably.  It takes 22 seconds to complete on my netbook, an eternity!  But this code makes me laugh, so here it is.  </p>
<p>Called by euler5:lowest(20).</p>
<p>Brutish code -</p>
<pre>
 -module(euler5).
 -export([lowest/1]).

  lowest(N) ->
     if
	N rem 19 == 0,
 	N rem 18 == 0,
 	N rem 17 == 0,
 	N rem 16 == 0,
 	N rem 15 == 0,
 	N rem 14 == 0,
 	N rem 13 == 0,
 	N rem 12 == 0,
 	N rem 11 == 0 ->
              N;
      true ->
          lowest(N+20)
      end.
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.nopermission.com/2009/09/project-euler-problem-5-in-dirty-dirty-erlang/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Euler, Problem 3, in Erlang</title>
		<link>http://www.nopermission.com/2009/09/project-euler-problem-3-in-erlang/</link>
		<comments>http://www.nopermission.com/2009/09/project-euler-problem-3-in-erlang/#comments</comments>
		<pubDate>Fri, 04 Sep 2009 15:39:22 +0000</pubDate>
		<dc:creator>Rod Ramsey</dc:creator>
				<category><![CDATA[erlang]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[project euler]]></category>

		<guid isPermaLink="false">http://www.nopermission.com/?p=104</guid>
		<description><![CDATA[When I first read  Problem 3  I thought it was going to be more involved than it turned out to be. 
&#8220;What is the largest prime factor of the number 600851475143 ?&#8221;
And I started thinking I&#8217;ll need to list the factors and test them for primality, or find primes first and test only [...]]]></description>
			<content:encoded><![CDATA[<p>When I first read <a href="http://projecteuler.net/index.php?section=problems&#038;id=3"> Problem 3 </a> I thought it was going to be more involved than it turned out to be. </p>
<p>&#8220;What is the largest prime factor of the number 600851475143 ?&#8221;</p>
<p>And I started thinking I&#8217;ll need to list the factors and test them for primality, or find primes first and test only those for being factors, etc.  </p>
<p>But after thinking about how to actually factor the number, I remembered factor trees and how they&#8217;d lead me to prime factors by default.  And as an added bonus, factor trees are inherently recursive!  This wasn&#8217;t going to be so bad after all.  </p>
<p>For some reason I got hooked on the idea that I needed to build a list of the factors, and then at the end I would find the largest of them.  Eventually I realized I just needed to track the largest so far; I think I just wanted to fiddle with lists in Erlang.  </p>
<p>The Erlang came pretty easy to me this time. I&#8217;m adjusting to the structures imposed by Single Assignment, which was conceptually a challenge at first.  The idea of immutable variables just seemed so wrong! But I&#8217;ve read more and understand why that&#8217;s the case and I&#8217;m getting a feel for how to deal with them.  </p>
<p>The only real Erlang catch came from the line that finds the co-factor of the most recently found factor  &#8211; &#8220;Cofactor = round(N / Factor)&#8221;.  You&#8217;ll notice the &#8220;round&#8221; function there.  The division returned a floating point number, when what I wanted was an integer.  Casting variable types turns out to be a bit clunky in Erlang, mostly done by BIFs (Built In Functions) with unwieldy names like &#8220;integer_to_list&#8221; and so on.  But there is no &#8220;float_to_integer&#8221; BIF!  So it had to be the &#8220;round()&#8221; function.  While fine in this case, I&#8217;m not sure this will always do what I need it to do.</p>
<p>A couple of other notes- This isn&#8217;t very optimized, I&#8217;m intentionally not optimizing in this initial pass.  I do plan to revisit and refactor down the road, once I have a better idea what I&#8217;m doing. I did sneak in an optimization in the factor search, limiting the upper end to the square root of the number we&#8217;re looking for factors of.  That&#8217;s a very simple trick which does save some iterations (if you haven&#8217;t found a factor by the time you reach the square root of a number, you&#8217;re not going to).  I could have further limited the search range by limiting the lower end to being equal to or greater than the previously found factors, but that would add parameters to the functions so I skipped it for now. </p>
<p>One last thing, the program doesn&#8217;t seem to work properly on very large numbers (giving me a factor of &#8220;2&#8243; on numbers that aren&#8217;t even, for example), I&#8217;m not sure why.  Probably some sort of overflow condition.</p>
<p>My code of codes-</p>
<pre>
 -module(euler3).
  -import(math).
  -export([greatestprimefactor/1]).

  greatestprimefactor(N) ->
     Greatest = factor(1,N),
     io:format("GPF is ~w~n", [Greatest]).

  factor(Greatestsofar,N) ->
    Max=math:sqrt(N),
    Factor = nextfactor(Max,2,N),
    if
      Factor > Greatestsofar ->
            Newgreatest = Factor;
      true ->
            Newgreatest = Greatestsofar
    end,
    Cofactor = round(N / Factor),
    io:format("Factor ~w~n",[Factor]),
    if
      Cofactor == 1 ->
           Newgreatest;
      true->
           factor(Newgreatest,Cofactor)
    end.

 nextfactor(Max,X,N)->
      if
       N rem X == 0 ->
 	    X;
       X =< Max ->
            nextfactor(Max,X+1,N);
       true ->
            N
       end.
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.nopermission.com/2009/09/project-euler-problem-3-in-erlang/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Euler in Erlang, Problem 2</title>
		<link>http://www.nopermission.com/2009/09/project-euler-in-erlang-problem-2/</link>
		<comments>http://www.nopermission.com/2009/09/project-euler-in-erlang-problem-2/#comments</comments>
		<pubDate>Thu, 03 Sep 2009 03:48:21 +0000</pubDate>
		<dc:creator>Rod Ramsey</dc:creator>
				<category><![CDATA[erlang]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[project euler]]></category>

		<guid isPermaLink="false">http://www.nopermission.com/?p=96</guid>
		<description><![CDATA[Problem 2 is again stated simply- &#8220;Find the sum of all the even-valued terms in the (Fibonacci) sequence which do not exceed four million.&#8221;
The Fibonacci Sequence is the sequence which runs 1, 2, 3, 5, 8, 13, 21  etc. in which each term is the sum of the previous two terms.
Again, conceptually easy.  [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://projecteuler.net/index.php?section=problems&#038;id=2">Problem 2</a> is again stated simply- &#8220;Find the sum of all the even-valued terms in the (Fibonacci) sequence which do not exceed four million.&#8221;</p>
<p>The Fibonacci Sequence is the sequence which runs 1, 2, 3, 5, 8, 13, 21  etc. in which each term is the sum of the previous two terms.</p>
<p>Again, conceptually easy.  Just run the terms until one exceeds 4,000,000 and sum those which are divisible by 2.</p>
<p>In a procedural language, this is another problem that I can solve as fast as I can type.  It&#8217;s very straight-forward in approach.</p>
<p>But I&#8217;ve quickly learned that recursion is the default thought-process for the functional language Erlang, so I immediately set about trying to think of the problem recursively.  I admit to heading down a couple of dead end paths at first, really just approaching it from the wrong end.  I kept worrying about building the terms when what I really wanted to do was focus on accumulating the sum.  Once I realized that, everything fell into place quickly.  The end result is a little messy, having to call the function with seed values &#8211; sumofevenfib(1,1,0) and having the cutoff value of 4,000,000 hardcoded.  But it works.</p>
<p>Only a couple of problems into the euler series, but I&#8217;m getting the effects I wanted.  Spending a lot more time studying up on Erlang, reading about why it&#8217;s designed the way it is, and understanding what the syntax is really saying and so on.  Also learning to move from the procedural-by-reflex mindset I&#8217;ve developed over years and years of coding to a more functional approach.  There&#8217;s definitely a switch I need to flip over in my head about that kind of thing.</p>
<p>Le code -</p>
<pre>

-module(euler2).
-export([sumofevenfib/3]).

sumofevenfib(Prev,Cur,Sum) ->
   Next = Prev + Cur,
   if
      Next =< 4000000 ->
       if
         Next rem 2 == 0 ->
           sumofevenfib(Cur,Next,Sum+Next);
         true ->
           sumofevenfib(Cur,Next,Sum)
       end;
      true -> Sum
    end.
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.nopermission.com/2009/09/project-euler-in-erlang-problem-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Euler, Problem 1</title>
		<link>http://www.nopermission.com/2009/09/project-euler-problem-1/</link>
		<comments>http://www.nopermission.com/2009/09/project-euler-problem-1/#comments</comments>
		<pubDate>Wed, 02 Sep 2009 06:39:45 +0000</pubDate>
		<dc:creator>Rod Ramsey</dc:creator>
				<category><![CDATA[erlang]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[project euler]]></category>

		<guid isPermaLink="false">http://www.nopermission.com/?p=84</guid>
		<description><![CDATA[Getting started with my quest via my first attempt at a solution in Erlang to Project Euler&#8217;s problem number 1.
The problem itself is simply stated : &#8220;Find the sum of all the multiples of 3 or 5 below 1000.&#8221;
A naive algorithm is immediately apparent- cycle through each number between below 1000 and sum those that [...]]]></description>
			<content:encoded><![CDATA[<p>Getting started with <a href="http://www.nopermission.com/2009/09/learning-erlang-via-euler/">my quest</a> via my first attempt at a solution in Erlang to <a href="http://projecteuler.net/">Project Euler&#8217;s</a> problem number 1.</p>
<p>The problem itself is simply stated : &#8220;Find the sum of all the multiples of 3 or 5 below 1000.&#8221;</p>
<p>A naive algorithm is immediately apparent- cycle through each number between below 1000 and sum those that are evenly divisible by 5 or 3.  While hardly sophisticated or efficient, it&#8217;s an algorithm that will work.</p>
<p>In thinking about how to do this in a functional language like Erlang, it seems the obvious approach is recursion.  A function that takes a number N as an input and calls itself for N-1, adding N to the result of the recursive call if  N is evenly divisible by 3 or 5.   I also decided to make the determination if N was a 3 or 5 multiple its own function.</p>
<p>So conceptually, the answer and the approach came pretty quickly. I stumbled a bit on implementation mostly due to not understanding Erlang&#8217;s if-statement at all.  Once I sussed out the oddities there (including not being able to call a function in a guard expression- this puzzles me, but I also get the feeling that understanding why this is true is important to my grokking Erlang.  I must explore further), the functions fell into place and I had a tiny understanding of how to start thinking about this sort of thing.</p>
<p>It&#8217;s been too long for me.  Tiny little baby steps needed. I could have written this in a C-like language as fast as I can type.  This took a bit longer.  <img src='http://www.nopermission.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>My magnificent code for Problem 1.</p>
<pre>
-module(euler1).
-export([sumof3and5multiples/1]).

sumof3and5multiples(0)->
	0;
sumof3and5multiples(N) ->
	Multiple = ismultipleof3or5(N),
	if
          Multiple == true ->
	    N + sumof3and5multiples(N-1);
          true ->
	    sumof3and5multiples(N-1)
	end.   	

ismultipleof3or5(X) ->
	if
	   X rem 3 == 0 ->
	     true;
	   X rem 5 == 0 ->
	     true;
	   true ->
	     false
	end.
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.nopermission.com/2009/09/project-euler-problem-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

