<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
		>
<channel>
	<title>Comments for Programming Praxis</title>
	<atom:link href="http://programmingpraxis.com/comments/feed/" rel="self" type="application/rss+xml" />
	<link>http://programmingpraxis.com</link>
	<description>A collection of etudes, updated weekly, for the education and enjoyment of the savvy programmer</description>
	<lastBuildDate>Wed, 22 Feb 2012 18:40:47 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
	<item>
		<title>Comment on Learn A New Language by Paul G.</title>
		<link>http://programmingpraxis.com/2012/02/21/learn-a-new-language-2/#comment-4411</link>
		<dc:creator><![CDATA[Paul G.]]></dc:creator>
		<pubDate>Wed, 22 Feb 2012 18:40:47 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=5569#comment-4411</guid>
		<description><![CDATA[I&#039;m a little surprised (and humbled) to be the first to post an answer to this challenge. Back when I was in College, I learned about Touring Completeness. At the time, I didn&#039;t give it much thought until about 5 or so years ago, I stumbled across a programming language called BrainF**k (BF for short). I didn&#039;t care for the name, but the concept was truly fascinating. The language simulates a one tape Touring Machine using only 8 instructions which manipulate one pointer and tape data under that pointer. In theory, this is all that&#039;s needed to write any program in the world. When I saw this 5 years ago, I wrote a Hello World to prove the concept to myself, and didn&#039;t give it another thought ... until now.

I decided to write a program which would output the HailStone sequence defined by the previous exercise, using BF. Well, almost. I actually used Procedural BF (pBrain for short) which slightly extends the instruction sets to add capability to define and call functions in 3 additional instructions. You can read more about pBrain at http://www.parkscomputing.com/code/pbrain/. To simplify my work, I made a couple of assumptions. The user input of the starting number must be between 01 and 99, always entered as two characters with a leading zero. The output will always be displayed as a 3 digit number with leading zeros. This touring machine has an 8-bit memory, so no single number within the sequence must exceed 255. 

Without further ado, here is my Touring Machine (with procedural enhancements) implementation of a HailStone sequence. There are plenty of comments in the code. Frankly, I needed them to keep myself sane. Of course, the nice side effect of the comments is that everyone should be able to understand what is going on. As an example, when run with an input like 07, the output is 007 022 011 034 017 052 026 013 040 020 010 005 016 008 004 002 001 

[sourcecode lang=&quot;bf&quot;]
/**************************************/
/*    HAILSTONE SEQUENCE PROGRAM      */
/*          in pBrain                 */
/**************************************/

// Memory map
//   0: ASCII_MSB
//   1: ASCII_LSB 
//   2: User_Input
//   3: R1
//   4: R2
//   5: R3
//   6: R4
//   7: R5
//   8: R6
//   9: R7
//   A: R8
//   B: R9
//   C: R10
//   D: R11
//   E: R12
//   F: R13
//  10: R14
//  11: R15
//  12: R16

/**************************************/
/* Function 1 - Convert ASCII(n) to integer N by subtracting 6*8=ASCII(0) */
/* Input:   @(-1) - the ASCII value of n */
/* Output:  @(-1) - the integer value N */
/* Local:   @(+1) and @(+2) */
+(
// loop 6*8 times, decrementing @(-1)
   &gt;++++++++[
     &gt;++++++[
       &lt;&lt;&lt;-
       &gt;&gt;&gt;- 
     ]&lt;-
   ]
   
// Exit with calling register set back to zero
   &lt;-
)

/*************************************/
/* Function 2 - Divide A/B           */
/* Input:   @(-2) - Divident A       */
/* Input:   @(-1) - Divisor B        */
/* Local:   @(+1) - Copy of dividend */
/* Local:   @(+2) - Copy of dividend */
/* Local:   @(+3) - Remainder        */
/* Local:   @(+4) - Copy of divisor  */
/* Local:   @(+5) - Quotient         */
/* Local:   @(+6) - Zero             */
/* Local:   @(+7) - Zero             */
/* Output:  @(+5) - Quotient         */
/* Output:  @(+3) - Remainder        */
+(
// make sure the local variables are zero&#039;d out
   &gt;[-]&gt;[-]&gt;[-]&gt;[-]&gt;[-]&gt;[-]&gt;[-]&lt;&lt;&lt;&lt;&lt;&lt;&lt;
// copy dividend to @(+1) and @(+2) destroying @(-2)
   &lt;&lt;[&gt;&gt;&gt;+&gt;+&lt;&lt;&lt;&lt;-]
// copy divisor to @(+4) destroying @(-1)
   &gt;[&gt;&gt;&gt;&gt;&gt;+&lt;&lt;&lt;&lt;&lt;-]
 
// point to first copy of dividend and start division 
   &gt;&gt;
   [               
      &gt;+&gt;+&gt;-[&gt;&gt;&gt;]        
      &lt;[[&gt;+&lt;-]&gt;&gt;+&gt;]   
      &lt;&lt;&lt;&lt;&lt;-     
   ]
   
// Exit with calling register set back to zero
   &lt;--
)

/**************************************/
/* Function 3 - display integer 0-255 as ASCII      */
/* Input:   @(-1) - the ASCII value of n            */
/* Local:   @(+1), @(+2), @(+3)  - ASCII result     */
/* Local:   @(+4) through @(+13) - used in division */
+(
// Make sure that we start with a clean slate for internal output registers
   [-]&gt;[-]&gt;[-]&gt;[-]

// Figure out the 100&#039;s digit.
// Copy number to divident
   &lt;&lt;&lt;&lt;[&gt;+&gt;&gt;&gt;&gt;+&lt;&lt;&lt;&lt;&lt;-]&gt;[&lt;+&gt;-]
// Set divisor to 10*10=100
   &gt;&gt;&gt;&gt;&gt;&gt;++++++++++[&gt;++++++++++[&lt;&lt;+&gt;&gt;-]&lt;-]&lt;
// Call division function 
   &gt;++:
// Process results of division
   &gt;&gt;&gt;&gt;&gt;
   [
// Copy quotient into MSB
      &lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;+
// subtract quotient*100 from number
      &gt;++++++++++ [&gt;++++++++++ [&lt;&lt;&lt;&lt;&lt;&lt;-&gt;&gt;&gt;&gt;&gt;&gt;-]&lt;- ]
      &gt;&gt;&gt;&gt;&gt;&gt;&gt;-
   ]
// Convert to ascii by adding 6*8=48
   &lt;&lt;&lt;&lt;&lt;&lt;[-]&lt;[-]++++++ [ &gt;++++++++ [&lt;&lt;+&gt;&gt;-]&lt;- ]
   
// Figure out the 10&#039;s digit
   &gt;&gt;&gt;[-]&lt;[-]&lt;[-]
// Copy number to divident
   &lt;&lt;&lt;&lt;&lt;&lt;[&gt;+&gt;&gt;&gt;&gt;+&lt;&lt;&lt;&lt;&lt;-]&gt;[&lt;+&gt;-]
// Set divisor to 2*5=10
   &gt;&gt;&gt;&gt;&gt;&gt;++[&gt;+++++[&lt;&lt;+&gt;&gt;-]&lt;-]&lt;
// Call division function 
   &gt;++:
// Process results of division
   &gt;&gt;&gt;&gt;&gt;
   [
// Copy quotient into Middle digit
      &lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;+
// subtract quotient*10 from number
      &gt;&gt;++ [ &gt;+++++ [&lt;&lt;&lt;&lt;&lt;&lt;-&gt;&gt;&gt;&gt;&gt;&gt;-]&lt;- ]
      &gt;&gt;&gt;&gt;&gt;&gt;&gt;-
   ]
// Convert to ascii by adding 6*8=48
   &lt;&lt;&lt;&lt;&lt;&lt;[-]&lt;[-]++++++ [ &gt;++++++++ [&lt;&lt;&lt;+&gt;&gt;&gt;-]&lt;- ]
   
// Copy the remainder (1&#039;s digit) into LSB
   &gt;&gt;&gt;&gt;&gt;[&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;+&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;-]
// Convert to ascii by adding 6*8=48
   &lt;&lt;&lt;&lt;[-]&lt;[-]++++++ [ &gt;++++++++ [&lt;&lt;&lt;&lt;+&gt;&gt;&gt;&gt;-]&lt;- ]
   
// Display the number with a space after it
   &lt;.&lt;.&lt;.[-]&gt;[-]&gt;[-]&lt; ++++ [ &gt;++++++++ [&lt;&lt;+&gt;&gt;-]&lt;- ] &lt;.

// exit with calling register set to zero
   &lt;[-]
)

/**************************************/
/**************** MAIN ****************/
// Get user input for MSB and LSB stored in @ASCII_MSB and @ASCII_LSB
,&gt;,

// Move @ASCII_MSB to R1
&lt;[&gt;&gt;&gt;+&lt;&lt;&lt;-]
// Call Func1 from R2, using R1 as arg1, R3 and R4 as local vars
&gt;&gt;&gt;&gt;+:

// Move @ASCII_LSB to R2
&lt;&lt;&lt;[&gt;&gt;&gt;+&lt;&lt;&lt;-]
// Call Func1 from R3, using R2 as arg1, R4 and R5 as local vars
&gt;&gt;&gt;&gt;+:

// @User_Input = R1*10 + R2, destroying R1, R2 contents
&lt;&lt;[&lt;++++++++++&gt;-]
&gt;[&lt;&lt;+&gt;&gt;-]

// Copy @User_input to R1 using R2
// Display R1 by calling Func3 from R2, using R3-R16 as local vars
&lt;&lt; [&gt;+&gt;+&lt;&lt;-] &gt;&gt;[&lt;&lt;+&gt;&gt;-] +++:

// While @User_Input != 1
&lt;&lt;-
[
// Copy @User_Input to R1, using R2, set R2 to two
   +&gt;[-]&lt;[&gt;+&gt;+&lt;&lt;-]&gt;&gt;[&lt;&lt;+&gt;&gt;-]++
// Call Func2 from R3, using R1 and R2 as arg1 and arg2, and R4-R10 as local vars
   &gt;[-]++:

// Test R6 (remainder) to see if it&#039;s Zero, use R7 for starting else clause
   &gt;&gt;&gt;&gt;[-]+&lt;
   [
// Remainder is not Zero, must have been an odd number
// @user_Input = 3*@User_input + 1
      &lt;&lt;&lt;&lt;&lt;[-]&lt;[&gt;+++&lt;-]&gt;+[&lt;+&gt;-]
// Set remainder and R7 to zero to skip else clause
      &gt;&gt;&gt;&gt;&gt;&gt;-&lt;-
   ]
   &gt;
   [
// Remainder is Zero, must have been an even number
// Copy R8 (quotient) to @User_input
      &lt;&lt;&lt;&lt;&lt;&lt;&lt;[-]&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;[&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;+&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;-]&lt;[-]
   ]
   
// Copy @User_input to R1 using R2
// Display R1 by calling Func3 from R2, using R3-R16 as local vars
   &lt;&lt;&lt;&lt;&lt;&lt;&lt; [&gt;+&gt;+&lt;&lt;-] &gt;&gt;[&lt;&lt;+&gt;&gt;-] +++:
   
// Test if @User_Input == 1
   &lt;&lt;- 
]
[/sourcecode]]]></description>
		<content:encoded><![CDATA[<p>I&#8217;m a little surprised (and humbled) to be the first to post an answer to this challenge. Back when I was in College, I learned about Touring Completeness. At the time, I didn&#8217;t give it much thought until about 5 or so years ago, I stumbled across a programming language called BrainF**k (BF for short). I didn&#8217;t care for the name, but the concept was truly fascinating. The language simulates a one tape Touring Machine using only 8 instructions which manipulate one pointer and tape data under that pointer. In theory, this is all that&#8217;s needed to write any program in the world. When I saw this 5 years ago, I wrote a Hello World to prove the concept to myself, and didn&#8217;t give it another thought &#8230; until now.</p>
<p>I decided to write a program which would output the HailStone sequence defined by the previous exercise, using BF. Well, almost. I actually used Procedural BF (pBrain for short) which slightly extends the instruction sets to add capability to define and call functions in 3 additional instructions. You can read more about pBrain at <a href="http://www.parkscomputing.com/code/pbrain/" rel="nofollow">http://www.parkscomputing.com/code/pbrain/</a>. To simplify my work, I made a couple of assumptions. The user input of the starting number must be between 01 and 99, always entered as two characters with a leading zero. The output will always be displayed as a 3 digit number with leading zeros. This touring machine has an 8-bit memory, so no single number within the sequence must exceed 255. </p>
<p>Without further ado, here is my Touring Machine (with procedural enhancements) implementation of a HailStone sequence. There are plenty of comments in the code. Frankly, I needed them to keep myself sane. Of course, the nice side effect of the comments is that everyone should be able to understand what is going on. As an example, when run with an input like 07, the output is 007 022 011 034 017 052 026 013 040 020 010 005 016 008 004 002 001 </p>
<p>/**************************************/<br />
/*    HAILSTONE SEQUENCE PROGRAM      */<br />
/*          in pBrain                 */<br />
/**************************************/</p>
<p>// Memory map<br />
//   0: ASCII_MSB<br />
//   1: ASCII_LSB<br />
//   2: User_Input<br />
//   3: R1<br />
//   4: R2<br />
//   5: R3<br />
//   6: R4<br />
//   7: R5<br />
//   8: R6<br />
//   9: R7<br />
//   A: R8<br />
//   B: R9<br />
//   C: R10<br />
//   D: R11<br />
//   E: R12<br />
//   F: R13<br />
//  10: R14<br />
//  11: R15<br />
//  12: R16</p>
<p>/**************************************/<br />
/* Function 1 - Convert ASCII(n) to integer N by subtracting 6*8=ASCII(0) */<br />
/* Input:   @(-1) - the ASCII value of n */<br />
/* Output:  @(-1) - the integer value N */<br />
/* Local:   @(+1) and @(+2) */<br />
+(<br />
// loop 6*8 times, decrementing @(-1)<br />
   &gt;++++++++[<br />
     &gt;++++++[<br />
       &lt;&lt;&lt;-<br />
       &gt;&gt;&gt;-<br />
     ]&lt;-<br />
   ]</p>
<p>// Exit with calling register set back to zero<br />
   &lt;-<br />
)</p>
<p>/*************************************/<br />
/* Function 2 - Divide A/B           */<br />
/* Input:   @(-2) - Divident A       */<br />
/* Input:   @(-1) - Divisor B        */<br />
/* Local:   @(+1) - Copy of dividend */<br />
/* Local:   @(+2) - Copy of dividend */<br />
/* Local:   @(+3) - Remainder        */<br />
/* Local:   @(+4) - Copy of divisor  */<br />
/* Local:   @(+5) - Quotient         */<br />
/* Local:   @(+6) - Zero             */<br />
/* Local:   @(+7) - Zero             */<br />
/* Output:  @(+5) - Quotient         */<br />
/* Output:  @(+3) - Remainder        */<br />
+(<br />
// make sure the local variables are zero'd out<br />
   &gt;[-]&gt;[-]&gt;[-]&gt;[-]&gt;[-]&gt;[-]&gt;[-]&lt;&lt;&lt;&lt;&lt;&lt;&lt;<br />
// copy dividend to @(+1) and @(+2) destroying @(-2)<br />
   &lt;&lt;[&gt;&gt;&gt;+&gt;+&lt;&lt;&lt;&lt;-]<br />
// copy divisor to @(+4) destroying @(-1)<br />
   &gt;[&gt;&gt;&gt;&gt;&gt;+&lt;&lt;&lt;&lt;&lt;-]</p>
<p>// point to first copy of dividend and start division<br />
   &gt;&gt;<br />
   [<br />
      &gt;+&gt;+&gt;-[&gt;&gt;&gt;]<br />
      &lt;[[&gt;+&lt;-]&gt;&gt;+&gt;]<br />
      &lt;&lt;&lt;&lt;&lt;-<br />
   ]</p>
<p>// Exit with calling register set back to zero<br />
   &lt;--<br />
)</p>
<p>/**************************************/<br />
/* Function 3 - display integer 0-255 as ASCII      */<br />
/* Input:   @(-1) - the ASCII value of n            */<br />
/* Local:   @(+1), @(+2), @(+3)  - ASCII result     */<br />
/* Local:   @(+4) through @(+13) - used in division */<br />
+(<br />
// Make sure that we start with a clean slate for internal output registers<br />
   [-]&gt;[-]&gt;[-]&gt;[-]</p>
<p>// Figure out the 100's digit.<br />
// Copy number to divident<br />
   &lt;&lt;&lt;&lt;[&gt;+&gt;&gt;&gt;&gt;+&lt;&lt;&lt;&lt;&lt;-]&gt;[&lt;+&gt;-]<br />
// Set divisor to 10*10=100<br />
   &gt;&gt;&gt;&gt;&gt;&gt;++++++++++[&gt;++++++++++[&lt;&lt;+&gt;&gt;-]&lt;-]&lt;<br />
// Call division function<br />
   &gt;++:<br />
// Process results of division<br />
   &gt;&gt;&gt;&gt;&gt;<br />
   [<br />
// Copy quotient into MSB<br />
      &lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;+<br />
// subtract quotient*100 from number<br />
      &gt;++++++++++ [&gt;++++++++++ [&lt;&lt;&lt;&lt;&lt;&lt;-&gt;&gt;&gt;&gt;&gt;&gt;-]&lt;- ]<br />
      &gt;&gt;&gt;&gt;&gt;&gt;&gt;-<br />
   ]<br />
// Convert to ascii by adding 6*8=48<br />
   &lt;&lt;&lt;&lt;&lt;&lt;[-]&lt;[-]++++++ [ &gt;++++++++ [&lt;&lt;+&gt;&gt;-]&lt;- ]</p>
<p>// Figure out the 10's digit<br />
   &gt;&gt;&gt;[-]&lt;[-]&lt;[-]<br />
// Copy number to divident<br />
   &lt;&lt;&lt;&lt;&lt;&lt;[&gt;+&gt;&gt;&gt;&gt;+&lt;&lt;&lt;&lt;&lt;-]&gt;[&lt;+&gt;-]<br />
// Set divisor to 2*5=10<br />
   &gt;&gt;&gt;&gt;&gt;&gt;++[&gt;+++++[&lt;&lt;+&gt;&gt;-]&lt;-]&lt;<br />
// Call division function<br />
   &gt;++:<br />
// Process results of division<br />
   &gt;&gt;&gt;&gt;&gt;<br />
   [<br />
// Copy quotient into Middle digit<br />
      &lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;+<br />
// subtract quotient*10 from number<br />
      &gt;&gt;++ [ &gt;+++++ [&lt;&lt;&lt;&lt;&lt;&lt;-&gt;&gt;&gt;&gt;&gt;&gt;-]&lt;- ]<br />
      &gt;&gt;&gt;&gt;&gt;&gt;&gt;-<br />
   ]<br />
// Convert to ascii by adding 6*8=48<br />
   &lt;&lt;&lt;&lt;&lt;&lt;[-]&lt;[-]++++++ [ &gt;++++++++ [&lt;&lt;&lt;+&gt;&gt;&gt;-]&lt;- ]</p>
<p>// Copy the remainder (1's digit) into LSB<br />
   &gt;&gt;&gt;&gt;&gt;[&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;+&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;-]<br />
// Convert to ascii by adding 6*8=48<br />
   &lt;&lt;&lt;&lt;[-]&lt;[-]++++++ [ &gt;++++++++ [&lt;&lt;&lt;&lt;+&gt;&gt;&gt;&gt;-]&lt;- ]</p>
<p>// Display the number with a space after it<br />
   &lt;.&lt;.&lt;.[-]&gt;[-]&gt;[-]&lt; ++++ [ &gt;++++++++ [&lt;&lt;+&gt;&gt;-]&lt;- ] &lt;.</p>
<p>// exit with calling register set to zero<br />
   &lt;[-]<br />
)</p>
<p>/**************************************/<br />
/**************** MAIN ****************/<br />
// Get user input for MSB and LSB stored in @ASCII_MSB and @ASCII_LSB<br />
,&gt;,</p>
<p>// Move @ASCII_MSB to R1<br />
&lt;[&gt;&gt;&gt;+&lt;&lt;&lt;-]<br />
// Call Func1 from R2, using R1 as arg1, R3 and R4 as local vars<br />
&gt;&gt;&gt;&gt;+:</p>
<p>// Move @ASCII_LSB to R2<br />
&lt;&lt;&lt;[&gt;&gt;&gt;+&lt;&lt;&lt;-]<br />
// Call Func1 from R3, using R2 as arg1, R4 and R5 as local vars<br />
&gt;&gt;&gt;&gt;+:</p>
<p>// @User_Input = R1*10 + R2, destroying R1, R2 contents<br />
&lt;&lt;[&lt;++++++++++&gt;-]<br />
&gt;[&lt;&lt;+&gt;&gt;-]</p>
<p>// Copy @User_input to R1 using R2<br />
// Display R1 by calling Func3 from R2, using R3-R16 as local vars<br />
&lt;&lt; [&gt;+&gt;+&lt;&lt;-] &gt;&gt;[&lt;&lt;+&gt;&gt;-] +++:</p>
<p>// While @User_Input != 1<br />
&lt;&lt;-<br />
[<br />
// Copy @User_Input to R1, using R2, set R2 to two<br />
   +&gt;[-]&lt;[&gt;+&gt;+&lt;&lt;-]&gt;&gt;[&lt;&lt;+&gt;&gt;-]++<br />
// Call Func2 from R3, using R1 and R2 as arg1 and arg2, and R4-R10 as local vars<br />
   &gt;[-]++:</p>
<p>// Test R6 (remainder) to see if it's Zero, use R7 for starting else clause<br />
   &gt;&gt;&gt;&gt;[-]+&lt;<br />
   [<br />
// Remainder is not Zero, must have been an odd number<br />
// @user_Input = 3*@User_input + 1<br />
      &lt;&lt;&lt;&lt;&lt;[-]&lt;[&gt;+++&lt;-]&gt;+[&lt;+&gt;-]<br />
// Set remainder and R7 to zero to skip else clause<br />
      &gt;&gt;&gt;&gt;&gt;&gt;-&lt;-<br />
   ]<br />
   &gt;<br />
   [<br />
// Remainder is Zero, must have been an even number<br />
// Copy R8 (quotient) to @User_input<br />
      &lt;&lt;&lt;&lt;&lt;&lt;&lt;[-]&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;[&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;+&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;-]&lt;[-]<br />
   ]</p>
<p>// Copy @User_input to R1 using R2<br />
// Display R1 by calling Func3 from R2, using R3-R16 as local vars<br />
   &lt;&lt;&lt;&lt;&lt;&lt;&lt; [&gt;+&gt;+&lt;&lt;-] &gt;&gt;[&lt;&lt;+&gt;&gt;-] +++:</p>
<p>// Test if @User_Input == 1<br />
   &lt;&lt;-<br />
]</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on Search In An Ascending Matrix by dmitru</title>
		<link>http://programmingpraxis.com/2012/02/10/search-in-an-ascending-matrix/#comment-4409</link>
		<dc:creator><![CDATA[dmitru]]></dc:creator>
		<pubDate>Wed, 22 Feb 2012 14:20:37 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=5423#comment-4409</guid>
		<description><![CDATA[My recoursive solution:

[sourcecode lang=&quot;python&quot;]
def find(M, key):
    cols = len(M[0])
    def f(i, j):
        if i &lt; 0 or j &gt;= cols:
            return False
        if key &lt; M[i][j]:
            return f(i - 1, j)
        if key &gt; M[i][j]:
            return f(i, j + 1)
        return True
    return f(len(M) - 1, 0)

m = [[1, 5, 7, 9], [4, 6, 10, 15], [8, 11, 12, 19], [14, 16, 18, 21]]

# Tests: 
solve(m, 9)        # True
solve(m, 12)      # True
solev(m, 21)      # True
solve(m, 1000)  # False
solve(m, 17)      # False
[/sourcecode]]]></description>
		<content:encoded><![CDATA[<p>My recoursive solution:</p>
<pre class="brush: python;">
def find(M, key):
    cols = len(M[0])
    def f(i, j):
        if i &lt; 0 or j &gt;= cols:
            return False
        if key &lt; M[i][j]:
            return f(i - 1, j)
        if key &gt; M[i][j]:
            return f(i, j + 1)
        return True
    return f(len(M) - 1, 0)

m = [[1, 5, 7, 9], [4, 6, 10, 15], [8, 11, 12, 19], [14, 16, 18, 21]]

# Tests:
solve(m, 9)        # True
solve(m, 12)      # True
solev(m, 21)      # True
solve(m, 1000)  # False
solve(m, 17)      # False
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on Hailstones by Shankar Chakkere</title>
		<link>http://programmingpraxis.com/2012/02/17/hailstones/#comment-4407</link>
		<dc:creator><![CDATA[Shankar Chakkere]]></dc:creator>
		<pubDate>Wed, 22 Feb 2012 04:48:12 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=5440#comment-4407</guid>
		<description><![CDATA[In Apple I BASIC on Mac OS X (http://www.pagetable.com/?p=35)

#!/usr/bin/apple1basic
1   REM February 21, 2012 11:25 PM 
100 PRINT &quot;ENTER THE NUMBER&quot;
200 INPUT N
300 PRINT N
310 IF N = 1 THEN END
410 IF (N/2)*2 = N THEN GOTO 600
500 REM EVEN NUMBER
510 N = 3 * N +1
530 GOTO 300
600 REM ODD NUMBER
610 N = N /2
630 GOTO 300
900 END

new-host-4:Apple BASIC shankara_c$ apple1basic hailstone.bas 
ENTER THE NUMBER
?13
13
40
20
10
5
16
8
4
2
1]]></description>
		<content:encoded><![CDATA[<p>In Apple I BASIC on Mac OS X (<a href="http://www.pagetable.com/?p=35" rel="nofollow">http://www.pagetable.com/?p=35</a>)</p>
<p>#!/usr/bin/apple1basic<br />
1   REM February 21, 2012 11:25 PM<br />
100 PRINT &#8220;ENTER THE NUMBER&#8221;<br />
200 INPUT N<br />
300 PRINT N<br />
310 IF N = 1 THEN END<br />
410 IF (N/2)*2 = N THEN GOTO 600<br />
500 REM EVEN NUMBER<br />
510 N = 3 * N +1<br />
530 GOTO 300<br />
600 REM ODD NUMBER<br />
610 N = N /2<br />
630 GOTO 300<br />
900 END</p>
<p>new-host-4:Apple BASIC shankara_c$ apple1basic hailstone.bas<br />
ENTER THE NUMBER<br />
?13<br />
13<br />
40<br />
20<br />
10<br />
5<br />
16<br />
8<br />
4<br />
2<br />
1</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on Divisors And Totatives by Shankar Chakkere</title>
		<link>http://programmingpraxis.com/2010/11/26/divisors-and-totatives/#comment-4406</link>
		<dc:creator><![CDATA[Shankar Chakkere]]></dc:creator>
		<pubDate>Wed, 22 Feb 2012 04:10:30 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=3413#comment-4406</guid>
		<description><![CDATA[Here is my take in APPLE 1 BASIC

#!/usr/bin/apple1basic
100 PRINT &quot;ENTER THE NUMBER&quot;
200 INPUT N
300 REM * HOLDS SUM OF DIVISORS
310 LET S = 0
400 REM * HOLDS NUMBER OF DIVISORS
410 LET D = 0
505 FOR I = 1 TO N
510 	  REM IF NOT EXACT DIVISIBLE NEXT NUMBER
600 	  IF (N/I)*I  N THEN NEXT I
610 	  PRINT I 
620 	  S = S + I: D = D + 1
700 NEXT I
800 PRINT &quot;SUM = &quot;;S,&quot;NUMBER OF DIVISORS = &quot;;D
900 END]]></description>
		<content:encoded><![CDATA[<p>Here is my take in APPLE 1 BASIC</p>
<p>#!/usr/bin/apple1basic<br />
100 PRINT &#8220;ENTER THE NUMBER&#8221;<br />
200 INPUT N<br />
300 REM * HOLDS SUM OF DIVISORS<br />
310 LET S = 0<br />
400 REM * HOLDS NUMBER OF DIVISORS<br />
410 LET D = 0<br />
505 FOR I = 1 TO N<br />
510 	  REM IF NOT EXACT DIVISIBLE NEXT NUMBER<br />
600 	  IF (N/I)*I  N THEN NEXT I<br />
610 	  PRINT I<br />
620 	  S = S + I: D = D + 1<br />
700 NEXT I<br />
800 PRINT &#8220;SUM = &#8220;;S,&#8221;NUMBER OF DIVISORS = &#8220;;D<br />
900 END</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on Hailstones by Joe Taylor</title>
		<link>http://programmingpraxis.com/2012/02/17/hailstones/#comment-4405</link>
		<dc:creator><![CDATA[Joe Taylor]]></dc:creator>
		<pubDate>Tue, 21 Feb 2012 15:45:22 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=5440#comment-4405</guid>
		<description><![CDATA[Caveat:
Oh my, the path length from 2,139,935,758 is apparently 2 -- sometimes 32 bits (unsigned) isn&#039;t enough.]]></description>
		<content:encoded><![CDATA[<p>Caveat:<br />
Oh my, the path length from 2,139,935,758 is apparently 2 &#8212; sometimes 32 bits (unsigned) isn&#8217;t enough.</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on Hailstones by Joe Taylor</title>
		<link>http://programmingpraxis.com/2012/02/17/hailstones/#comment-4404</link>
		<dc:creator><![CDATA[Joe Taylor]]></dc:creator>
		<pubDate>Tue, 21 Feb 2012 15:16:34 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=5440#comment-4404</guid>
		<description><![CDATA[&lt;code&gt;
  // just checking
  Console.WriteLine(&quot;Hello world);
&lt;/code&gt;]]></description>
		<content:encoded><![CDATA[<p><code><br />
  // just checking<br />
  Console.WriteLine("Hello world);<br />
</code></p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on Hailstones by Joe Taylor</title>
		<link>http://programmingpraxis.com/2012/02/17/hailstones/#comment-4403</link>
		<dc:creator><![CDATA[Joe Taylor]]></dc:creator>
		<pubDate>Tue, 21 Feb 2012 15:12:56 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=5440#comment-4403</guid>
		<description><![CDATA[OK, so now I have two versions, the recursive, which will very quickly lead to resource abuse, and a non-recursive, which does a little bit less so.  Was able to determine paths for fourch 2.1 * 10^6 elements in 4.5 seconds on my seemingly beefy desktop.
[Apologies for the lack of code style]

        static private int Hail(int x, IDictionary stones)
        {
            int result = 0;
            var trail = new Stack();
            while(x &gt; 0)
            {
                if (stones.ContainsKey(x))
                {
                    result = stones[x];
                    break;
                }
                else if (x &gt; 1)
                {
                    trail.Push(x);
                    x = (x%2) == 0 ? x &gt;&gt; 1 : x*3 + 1;
                }
                else   // x = 1
                {
                    result = stones[x] = 1;
                    break;
                }
            }

            while(trail.Count &gt; 0)
            {
                var key = trail.Pop();
                stones[key] = ++result;
            }

            return result;


        }

        static private int RecursiveHail(int x, IDictionary stones)
        {
            if (x == 1)
            {
                stones[x] = 1;
            }
            else if (!stones.ContainsKey(x))
            {
                stones[x] = (x % 2 == 0) ?
                     1 + Hail(x &gt;&gt; 1, stones) :
                     1 + Hail(x * 3 + 1, stones);
            }
            return stones[x];            
        }]]></description>
		<content:encoded><![CDATA[<p>OK, so now I have two versions, the recursive, which will very quickly lead to resource abuse, and a non-recursive, which does a little bit less so.  Was able to determine paths for fourch 2.1 * 10^6 elements in 4.5 seconds on my seemingly beefy desktop.<br />
[Apologies for the lack of code style]</p>
<p>        static private int Hail(int x, IDictionary stones)<br />
        {<br />
            int result = 0;<br />
            var trail = new Stack();<br />
            while(x &gt; 0)<br />
            {<br />
                if (stones.ContainsKey(x))<br />
                {<br />
                    result = stones[x];<br />
                    break;<br />
                }<br />
                else if (x &gt; 1)<br />
                {<br />
                    trail.Push(x);<br />
                    x = (x%2) == 0 ? x &gt;&gt; 1 : x*3 + 1;<br />
                }<br />
                else   // x = 1<br />
                {<br />
                    result = stones[x] = 1;<br />
                    break;<br />
                }<br />
            }</p>
<p>            while(trail.Count &gt; 0)<br />
            {<br />
                var key = trail.Pop();<br />
                stones[key] = ++result;<br />
            }</p>
<p>            return result;</p>
<p>        }</p>
<p>        static private int RecursiveHail(int x, IDictionary stones)<br />
        {<br />
            if (x == 1)<br />
            {<br />
                stones[x] = 1;<br />
            }<br />
            else if (!stones.ContainsKey(x))<br />
            {<br />
                stones[x] = (x % 2 == 0) ?<br />
                     1 + Hail(x &gt;&gt; 1, stones) :<br />
                     1 + Hail(x * 3 + 1, stones);<br />
            }<br />
            return stones[x];<br />
        }</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on Hailstones by Alec Gorge</title>
		<link>http://programmingpraxis.com/2012/02/17/hailstones/#comment-4401</link>
		<dc:creator><![CDATA[Alec Gorge]]></dc:creator>
		<pubDate>Tue, 21 Feb 2012 03:09:39 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=5440#comment-4401</guid>
		<description><![CDATA[[sourcecode lang=&quot;js&quot;]
var s = require(&#039;util&#039;).puts, n = 13;
for(;n!=1;n=n&amp;1?3*n+1:n/2,s(n+&quot; &quot;));
[/sourcecode]]]></description>
		<content:encoded><![CDATA[<pre class="brush: jscript;">
var s = require('util').puts, n = 13;
for(;n!=1;n=n&amp;1?3*n+1:n/2,s(n+&quot; &quot;));
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on Hailstones by Joe Taylor</title>
		<link>http://programmingpraxis.com/2012/02/17/hailstones/#comment-4399</link>
		<dc:creator><![CDATA[Joe Taylor]]></dc:creator>
		<pubDate>Mon, 20 Feb 2012 23:19:40 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=5440#comment-4399</guid>
		<description><![CDATA[I wass interested in developing a count for the number of items in a sequence for some set of numbers, e.g.:
1 =&gt; 1, length 1
2 =&gt; 2, 1, length 2
3 =&gt; 3, 16, 8, 4, 2, 1, length = 6
4 =&gt; 4, 2, 1, length = 3
etc.

The problem seemed to scream &quot;recursion&quot;, and so it can be solved... up to a point, depending on your resources: this code runs in roughly 400ms on a particularly beefy 64-bit desktop with 16GB RAM and quad core, .Net 4.0
I&#039;m going to see about being a bit less recursive but still performant:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;

namespace Hailstone
{
    class Program
    {
        static void Main(string[] args)
        {
            var maxValue = 100000;
            IDictionary stones = new SortedDictionary();
            var stopwatch = new Stopwatch();
            stopwatch.Start();
            //Enumerable.Range(1, maxValue).AsParallel().ForAll(i =&gt;
            //                                            {
            //                                                _stones[i] = Hail(i);
            //                                            });

            foreach (var i in Enumerable.Range(1, maxValue))
            {
                if (!stones.ContainsKey(i))
                {
                    stones[i] = Hail(i, stones);
                }
            }

            var byValue = stones.OrderBy(kv =&gt; kv.Value).ToDictionary(kv =&gt; kv.Key, kv=&gt;kv.Value);
            stopwatch.Stop();
            Console.WriteLine(stopwatch.Elapsed);
        }

        static private int Hail(int x, IDictionary stones)
        {
            if (x == 1)
            {
                stones[x] = x;
            }
            else if (!stones.ContainsKey(x))
            {
                stones[x] = (x % 2 == 0) ?
                     1 + Hail(x &gt;&gt; 1, stones) :
                     1 + Hail(x*3 + 1, stones);
            }
            return stones[x];
        }
    }
}]]></description>
		<content:encoded><![CDATA[<p>I wass interested in developing a count for the number of items in a sequence for some set of numbers, e.g.:<br />
1 =&gt; 1, length 1<br />
2 =&gt; 2, 1, length 2<br />
3 =&gt; 3, 16, 8, 4, 2, 1, length = 6<br />
4 =&gt; 4, 2, 1, length = 3<br />
etc.</p>
<p>The problem seemed to scream &#8220;recursion&#8221;, and so it can be solved&#8230; up to a point, depending on your resources: this code runs in roughly 400ms on a particularly beefy 64-bit desktop with 16GB RAM and quad core, .Net 4.0<br />
I&#8217;m going to see about being a bit less recursive but still performant:<br />
using System;<br />
using System.Collections.Generic;<br />
using System.Diagnostics;<br />
using System.Linq;<br />
using System.Text;</p>
<p>namespace Hailstone<br />
{<br />
    class Program<br />
    {<br />
        static void Main(string[] args)<br />
        {<br />
            var maxValue = 100000;<br />
            IDictionary stones = new SortedDictionary();<br />
            var stopwatch = new Stopwatch();<br />
            stopwatch.Start();<br />
            //Enumerable.Range(1, maxValue).AsParallel().ForAll(i =&gt;<br />
            //                                            {<br />
            //                                                _stones[i] = Hail(i);<br />
            //                                            });</p>
<p>            foreach (var i in Enumerable.Range(1, maxValue))<br />
            {<br />
                if (!stones.ContainsKey(i))<br />
                {<br />
                    stones[i] = Hail(i, stones);<br />
                }<br />
            }</p>
<p>            var byValue = stones.OrderBy(kv =&gt; kv.Value).ToDictionary(kv =&gt; kv.Key, kv=&gt;kv.Value);<br />
            stopwatch.Stop();<br />
            Console.WriteLine(stopwatch.Elapsed);<br />
        }</p>
<p>        static private int Hail(int x, IDictionary stones)<br />
        {<br />
            if (x == 1)<br />
            {<br />
                stones[x] = x;<br />
            }<br />
            else if (!stones.ContainsKey(x))<br />
            {<br />
                stones[x] = (x % 2 == 0) ?<br />
                     1 + Hail(x &gt;&gt; 1, stones) :<br />
                     1 + Hail(x*3 + 1, stones);<br />
            }<br />
            return stones[x];<br />
        }<br />
    }<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on RPN Calculator by Na dobry początek&#8230; &#187; Maksymilian Węcławski</title>
		<link>http://programmingpraxis.com/2009/02/19/rpn-calculator/#comment-4391</link>
		<dc:creator><![CDATA[Na dobry początek&#8230; &#187; Maksymilian Węcławski]]></dc:creator>
		<pubDate>Mon, 20 Feb 2012 14:32:41 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.wordpress.com/?p=19#comment-4391</guid>
		<description><![CDATA[[...] zadanie to prosty kalkulator do wykonywania podstawowych operacji przy użyciu tzw. odwróconej notacji polskie..., która znakomicie sprawdza się przy traktowaniu danych wejściowych jako stosu (operator [...]]]></description>
		<content:encoded><![CDATA[<p>[...] zadanie to prosty kalkulator do wykonywania podstawowych operacji przy użyciu tzw. odwróconej notacji polskie&#8230;, która znakomicie sprawdza się przy traktowaniu danych wejściowych jako stosu (operator [...]</p>
]]></content:encoded>
	</item>
</channel>
</rss>
