<?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>Dan&#039;s Thoughts &#187; algorithms</title>
	<atom:link href="http://danboykis.com/category/algorithms/feed/" rel="self" type="application/rss+xml" />
	<link>http://danboykis.com</link>
	<description>Thinking somewhat carefully</description>
	<lastBuildDate>Tue, 18 Jan 2011 00:46:24 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Exercise 1.45 of SICP</title>
		<link>http://danboykis.com/2009/07/exercise-1-45-of-sicp/</link>
		<comments>http://danboykis.com/2009/07/exercise-1-45-of-sicp/#comments</comments>
		<pubDate>Thu, 16 Jul 2009 02:17:16 +0000</pubDate>
		<dc:creator>dan</dc:creator>
				<category><![CDATA[algorithms]]></category>
		<category><![CDATA[lisp]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[SICP]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://danboykis.com/?p=1128</guid>
		<description><![CDATA[Exercise 1.45: We saw in section 1.3.3 that attempting to compute square roots by naively finding a fixed point of does not converge, and that this can be fixed by average damping. The same method works for finding cube roots as fixed points of the average-damped . Unfortunately, the process does not work for fourth [...]]]></description>
			<content:encoded><![CDATA[<p><b>Exercise 1.45:</b>  We saw in section 1.3.3 that attempting to compute square roots by naively finding a fixed point of <img src='http://danboykis.com/wp-content/latex/c7f/c7fab3f4d701f5260182910732191113-ffffff-000000-0.png' alt='y \mapsto \frac{x}{y}' title='y \mapsto \frac{x}{y}' class='latex' /> does not converge, and that this can be fixed by average damping. The same method works for finding cube roots as fixed points of the <b>average-damp</b>ed <img src='http://danboykis.com/wp-content/latex/eb1/eb16dfd3882ae37ea3d282e74b64bdef-ffffff-000000-0.png' alt='y \mapsto \frac{x}{y^2}' title='y \mapsto \frac{x}{y^2}' class='latex' />. Unfortunately, the process does not work for fourth roots -- a single average damp is not enough to make a <b>fixed-point</b> search for  <img src='http://danboykis.com/wp-content/latex/cd6/cd63fae9f13b28a2be660b56adfd7c20-ffffff-000000-0.png' alt='y \mapsto \frac{x}{y^3}' title='y \mapsto \frac{x}{y^3}' class='latex' /> converge. On the other hand, if we average damp twice (i.e., use the average damp of the average damp of <img src='http://danboykis.com/wp-content/latex/cd6/cd63fae9f13b28a2be660b56adfd7c20-ffffff-000000-0.png' alt='y \mapsto \frac{x}{y^3}' title='y \mapsto \frac{x}{y^3}' class='latex' />) the <b>fixed-point</b> search does converge. Do some experiments to determine how many average damps are required to compute nth roots as a <b>fixed-point</b> search based upon repeated average damping of <img src='http://danboykis.com/wp-content/latex/500/5003b6b550c8de4f33f3ba51d0e17998-ffffff-000000-0.png' alt='y \mapsto \frac{x}{y^{n-1}}' title='y \mapsto \frac{x}{y^{n-1}}' class='latex' />. Use this to implement a simple procedure for computing n<sup>th</sup> roots using <b>fixed-point</b>, <b>average-damp</b>, and the repeated procedure of exercise 1.43. Assume that any arithmetic operations you need are available as primitives. </p>
<p>Doing experiments on the average number of damps to get the fixed-point procedure to converge seems to be:</p>
<table border="1">
<tr>
<td>Degree of root</td>
<td>Power of 2</td>
<td>Number of average damps</td>
</tr>
<tr>
<td>2 </td>
<td>2<sup>1</sup></td>
<td>1</td>
</tr>
<tr>
<td>4 </td>
<td>2<sup>2</sup></td>
<td>2</td>
</tr>
<tr>
<td>8 </td>
<td>2<sup>3</sup></td>
<td>3</td>
</tr>
<tr>
<td>16</td>
<td>2<sup>4</sup></td>
<td>4</td>
</tr>
<tr>
<td>32</td>
<td>2<sup>5</sup></td>
<td>5</td>
</tr>
</table>
<p>Using this basis it makes sense to write a procedure <strong>discrete-log</strong> that computes the number of powers of 2 in a given number. It's a bit like a very primitive implementation log<sub>2</sub> function hence the name. Discrete-log is <img src='http://danboykis.com/wp-content/latex/285/285b5b93bad20246645ec22894fcc069-ffffff-000000-0.png' alt='\Theta(\log_2 n)' title='\Theta(\log_2 n)' class='latex' /> because it halves the number of computations every iteration.</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>define <span style="color: #66cc66;">&#40;</span>root num degree<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>define <span style="color: #66cc66;">&#40;</span>discrete-log n<span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&lt;</span> n <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #cc66cc;">0</span>
      <span style="color: #66cc66;">&#40;</span>+ <span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">&#40;</span>discrete-log <span style="color: #66cc66;">&#40;</span>/ n <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>define <span style="color: #66cc66;">&#40;</span>compose f g<span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">lambda</span> <span style="color: #66cc66;">&#40;</span>x<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>f <span style="color: #66cc66;">&#40;</span>g x<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>define <span style="color: #66cc66;">&#40;</span>repeated-compose f n<span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&lt;</span> n <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span>
      f
      <span style="color: #66cc66;">&#40;</span>compose f <span style="color: #66cc66;">&#40;</span>repeated-compose f <span style="color: #66cc66;">&#40;</span>- n <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>define <span style="color: #66cc66;">&#40;</span>average-damp f<span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">lambda</span> <span style="color: #66cc66;">&#40;</span>x<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>/ <span style="color: #66cc66;">&#40;</span>+ x <span style="color: #66cc66;">&#40;</span>f x<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>number-of-compositions <span style="color: #66cc66;">&#40;</span>discrete-log degree<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span>initial-<span style="color: #b1b100;">function</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">lambda</span> <span style="color: #66cc66;">&#40;</span>x<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>/ num <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">expt</span> x <span style="color: #66cc66;">&#40;</span>- degree <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cond</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">=</span> degree <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>
          <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">=</span> degree <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> num<span style="color: #66cc66;">&#41;</span>
          <span style="color: #66cc66;">&#40;</span>else
            <span style="color: #66cc66;">&#40;</span>fixed-point 
              <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>repeated-compose average-damp number-of-compositions<span style="color: #66cc66;">&#41;</span> initial-<span style="color: #b1b100;">function</span><span style="color: #66cc66;">&#41;</span>
              <span style="color: #cc66cc;">1.0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>define <span style="color: #66cc66;">&#40;</span>fixed-point f guess<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>define <span style="color: #b1b100;">error</span> <span style="color: #cc66cc;">0.001</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>define <span style="color: #66cc66;">&#40;</span>close-enough? x y<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&lt;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">abs</span> <span style="color: #66cc66;">&#40;</span>- <span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">&#40;</span>/ x y<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">error</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>define <span style="color: #66cc66;">&#40;</span>try guess<span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>new-guess <span style="color: #66cc66;">&#40;</span>f guess<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>close-enough? guess new-guess<span style="color: #66cc66;">&#41;</span>
        new-guess
        <span style="color: #66cc66;">&#40;</span>try new-guess<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>try guess<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>> (root 2 0)<br />
<em>1</em><br />
> (root 2 1)<br />
<em>2</em><br />
> (root 2 2)<br />
<em>1.4142135623746899</em><br />
> (root (expt 2 50) 50)<br />
<em>1.9999956962054166</em><br />
> (root 0.00000001 2)<br />
<em>1.0000000000082464e-4</em></p>
<p>This program summarizes most of Chapter 1. It has lexical scope, passing functions as parameters, functions that construct and return other functions and the square root algorithm with an improved <strong>close-enough?</strong> procedure from exercise 1.7.<br />
The <strong>repeated-compose</strong> and <strong>discrete-log</strong> could be done iteratively instead of recursively like this.</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>define <span style="color: #66cc66;">&#40;</span>repeated-compose f n<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>define <span style="color: #66cc66;">&#40;</span>compose f g<span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">lambda</span> <span style="color: #66cc66;">&#40;</span>x<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>f <span style="color: #66cc66;">&#40;</span>g x<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>define <span style="color: #66cc66;">&#40;</span>iter n func<span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&lt;</span> n <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span>
      func
      <span style="color: #66cc66;">&#40;</span>iter <span style="color: #66cc66;">&#40;</span>- n <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> 
            <span style="color: #66cc66;">&#40;</span>compose f func<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>iter n f<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>define <span style="color: #66cc66;">&#40;</span>discrete-log n<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>define <span style="color: #66cc66;">&#40;</span>iter n exponent<span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&lt;</span> n <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span>
      exponent
      <span style="color: #66cc66;">&#40;</span>iter <span style="color: #66cc66;">&#40;</span>/ n <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>+ <span style="color: #cc66cc;">1</span> exponent<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>iter n <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://danboykis.com/2009/07/exercise-1-45-of-sicp/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Exercise 1.26 of SICP</title>
		<link>http://danboykis.com/2009/06/exercise-1-26-of-sicp/</link>
		<comments>http://danboykis.com/2009/06/exercise-1-26-of-sicp/#comments</comments>
		<pubDate>Wed, 24 Jun 2009 02:33:11 +0000</pubDate>
		<dc:creator>dan</dc:creator>
				<category><![CDATA[algorithms]]></category>
		<category><![CDATA[lisp]]></category>
		<category><![CDATA[SICP]]></category>

		<guid isPermaLink="false">http://danboykis.com/?p=695</guid>
		<description><![CDATA[Exercise 1.26: Louis Reasoner is having great difficulty doing exercise 1.24. His fast-prime? test seems to run more slowly than his prime? test. Louis calls his friend Eva Lu Ator over to help. When they examine Louis's code, they find that he has rewritten the expmod procedure to use an explicit multiplication, rather than calling [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Exercise 1.26:</strong>  Louis Reasoner is having great difficulty doing exercise 1.24. His fast-prime? test seems to run more slowly than his prime? test. Louis calls his friend Eva Lu Ator over to help. When they examine Louis's code, they find that he has rewritten the expmod procedure to use an explicit multiplication, rather than calling square:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>define <span style="color: #66cc66;">&#40;</span>expmod base <span style="color: #b1b100;">exp</span> m<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cond</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">=</span> <span style="color: #b1b100;">exp</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>even? <span style="color: #b1b100;">exp</span><span style="color: #66cc66;">&#41;</span>
         <span style="color: #66cc66;">&#40;</span>remainder <span style="color: #66cc66;">&#40;</span>* <span style="color: #66cc66;">&#40;</span>expmod base <span style="color: #66cc66;">&#40;</span>/ <span style="color: #b1b100;">exp</span> <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span> m<span style="color: #66cc66;">&#41;</span>
                       <span style="color: #66cc66;">&#40;</span>expmod base <span style="color: #66cc66;">&#40;</span>/ <span style="color: #b1b100;">exp</span> <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span> m<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
                    m<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span>else
         <span style="color: #66cc66;">&#40;</span>remainder <span style="color: #66cc66;">&#40;</span>* base <span style="color: #66cc66;">&#40;</span>expmod base <span style="color: #66cc66;">&#40;</span>- <span style="color: #b1b100;">exp</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> m<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
                    m<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>"I don't see what difference that could make," says Louis. "I do." says Eva. "By writing the procedure like that, you have transformed the <img src='http://danboykis.com/wp-content/latex/353/3536ba655433c687015156ca75e9db1c-ffffff-000000-0.png' alt='\Theta(\log n)' title='\Theta(\log n)' class='latex' /> process into a <img src='http://danboykis.com/wp-content/latex/244/2449b2b12012224dbe9f384565e9d9a6-ffffff-000000-0.png' alt='\Theta(n)' title='\Theta(n)' class='latex' /> process." Explain. </p>
<p>Just like the recursive binary tree computational procedure for Fibonacci numbers was exponential so is this process exponential because of the double call to expmod.<br />
This double call creates a tree of depth log<sub>2</sub>(n). The amount of nodes in a tree (the amount of computational steps to perform the algorithm), of depth log<sub>2</sub>(n) is <img src='http://danboykis.com/wp-content/latex/c8b/c8bbfb2455a14331ef898fcb8b7bbc5b-ffffff-000000-0.png' alt='2^{\log_2 n} = n' title='2^{\log_2 n} = n' class='latex' />. This means the algorithm is indeed <img src='http://danboykis.com/wp-content/latex/244/2449b2b12012224dbe9f384565e9d9a6-ffffff-000000-0.png' alt='\Theta(n)' title='\Theta(n)' class='latex' />.   </p>
]]></content:encoded>
			<wfw:commentRss>http://danboykis.com/2009/06/exercise-1-26-of-sicp/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Exercise 1.25 of SICP</title>
		<link>http://danboykis.com/2009/06/exercise-1-25-of-sicp/</link>
		<comments>http://danboykis.com/2009/06/exercise-1-25-of-sicp/#comments</comments>
		<pubDate>Mon, 22 Jun 2009 11:00:40 +0000</pubDate>
		<dc:creator>dan</dc:creator>
				<category><![CDATA[algorithms]]></category>
		<category><![CDATA[lisp]]></category>
		<category><![CDATA[SICP]]></category>

		<guid isPermaLink="false">http://danboykis.com/?p=692</guid>
		<description><![CDATA[Exercise 1.25: Alyssa P. Hacker complains that we went to a lot of extra work in writing expmod. After all, she says, since we already know how to compute exponentials, we could have simply written &#40;define &#40;expmod base exp m&#41; &#40;remainder &#40;fast-expt base exp&#41; m&#41;&#41; Is she correct? Would this procedure serve as well for [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Exercise 1.25:</strong>  Alyssa P. Hacker complains that we went to a lot of extra work in writing expmod. After all, she says, since we already know how to compute exponentials, we could have simply written</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>define <span style="color: #66cc66;">&#40;</span>expmod base <span style="color: #b1b100;">exp</span> m<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>remainder <span style="color: #66cc66;">&#40;</span>fast-<span style="color: #b1b100;">expt</span> base <span style="color: #b1b100;">exp</span><span style="color: #66cc66;">&#41;</span> m<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>Is she correct? Would this procedure serve as well for our fast prime tester? Explain. </p>
<p>While the procedure is mathematically accurate and would produce the right results, fast-expt would produce enormous numbers that would then have to be reduced modulo m. For example:</p>
<p>(expmod 2 10 7)<br />
(remainder (fast-expt 2 10) 7)<br />
(remainder 1024 7)</p>
<p>The 1024 might not seem so bad at first.</p>
<p>(expmod 2 100 7)<br />
(remainder (fast-expt 2 10) 7)<br />
(remainder 1267650600228229401496703205376 7)<br />
That's a bit excessive. </p>
<p>The version bellow keeps the number in check.</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>define <span style="color: #66cc66;">&#40;</span>expmod base <span style="color: #b1b100;">exp</span> m<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cond</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">=</span> <span style="color: #b1b100;">exp</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>even? <span style="color: #b1b100;">exp</span><span style="color: #66cc66;">&#41;</span>
         <span style="color: #66cc66;">&#40;</span>remainder <span style="color: #66cc66;">&#40;</span>square <span style="color: #66cc66;">&#40;</span>expmod base <span style="color: #66cc66;">&#40;</span>/ <span style="color: #b1b100;">exp</span> <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span> m<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
                    m<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span>else
         <span style="color: #66cc66;">&#40;</span>remainder <span style="color: #66cc66;">&#40;</span>* base <span style="color: #66cc66;">&#40;</span>expmod base <span style="color: #66cc66;">&#40;</span>- <span style="color: #b1b100;">exp</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> m<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
                    m<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>I am taking some liberty here in skipping a few minor steps.<br />
(expmod 2 10 7)<br />
(remainder (square (expmod 2 5 7)) 7)<br />
(remainder (square (remainder (* 2 (expmod 2 4 7)) 7)) 7)<br />
(remainder (square (remainder (* 2 (remainder (square (expmod 2 2 7)) 7)) 7)) 7)<br />
(remainder (square (remainder (* 2 (remainder (square (remainder (square (expmod 2 1 7)) 7)) 7) 7) 7)) 7)<br />
(remainder (square (remainder (* 2 (remainder (square (remainder (square (remainder (* 2 1) 7)) 7)) 7)) 7)) 7)<br />
(remainder (square (remainder (* 2 (remainder (square (remainder (square (remainder 2 7)) 7)) 7)) 7)) 7)<br />
(remainder (square (remainder (* 2 (remainder (square (remainder (square 2) 7)) 7)) 7)) 7)<br />
(remainder (square (remainder (* 2 (remainder (square (remainder 4 7)) 7)) 7)) 7)<br />
(remainder (square (remainder (* 2 (remainder (square 4) 7)) 7)) 7)<br />
(remainder (square (remainder (* 2 2) 7)) 7)<br />
(remainder (square (remainder 4 7)) 7)<br />
(remainder (square 4) 7)<br />
2</p>
<p>The key difference is that at no point do any of the numbers grow to be much larger than the base.</p>
]]></content:encoded>
			<wfw:commentRss>http://danboykis.com/2009/06/exercise-1-25-of-sicp/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Exercise 1.24 of SICP</title>
		<link>http://danboykis.com/2009/06/exercise-1-24-of-sicp/</link>
		<comments>http://danboykis.com/2009/06/exercise-1-24-of-sicp/#comments</comments>
		<pubDate>Sun, 21 Jun 2009 00:33:18 +0000</pubDate>
		<dc:creator>dan</dc:creator>
				<category><![CDATA[algorithms]]></category>
		<category><![CDATA[lisp]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[SICP]]></category>

		<guid isPermaLink="false">http://danboykis.com/?p=683</guid>
		<description><![CDATA[Exercise 1.24: Modify the timed-prime-test procedure of exercise 1.22 to use fast-prime? (the Fermat method), and test each of the 12 primes you found in that exercise. Since the Fermat test has (log n) growth, how would you expect the time to test primes near 1,000,000 to compare with the time needed to test primes [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Exercise 1.24:</strong>  Modify the timed-prime-test procedure of exercise 1.22 to use fast-prime? (the Fermat method), and test each of the 12 primes you found in that exercise. Since the Fermat test has (log n) growth, how would you expect the time to test primes near 1,000,000 to compare with the time needed to test primes near 1000? Do your data bear this out? Can you explain any discrepancy you find? </p>
<p>I arbitrarily chose to run the fast-prime? test 100 times. As in 1.22 I ran it only larger ranges than the book suggested.</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>define <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">random</span> n<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>random-<span style="color: #b1b100;">integer</span> n<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>define <span style="color: #66cc66;">&#40;</span>runtime<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>time-<span style="color: #66cc66;">&gt;</span>seconds <span style="color: #66cc66;">&#40;</span>current-time<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>define <span style="color: #66cc66;">&#40;</span>square x<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>* x x<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>define <span style="color: #66cc66;">&#40;</span>square-mod n m<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>remainder <span style="color: #66cc66;">&#40;</span>* n n<span style="color: #66cc66;">&#41;</span> m<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>define <span style="color: #66cc66;">&#40;</span>expmod base <span style="color: #b1b100;">exp</span> m<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cond</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">=</span> <span style="color: #b1b100;">exp</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>even? <span style="color: #b1b100;">exp</span><span style="color: #66cc66;">&#41;</span>
         <span style="color: #66cc66;">&#40;</span>remainder <span style="color: #66cc66;">&#40;</span>square <span style="color: #66cc66;">&#40;</span>expmod base <span style="color: #66cc66;">&#40;</span>/ <span style="color: #b1b100;">exp</span> <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span> m<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
                    m<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span>else
         <span style="color: #66cc66;">&#40;</span>remainder <span style="color: #66cc66;">&#40;</span>* base <span style="color: #66cc66;">&#40;</span>expmod base <span style="color: #66cc66;">&#40;</span>- <span style="color: #b1b100;">exp</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> m<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
                    m<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>        
<span style="color: #66cc66;">&#40;</span>define <span style="color: #66cc66;">&#40;</span>fermat-test n<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>define <span style="color: #66cc66;">&#40;</span>try-it a<span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#40;</span>expmod a n n<span style="color: #66cc66;">&#41;</span> a<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>try-it <span style="color: #66cc66;">&#40;</span>+ <span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">random</span> <span style="color: #66cc66;">&#40;</span>- n <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span>define <span style="color: #66cc66;">&#40;</span>fast-prime? n times<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cond</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">=</span> times <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> #t<span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>fermat-test n<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>fast-prime? n <span style="color: #66cc66;">&#40;</span>- times <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span>else #f<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span>define <span style="color: #66cc66;">&#40;</span>timed-prime-test n<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>newline<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>display n<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>start-prime-test n <span style="color: #66cc66;">&#40;</span>runtime<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>define <span style="color: #66cc66;">&#40;</span>start-prime-test n start-time<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>fast-prime? n <span style="color: #cc66cc;">100</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#40;</span>report-prime <span style="color: #66cc66;">&#40;</span>- <span style="color: #66cc66;">&#40;</span>runtime<span style="color: #66cc66;">&#41;</span> start-time<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>define <span style="color: #66cc66;">&#40;</span>report-prime elapsed-time<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>display <span style="color: #ff0000;">&quot; *** &quot;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>display elapsed-time<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>define <span style="color: #66cc66;">&#40;</span>search-for-primes beg end<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>define <span style="color: #66cc66;">&#40;</span>search beg end num-primes<span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cond</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">=</span> num-primes <span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#41;</span> num-primes<span style="color: #66cc66;">&#41;</span>
          <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&gt;</span> beg end<span style="color: #66cc66;">&#41;</span> num-primes<span style="color: #66cc66;">&#41;</span>
          <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>even? beg<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>search <span style="color: #66cc66;">&#40;</span>+ beg <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> end num-primes<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
          <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>fast-prime? beg <span style="color: #cc66cc;">100</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>timed-prime-test beg<span style="color: #66cc66;">&#41;</span>
                        <span style="color: #66cc66;">&#40;</span>search <span style="color: #66cc66;">&#40;</span>+ beg <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span> end <span style="color: #66cc66;">&#40;</span>+ <span style="color: #cc66cc;">1</span> num-primes<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
          <span style="color: #66cc66;">&#40;</span>else
            <span style="color: #66cc66;">&#40;</span>search <span style="color: #66cc66;">&#40;</span>+ beg <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span> end num-primes<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>search beg end <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>> (search-for-primes (expt 10 10) (- (expt 10 11) 1))</p>
<p><em>10000000019 *** .016000032424926758<br />
10000000033 *** .016000032424926758<br />
10000000061 *** .0160000324249267583</em></p>
<p>> (search-for-primes (expt 10 11) (- (expt 10 12) 1))</p>
<p>100000000003 *** .016000032424926758<br />
100000000019 *** .016000032424926758<br />
100000000057 *** .0150001049041748053</p>
<p>> (search-for-primes (expt 10 12) (- (expt 10 13) 1))</p>
<p><em>1000000000039 *** .016000032424926758<br />
1000000000061 *** 0.<br />
1000000000063 *** .0159997940063476563</em></p>
<p>> (search-for-primes (expt 10 13) (- (expt 10 14) 1))</p>
<p><em>10000000000037 *** .015999794006347656<br />
10000000000051 *** .016000032424926758<br />
10000000000099 *** .0149998664855957033</em></p>
<p>The test is too fast to give meaningful times to compare against. I would expect numbers of order 10<sup>6</sup> to take 2 times longer to compute than 10<sup>3</sup>. The reasoning follows the property of logarithms. log(10<sup>6</sup>) = 6 and log(10<sup>3</sup>) = 3.</p>
<p>In my case:  log(10<sup>10</sup>) = .016<br />
I would predict: log(10<sup>20</sup>) = .032<br />
> (search-for-primes (expt 10 20) (- (expt 10 21) 1))</p>
<p><em>100000000000000000039 *** .03099989891052246<br />
100000000000000000129 *** .03099989891052246<br />
100000000000000000151 *** .030999898910522463</em></p>
]]></content:encoded>
			<wfw:commentRss>http://danboykis.com/2009/06/exercise-1-24-of-sicp/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Exercise 1.23 of SICP</title>
		<link>http://danboykis.com/2009/06/exercise-1-23-of-sicp/</link>
		<comments>http://danboykis.com/2009/06/exercise-1-23-of-sicp/#comments</comments>
		<pubDate>Fri, 19 Jun 2009 11:52:51 +0000</pubDate>
		<dc:creator>dan</dc:creator>
				<category><![CDATA[algorithms]]></category>
		<category><![CDATA[lisp]]></category>
		<category><![CDATA[SICP]]></category>

		<guid isPermaLink="false">http://danboykis.com/?p=665</guid>
		<description><![CDATA[Exercise 1.23: The smallest-divisor procedure shown at the start of this section does lots of needless testing: After it checks to see if the number is divisible by 2 there is no point in checking to see if it is divisible by any larger even numbers. This suggests that the values used for test-divisor should [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Exercise 1.23:</strong> The smallest-divisor procedure shown at the start of this section does lots of needless testing: After it checks to see if the number is divisible by 2 there is no point in checking to see if it is divisible by any larger even numbers. This suggests that the values used for test-divisor should not be 2, 3, 4, 5, 6, ..., but rather 2, 3, 5, 7, 9, .... To implement this change, define a procedure next that returns 3 if its input is equal to 2 and otherwise returns its input plus 2. Modify the smallest-divisor procedure to use (next test-divisor) instead of (+ test-divisor 1). With timed-prime-test incorporating this modified version of smallest-divisor, run the test for each of the 12 primes found in exercise 1.22. Since this modification halves the number of test steps, you should expect it to run about twice as fast. Is this expectation confirmed? If not, what is the observed ratio of the speeds of the two algorithms, and how do you explain the fact that it is different from 2?</p>
<p><strong>12 Primes from 1.22:</strong></p>
<ol>
<li> 10000000019 *** .14100003242492676</li>
<li> 10000000033 *** .1399998664855957</li>
<li> 10000000061 *** .14000010490417483</li>
<li> 100000000003 *** .5310001373291016</li>
<li> 100000000019 *** .5320000648498535</li>
<li> 100000000057 *** .54699993133544923</li>
<li> 1000000000039 *** 1.7660000324249268</li>
<li> 1000000000061 *** 1.75</li>
<li> 1000000000063 *** 1.76500010490417483</li>
<li> 10000000000037 *** 5.672000169754028</li>
<li> 10000000000051 *** 5.672000169754028</li>
<li> 10000000000099 *** 5.65599989891052253</li>
</ol>
<p>Using <strong>time-prime-test</strong> with the new <strong>next </strong>function</p>
<ol>
<li> 10000000019 *** .07799983024597168</li>
<li> 10000000033 *** .07800006866455078</li>
<li> 10000000061 *** .07800006866455078</li>
<li> 100000000003 *** .2969999313354492</li>
<li> 100000000019 *** .29600000381469727</li>
<li> 100000000057 *** .2969999313354492</li>
<li> 1000000000039 *** .9849998950958252</li>
<li> 1000000000061 *** .9840002059936523</li>
<li> 1000000000063 *** .9839999675750732</li>
<li> 10000000000037 *** 3.171999931335449</li>
<li> 10000000000051 *** 3.1570000648498535</li>
<li> 10000000000099 *** 3.171999931335449</li>
</ol>
<table border="1">
<tr>
<td>Using next</td>
<td>Exercise 1.22</td>
<td>Factor of speedup</td>
</tr>
<tr>
<td>0.07799983</td>
<td>0.141000032</td>
<td>1.807696658</td>
</tr>
<tr>
<td>0.078000069</td>
<td>0.139999866</td>
<td>1.794868503</td>
</tr>
<tr>
<td>0.078000069</td>
<td>0.140000105</td>
<td>1.79487156</td>
</tr>
<tr>
<td>0.296999931</td>
<td>0.531000137</td>
<td>1.787879664</td>
</tr>
<tr>
<td>0.296000004</td>
<td>0.532000065</td>
<td>1.797297493</td>
</tr>
<tr>
<td>0.296999931</td>
<td>0.546999931</td>
<td>1.841751036</td>
</tr>
<tr>
<td>0.984999895</td>
<td>1.766000032</td>
<td>1.792893625</td>
</tr>
<tr>
<td>0.984000206</td>
<td>1.75</td>
<td>1.778454912</td>
</tr>
<tr>
<td>0.983999968</td>
<td>1.765000105</td>
<td>1.793699353</td>
</tr>
<tr>
<td>3.171999931</td>
<td>5.67200017</td>
<td>1.788146372</td>
</tr>
<tr>
<td>3.157000065</td>
<td>5.67200017</td>
<td>1.796642399</td>
</tr>
<tr>
<td>3.171999931</td>
<td>5.655999899</td>
<td>1.78310215</td>
</tr>
</table>
<p>Judging by the speedup it does look like there is about a 2 fold increase in speed of determining if a number is prime.</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>define <span style="color: #66cc66;">&#40;</span>runtime<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>time-<span style="color: #66cc66;">&amp;</span>gt<span style="color: #808080; font-style: italic;">;seconds (current-time)))</span>
<span style="color: #66cc66;">&#40;</span>define <span style="color: #66cc66;">&#40;</span>square x<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>* x x<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>define <span style="color: #66cc66;">&#40;</span>smallest-divisor n<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>find-divisor n <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>define <span style="color: #66cc66;">&#40;</span>next n<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">=</span> n <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #cc66cc;">3</span>
    <span style="color: #66cc66;">&#40;</span>+ n <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>define <span style="color: #66cc66;">&#40;</span>find-divisor n test-divisor<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cond</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&amp;</span>gt<span style="color: #808080; font-style: italic;">; (square test-divisor) n) n)</span>
        <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>divides? test-divisor n<span style="color: #66cc66;">&#41;</span> test-divisor<span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span>else <span style="color: #66cc66;">&#40;</span>find-divisor n <span style="color: #66cc66;">&#40;</span>next test-divisor<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>define <span style="color: #66cc66;">&#40;</span>divides? a b<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#40;</span>remainder b a<span style="color: #66cc66;">&#41;</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>define <span style="color: #66cc66;">&#40;</span>prime? n<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">=</span> n <span style="color: #66cc66;">&#40;</span>smallest-divisor n<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span>define <span style="color: #66cc66;">&#40;</span>timed-prime-test n<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>newline<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>display n<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>start-prime-test n <span style="color: #66cc66;">&#40;</span>runtime<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>define <span style="color: #66cc66;">&#40;</span>start-prime-test n start-time<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>prime? n<span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#40;</span>report-prime <span style="color: #66cc66;">&#40;</span>- <span style="color: #66cc66;">&#40;</span>runtime<span style="color: #66cc66;">&#41;</span> start-time<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>define <span style="color: #66cc66;">&#40;</span>report-prime elapsed-time<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>display <span style="color: #ff0000;">&quot; *** &quot;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>display elapsed-time<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://danboykis.com/2009/06/exercise-1-23-of-sicp/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Iterative version of expmod</title>
		<link>http://danboykis.com/2009/06/iterative-version-of-expmod/</link>
		<comments>http://danboykis.com/2009/06/iterative-version-of-expmod/#comments</comments>
		<pubDate>Sun, 14 Jun 2009 09:05:07 +0000</pubDate>
		<dc:creator>dan</dc:creator>
				<category><![CDATA[algorithms]]></category>
		<category><![CDATA[lisp]]></category>
		<category><![CDATA[SICP]]></category>

		<guid isPermaLink="false">http://danboykis.com/?p=621</guid>
		<description><![CDATA[Recursive version: &#40;define &#40;square x&#41; &#40;* x x&#41;&#41; &#40;define &#40;expmod base exp m&#41; &#40;cond &#40;&#40;= exp 0&#41; 1&#41; &#40;&#40;even? exp&#41; &#40;remainder &#40;square &#40;expmod base &#40;/ exp 2&#41; m&#41;&#41; m&#41;&#41; &#40;else &#40;remainder &#40;* base &#40;expmod base &#40;- exp 1&#41; m&#41;&#41; m&#41;&#41;&#41;&#41; I was a bit surprised that the above algorithm didn't come with the problem set [...]]]></description>
			<content:encoded><![CDATA[<p>Recursive version:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>define <span style="color: #66cc66;">&#40;</span>square x<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>* x x<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>define <span style="color: #66cc66;">&#40;</span>expmod base <span style="color: #b1b100;">exp</span> m<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cond</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">=</span> <span style="color: #b1b100;">exp</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>even? <span style="color: #b1b100;">exp</span><span style="color: #66cc66;">&#41;</span>
         <span style="color: #66cc66;">&#40;</span>remainder <span style="color: #66cc66;">&#40;</span>square <span style="color: #66cc66;">&#40;</span>expmod base <span style="color: #66cc66;">&#40;</span>/ <span style="color: #b1b100;">exp</span> <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span> m<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
                    m<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span>else
         <span style="color: #66cc66;">&#40;</span>remainder <span style="color: #66cc66;">&#40;</span>* base <span style="color: #66cc66;">&#40;</span>expmod base <span style="color: #66cc66;">&#40;</span>- <span style="color: #b1b100;">exp</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> m<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
                    m<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>I was a bit surprised that the above algorithm didn't come with the problem set of to rewrite it in an iterative form. So here it is.<br />
Iterative Version:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>define <span style="color: #66cc66;">&#40;</span>square-mod n m<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>remainder <span style="color: #66cc66;">&#40;</span>* n n<span style="color: #66cc66;">&#41;</span> m<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>define <span style="color: #66cc66;">&#40;</span>expmod-iter base n m a<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cond</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">=</span> n <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> a<span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>even? n<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>expmod-iter <span style="color: #66cc66;">&#40;</span>square-mod base m<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>/ n <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span> m a<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span>else
          <span style="color: #66cc66;">&#40;</span>expmod-iter base <span style="color: #66cc66;">&#40;</span>- n <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> m <span style="color: #66cc66;">&#40;</span>remainder <span style="color: #66cc66;">&#40;</span>* a base<span style="color: #66cc66;">&#41;</span> m<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>define <span style="color: #66cc66;">&#40;</span>expmod base n m<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>expmod-iter base n m <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://danboykis.com/2009/06/iterative-version-of-expmod/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Exercise 1.20 of SICP</title>
		<link>http://danboykis.com/2009/06/exercise-120-of-sicp/</link>
		<comments>http://danboykis.com/2009/06/exercise-120-of-sicp/#comments</comments>
		<pubDate>Sat, 13 Jun 2009 23:49:45 +0000</pubDate>
		<dc:creator>dan</dc:creator>
				<category><![CDATA[algorithms]]></category>
		<category><![CDATA[lisp]]></category>
		<category><![CDATA[SICP]]></category>

		<guid isPermaLink="false">http://danboykis.com/?p=600</guid>
		<description><![CDATA[Exercise 1.20: The process that a procedure generates is of course dependent on the rules used by the interpreter. As an example, consider the iterative gcd procedure given above. Suppose we were to interpret this procedure using normal-order evaluation, as discussed in section 1.1.5. (The normal-order-evaluation rule for if is described in exercise 1.5.) Using [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Exercise 1.20:</strong> The process that a procedure generates is of course dependent on the rules used by the interpreter. As an example, consider the iterative gcd procedure given above. Suppose we were to interpret this procedure using normal-order evaluation, as discussed in section 1.1.5. (The normal-order-evaluation rule for if is described in exercise 1.5.) Using the substitution method (for normal order), illustrate the process generated in evaluating (gcd 206 40) and indicate the remainder operations that are actually performed. How many remainder operations are actually performed in the normal-order evaluation of (gcd 206 40)? In the applicative-order evaluation?</p>
<p>GCD Algorithm:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>define <span style="color: #66cc66;">&#40;</span>gcd a b<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">=</span> b <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>
    a
    <span style="color: #66cc66;">&#40;</span>gcd b <span style="color: #66cc66;">&#40;</span>remainder a b<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>Normal Order:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;">R1 <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#40;</span>remainder <span style="color: #cc66cc;">206</span> <span style="color: #cc66cc;">40</span><span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">; 6</span>
<span style="color: #66cc66;">&#40;</span>gcd <span style="color: #cc66cc;">206</span> <span style="color: #cc66cc;">40</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">40</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #cc66cc;">40</span>
  <span style="color: #66cc66;">&#40;</span>gcd <span style="color: #cc66cc;">40</span> R1<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">=</span> R1 <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #cc66cc;">40</span>
  <span style="color: #66cc66;">&#40;</span>gcd R1 <span style="color: #66cc66;">&#40;</span>remainder <span style="color: #cc66cc;">40</span> R1<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
R2 <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#40;</span>remainder <span style="color: #cc66cc;">40</span> R1<span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">; 4</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">=</span> R2 <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>
    R1
    <span style="color: #66cc66;">&#40;</span>gcd R2 <span style="color: #66cc66;">&#40;</span>remainder R1 R2<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
R3 <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#40;</span>remainder R1 R2<span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">; 2</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">=</span> R3 <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>
    R2
    <span style="color: #66cc66;">&#40;</span>gcd R3 <span style="color: #66cc66;">&#40;</span>remainder R2 R3<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
R4 <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#40;</span>remainder R2 R3<span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">; 0</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">=</span> R4 <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>
  R3
  <span style="color: #66cc66;">&#40;</span>gcd <span style="color: #66cc66;">....</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">; Never called because R4 evaluates to 0</span></pre></div></div>

<p>Every time there is an R in an if = statement that R gets evaluated.<br />
#{R1}+#{R2}+#{R3}+#{R4}= 1 + 2 + 4 + 7 = 14 times remainder is run in the if =<br />
R3 is ran once in order to return the actual remainder.<br />
14+#{R3}=18<br />
Number of Times "remainder" function called <strong>18</strong>.</p>
<p>Applicative Order:<br />
(gcd 206 40)<br />
(gcd 40 (remainder 206 40))<br />
(gcd 40 6)<br />
(gcd 6 (remainder 40 6))<br />
(gcd 6 4)<br />
(gcd 4 (remainder 6 4))<br />
(gcd 4 2)<br />
(gcd 2 (remainder 4 2))<br />
(gcd 2 0)<br />
2<br />
Number of Times "remainder" function called <strong>4</strong>.</p>
]]></content:encoded>
			<wfw:commentRss>http://danboykis.com/2009/06/exercise-120-of-sicp/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Exercise 1.19 of SICP</title>
		<link>http://danboykis.com/2009/06/exercise-119-of-sicp/</link>
		<comments>http://danboykis.com/2009/06/exercise-119-of-sicp/#comments</comments>
		<pubDate>Fri, 12 Jun 2009 02:53:40 +0000</pubDate>
		<dc:creator>dan</dc:creator>
				<category><![CDATA[algorithms]]></category>
		<category><![CDATA[lisp]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[SICP]]></category>

		<guid isPermaLink="false">http://danboykis.com/?p=572</guid>
		<description><![CDATA[Exercise 1.19: There is a clever algorithm for computing the Fibonacci numbers in a logarithmic number of steps. Recall the transformation of the state variables a and b in the fib-iter process of section 1.2.2: a]]></description>
			<content:encoded><![CDATA[<p><strong>Exercise 1.19:</strong>   There is a clever algorithm for computing the Fibonacci numbers in a logarithmic number of steps. Recall the transformation of the state variables a and b in the <strong>fib-iter</strong> process of section 1.2.2: a <- a + b and b <- a. Call this transformation T, and observe that applying T over and over again n times, starting with 1 and 0, produces the pair Fib(n + 1) and Fib(n). In other words, the Fibonacci numbers are produced by applying T<sup>n</sup>, the n<sup>th</sup> power of the transformation T, starting with the pair (1,0). Now consider T<sub>pq</sub> to be the special case of p = 0 and q = 1 in a family of transformations T<sub>pq</sub> , where T<sub>pq</sub> transforms the pair (a,b) according to a <- bq + aq + ap and b <- bp + aq. Show that if we apply such a transformation T<sub>pq</sub> twice, the effect is the same as using a single transformation T<sub>p'q'</sub> of the same form, and compute p' and q' in terms of p and q. This gives us an explicit way to square these transformations, and thus we can compute T<sup>n</sup> using successive squaring, as in the <strong>fast-expt</strong> procedure. Put this all together to complete the following procedure, which runs in a logarithmic number of steps:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>define <span style="color: #66cc66;">&#40;</span>fib n<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>fib-iter <span style="color: #cc66cc;">1</span> <span style="color: #cc66cc;">0</span> <span style="color: #cc66cc;">0</span> <span style="color: #cc66cc;">1</span> n<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>define <span style="color: #66cc66;">&#40;</span>fib-iter a b p q count<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cond</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">=</span> count <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> b<span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>even? count<span style="color: #66cc66;">&#41;</span>
         <span style="color: #66cc66;">&#40;</span>fib-iter a
                   b
                   <span style="color: #66cc66;">&lt;</span>??<span style="color: #66cc66;">&gt;</span>      <span style="color: #808080; font-style: italic;">; compute p'</span>
                   <span style="color: #66cc66;">&lt;</span>??<span style="color: #66cc66;">&gt;</span>      <span style="color: #808080; font-style: italic;">; compute q'</span>
                   <span style="color: #66cc66;">&#40;</span>/ count <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span>else <span style="color: #66cc66;">&#40;</span>fib-iter <span style="color: #66cc66;">&#40;</span>+ <span style="color: #66cc66;">&#40;</span>* b q<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>* a q<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>* a p<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
                        <span style="color: #66cc66;">&#40;</span>+ <span style="color: #66cc66;">&#40;</span>* b p<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>* a q<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
                        p
                        q
                        <span style="color: #66cc66;">&#40;</span>- count <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>Definition of a<sub>1</sub> and b<sub>1</sub>:<br />
<img src='http://danboykis.com/wp-content/latex/dcc/dcca3a87e9a2c03d15786070a612c087-ffffff-000000-0.png' alt='a_1 \leftarrow bq + aq + ap' title='a_1 \leftarrow bq + aq + ap' class='latex' /><br />
<img src='http://danboykis.com/wp-content/latex/0d1/0d1aed12e925d9d80b7b84389b9aa685-ffffff-000000-0.png' alt='b_1 \leftarrow bp + aq' title='b_1 \leftarrow bp + aq' class='latex' /><br />
<img src='http://danboykis.com/wp-content/latex/463/463dad0081049d8281f40d110fd22876-ffffff-000000-0.png' alt='T^2_{pq} = T_{p&#039;q&#039;}' title='T^2_{pq} = T_{p&#039;q&#039;}' class='latex' /><br />
<img src='http://danboykis.com/wp-content/latex/15a/15ab942a71e722b370146504bfda047a-ffffff-000000-0.png' alt='a_1 = bq + aq + ap' title='a_1 = bq + aq + ap' class='latex' /><br />
<img src='http://danboykis.com/wp-content/latex/73b/73be238bf8a34b5d24693ae3a2912763-ffffff-000000-0.png' alt='b_1 = bp + aq' title='b_1 = bp + aq' class='latex' /><br />
<img src='http://danboykis.com/wp-content/latex/357/3578495982e03d6c584bd50c58f0d836-ffffff-000000-0.png' alt='a_2 = b_1q + a_1q + a_1p' title='a_2 = b_1q + a_1q + a_1p' class='latex' /><br />
<img src='http://danboykis.com/wp-content/latex/eeb/eeb31d67cce0dcf07e0e94cecf44d142-ffffff-000000-0.png' alt='b_2 = b_1p + a_1q' title='b_2 = b_1p + a_1q' class='latex' /><br />
The key here is to substitute from the definition of a<sub>1</sub> and b<sub>1</sub> into a<sub>2</sub> and b<sub>2</sub> in order to get p' and q'<br />
I start with b<sub>2</sub> because the expression is simpler to work with. the values I get for p' and q' must also work with a<sub>2</sub><br />
<img src='http://danboykis.com/wp-content/latex/497/497a4b2e30d65fba889f9a0d223a0922-ffffff-000000-0.png' alt='b_2 = b_1p + a_1q = (bp+aq)p + (bq+aq+ap)q' title='b_2 = b_1p + a_1q = (bp+aq)p + (bq+aq+ap)q' class='latex' /><br />
<img src='http://danboykis.com/wp-content/latex/401/401d76acea4c4f0d54b3d715c45bf595-ffffff-000000-0.png' alt='b_2 =  bp^2+aqp + bq^2+aq^2+aqp' title='b_2 =  bp^2+aqp + bq^2+aq^2+aqp' class='latex' /><br />
<img src='http://danboykis.com/wp-content/latex/8d8/8d897afcde585459e6c2b84b1d20ebe8-ffffff-000000-0.png' alt='b_2 =  b(p^2+ q^2)+a(2qp+q^2)' title='b_2 =  b(p^2+ q^2)+a(2qp+q^2)' class='latex' /><br />
<img src='http://danboykis.com/wp-content/latex/5d1/5d14325ba2a69921b70ffebb0e8d518f-ffffff-000000-0.png' alt='p&#039;=(p^2+ q^2)' title='p&#039;=(p^2+ q^2)' class='latex' /><br />
<img src='http://danboykis.com/wp-content/latex/76d/76db2b7a8c3645f09b06561b6bec45d3-ffffff-000000-0.png' alt='q&#039;= (2qp+q^2)' title='q&#039;= (2qp+q^2)' class='latex' /><br />
<img src='http://danboykis.com/wp-content/latex/800/8004ac94141f345a2fb748e427aeb750-ffffff-000000-0.png' alt='b_2 = bp&#039;+aq&#039;' title='b_2 = bp&#039;+aq&#039;' class='latex' /><br />
a<sub>2</sub> is trickier to check because of the number of terms, I'll take a few short cuts here in showing the math<br />
<img src='http://danboykis.com/wp-content/latex/440/440cdd48bb16a9c7c526f352a371b096-ffffff-000000-0.png' alt='a_2 = bpq + aq^2 + bq^2 + aq^2 + aqp + bqp + aqp + ap^2' title='a_2 = bpq + aq^2 + bq^2 + aq^2 + aqp + bqp + aqp + ap^2' class='latex' /><br />
<img src='http://danboykis.com/wp-content/latex/ec4/ec4f18091d90b077c336ffeab732e310-ffffff-000000-0.png' alt='a_2 = b(2qp+q^2) + a(2qp+q^2) + a(p^2+ q^2)' title='a_2 = b(2qp+q^2) + a(2qp+q^2) + a(p^2+ q^2)' class='latex' /><br />
<img src='http://danboykis.com/wp-content/latex/2e5/2e5abb5cac265975758689c950d31aeb-ffffff-000000-0.png' alt='a_2 = bq&#039;+aq&#039;+ap&#039;' title='a_2 = bq&#039;+aq&#039;+ap&#039;' class='latex' /><br />
Finally: <img src='http://danboykis.com/wp-content/latex/463/463dad0081049d8281f40d110fd22876-ffffff-000000-0.png' alt='T^2_{pq} = T_{p&#039;q&#039;}' title='T^2_{pq} = T_{p&#039;q&#039;}' class='latex' /> where <img src='http://danboykis.com/wp-content/latex/a96/a9672cb789da1b0ee4aa17016f747395-ffffff-000000-0.png' alt='p&#039;=p^2+ q^2,q&#039;=2qp+q^2' title='p&#039;=p^2+ q^2,q&#039;=2qp+q^2' class='latex' /></p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>define <span style="color: #66cc66;">&#40;</span>square x<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>* x x<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>define <span style="color: #66cc66;">&#40;</span>fib n<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>fib-iter <span style="color: #cc66cc;">1</span> <span style="color: #cc66cc;">0</span> <span style="color: #cc66cc;">0</span> <span style="color: #cc66cc;">1</span> n<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>define <span style="color: #66cc66;">&#40;</span>fib-iter a b p q count<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cond</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">=</span> count <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> b<span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>even? count<span style="color: #66cc66;">&#41;</span>
         <span style="color: #66cc66;">&#40;</span>fib-iter a
                   b
                   <span style="color: #66cc66;">&#40;</span>+ <span style="color: #66cc66;">&#40;</span>square p<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>square q<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>      <span style="color: #808080; font-style: italic;">; compute p'</span>
                   <span style="color: #66cc66;">&#40;</span>+ <span style="color: #66cc66;">&#40;</span>* <span style="color: #cc66cc;">2</span> p q<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>square q<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>       <span style="color: #808080; font-style: italic;">; compute q'</span>
                   <span style="color: #66cc66;">&#40;</span>/ count <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span>else <span style="color: #66cc66;">&#40;</span>fib-iter <span style="color: #66cc66;">&#40;</span>+ <span style="color: #66cc66;">&#40;</span>* b q<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>* a q<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>* a p<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
                        <span style="color: #66cc66;">&#40;</span>+ <span style="color: #66cc66;">&#40;</span>* b p<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>* a q<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
                        p
                        q
                        <span style="color: #66cc66;">&#40;</span>- count <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>> (fib 10)<br />
<em>55</em><br />
> (fib 100)<br />
<em>354224848179261915075</em></p>
]]></content:encoded>
			<wfw:commentRss>http://danboykis.com/2009/06/exercise-119-of-sicp/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Exercise 1.18 of SICP</title>
		<link>http://danboykis.com/2009/06/exercise-118-of-sicp/</link>
		<comments>http://danboykis.com/2009/06/exercise-118-of-sicp/#comments</comments>
		<pubDate>Wed, 10 Jun 2009 09:06:03 +0000</pubDate>
		<dc:creator>dan</dc:creator>
				<category><![CDATA[algorithms]]></category>
		<category><![CDATA[lisp]]></category>
		<category><![CDATA[SICP]]></category>

		<guid isPermaLink="false">http://danboykis.com/?p=541</guid>
		<description><![CDATA[Exercise 1.18: Using the results of exercises 1.16 and 1.17, devise a procedure that generates an iterative process for multiplying two integers in terms of adding, doubling, and halving and uses a logarithmic number of steps. &#40;define &#40;double x&#41; &#40;+ x x&#41;&#41; &#40;define &#40;halve x&#41; &#40;/ x 2&#41;&#41; &#40;define &#40;fast-mult-iter a b n&#41; &#40;cond &#40;&#40;= [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Exercise 1.18:</strong> Using the results of exercises 1.16 and 1.17, devise a procedure that generates an iterative process for multiplying two integers in terms of adding, doubling, and halving and uses a logarithmic number of steps.</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>define <span style="color: #66cc66;">&#40;</span>double x<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>+ x x<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>define <span style="color: #66cc66;">&#40;</span>halve x<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>/ x <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>define <span style="color: #66cc66;">&#40;</span>fast-mult-iter a b n<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cond</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">=</span> b <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> n<span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>even? b<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>fast-mult-iter <span style="color: #66cc66;">&#40;</span>double a<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>halve b<span style="color: #66cc66;">&#41;</span> n<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span>else
          <span style="color: #66cc66;">&#40;</span>fast-mult-iter a <span style="color: #66cc66;">&#40;</span>- b <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>+ a n<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://danboykis.com/2009/06/exercise-118-of-sicp/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Exercise 1.17 of SICP</title>
		<link>http://danboykis.com/2009/06/exercise-117-of-sicp/</link>
		<comments>http://danboykis.com/2009/06/exercise-117-of-sicp/#comments</comments>
		<pubDate>Tue, 09 Jun 2009 00:38:53 +0000</pubDate>
		<dc:creator>dan</dc:creator>
				<category><![CDATA[algorithms]]></category>
		<category><![CDATA[lisp]]></category>
		<category><![CDATA[SICP]]></category>

		<guid isPermaLink="false">http://danboykis.com/?p=535</guid>
		<description><![CDATA[Exercise 1.17: The exponentiation algorithms in this section are based on performing exponentiation by means of repeated multiplication. In a similar way, one can perform integer multiplication by means of repeated addition. The following multiplication procedure (in which it is assumed that our language can only add, not multiply) is analogous to the expt procedure: [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Exercise 1.17:</strong>  The exponentiation algorithms in this section are based on performing exponentiation by means of repeated multiplication. In a similar way, one can perform integer multiplication by means of repeated addition. The following multiplication procedure (in which it is assumed that our language can only add, not multiply) is analogous to the expt procedure:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>define <span style="color: #66cc66;">&#40;</span>mult a b<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">=</span> b <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #cc66cc;">0</span>
      <span style="color: #66cc66;">&#40;</span>+ a <span style="color: #66cc66;">&#40;</span>mult a <span style="color: #66cc66;">&#40;</span>- b <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>This algorithm takes a number of steps that is linear in b. Now suppose we include, together with addition, operations <strong>double</strong>, which doubles an integer, and <strong>halve</strong>, which divides an (even) integer by 2. Using these, design a multiplication procedure analogous to fast-expt that uses a logarithmic number of steps.</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>define <span style="color: #66cc66;">&#40;</span>double x<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>+ x x<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>define <span style="color: #66cc66;">&#40;</span>halve x<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>/ x <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>define <span style="color: #66cc66;">&#40;</span>fast-mult a b<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cond</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">=</span> b <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>even? b<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>double <span style="color: #66cc66;">&#40;</span>fast-mult a <span style="color: #66cc66;">&#40;</span>halve b<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span>else
          <span style="color: #66cc66;">&#40;</span>+ a <span style="color: #66cc66;">&#40;</span>fast-mult a <span style="color: #66cc66;">&#40;</span>- b <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>The difference in between <img src='http://danboykis.com/wp-content/latex/244/2449b2b12012224dbe9f384565e9d9a6-ffffff-000000-0.png' alt='\Theta(n)' title='\Theta(n)' class='latex' /> and <img src='http://danboykis.com/wp-content/latex/1fa/1fae9d1c2c4abfb6cba448b668abc2ad-ffffff-000000-0.png' alt='\Theta(log(n))' title='\Theta(log(n))' class='latex' /> is apparent for large numbers.<br />
On my system:<br />
> (mult 2 (expt 20 8))<br />
Takes forever, I had to interrupt it.<br />
> (fast-mult 2 (expt 20 8))<br />
<em>51200000000</em></p>
<p>The answer is almost instantaneous. </p>
]]></content:encoded>
			<wfw:commentRss>http://danboykis.com/2009/06/exercise-117-of-sicp/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

