<?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/"
	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>Programming Praxis</title>
	<atom:link href="http://programmingpraxis.com/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>Tue, 18 Jun 2013 17:51:57 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='programmingpraxis.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Programming Praxis</title>
		<link>http://programmingpraxis.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://programmingpraxis.com/osd.xml" title="Programming Praxis" />
	<atom:link rel='hub' href='http://programmingpraxis.com/?pushpress=hub'/>
		<item>
		<title>3SUM</title>
		<link>http://programmingpraxis.com/2013/06/18/3sum/</link>
		<comments>http://programmingpraxis.com/2013/06/18/3sum/#comments</comments>
		<pubDate>Tue, 18 Jun 2013 09:00:02 +0000</pubDate>
		<dc:creator>programmingpraxis</dc:creator>
				<category><![CDATA[Exercises]]></category>

		<guid isPermaLink="false">http://programmingpraxis.com/?p=7202</guid>
		<description><![CDATA[Today&#8217;s exercise is a classic problem of computer science: given an array of positive and negative integers, find three that sum to zero, or indicate that no such triple exists. This is similar to the subset sum problem, which we solved in a previous exercise, but simpler because of the limit to three items. A [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=programmingpraxis.com&#038;blog=6649073&#038;post=7202&#038;subd=programmingpraxis&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Today&#8217;s exercise is a classic problem of computer science: given an array of positive and negative integers, find three that sum to zero, or indicate that no such triple exists. This is similar to the subset sum problem, which we solved in a previous exercise, but simpler because of the limit to three items. A brute force solution that operates in O(<em>n</em><sup><small>3</small></sup>) time uses three nested loops to select items from the array and test their sum, but an O(<em>n</em><sup><small>2</small></sup>) solution exists. For instance, in the array [8, -25, 4, 10, -10, -7, 2, -3], the numbers -10, 2 and 8 sum to zero, as do the numbers -7, -3, and 10.</p>
<p>Your task is to write a program that solves the 3SUM problem in O(<em>n</em><sup><small>2</small></sup>) time. When you are finished, you are welcome to <a href="/2013/06/18/3sum/2/">read</a> or <a href="http://programmingpraxis.codepad.org/zW0EmC9m">run</a> a suggested solution, or to post your own solution or discuss the exercise in the comments below.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/programmingpraxis.wordpress.com/7202/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/programmingpraxis.wordpress.com/7202/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=programmingpraxis.com&#038;blog=6649073&#038;post=7202&#038;subd=programmingpraxis&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://programmingpraxis.com/2013/06/18/3sum/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/a4b14d4a7503a55cb803c049700ed83b?s=96&#38;d=http%3A%2F%2Fs0.wp.com%2Fi%2Fmu.gif" medium="image">
			<media:title type="html">programmingpraxis</media:title>
		</media:content>
	</item>
		<item>
		<title>The Digits of Pi, Again</title>
		<link>http://programmingpraxis.com/2013/06/14/the-digits-of-pi-again/</link>
		<comments>http://programmingpraxis.com/2013/06/14/the-digits-of-pi-again/#comments</comments>
		<pubDate>Fri, 14 Jun 2013 09:00:48 +0000</pubDate>
		<dc:creator>programmingpraxis</dc:creator>
				<category><![CDATA[Exercises]]></category>

		<guid isPermaLink="false">http://programmingpraxis.com/?p=7194</guid>
		<description><![CDATA[The ratio of the circumference to the diameter of a circle, known as &#960;, is constant regardless of the size of the circle; that fact has been known to mathematicians for about five thousand years; much of the history of mathematics is intertwined in the history of &#960;i, as approximations of the ratio have improved [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=programmingpraxis.com&#038;blog=6649073&#038;post=7194&#038;subd=programmingpraxis&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>The ratio of the circumference to the diameter of a circle, known as &pi;, is constant regardless of the size of the circle; that fact has been known to mathematicians for about five thousand years; much of the history of mathematics is intertwined in the <a href="http://www.google.com/search?q=history+of+pi">history of &pi;i</a>, as approximations of the ratio have improved over time. Much of the history of this blog is also intertwined in the calculation of &pi;; one of the original ten exercises used a <a href="/2009/02/20/the-digits-of-pi/">bounded spigot algorithm</a> of Jeremy Gibbons to calculate &pi;, and later we used an <a href="/2012/06/19/digits-of-e/">unbounded spigot algorithm</a> also due to Gibbons; we studied the ancient approximation of 355/113 calculated by <a href="/2009/10/09/calculating-pi/">Archimedes</a>; we studied <a href="/2009/10/09/calculating-pi/">two</a> <a href="/2013/03/15/buffons-needle/">different</a> Monte Carlo simulations of &pi;; and we even had the <a href="/2012/01/24/a-dozen-lines-of-code/">Brent-Salamin approximation</a> contributed by a reader in a comment.</p>
<p>The development of computers allows us to compute the digits of &pi; to an astonishing accuracy; the current record, unless somebody has bettered it recently, is ten trillion digits. That record was set by the Chudnovsky brothers, two Russian mathematicians living in New York, using an algorithm they developed in 1987. The algorithm is based on a definition of &pi; developed by Ramanujan, and is beautifully <a href="http://www.pnas.org/content/86/21/8178.full.pdf+html">described</a> by the two brothers.</p>
<p>Your task is to compute many digits of &pi; using the Chudnovsky algorithm. When you are finished, you are welcome to <a href="/2013/06/14/the-digits-of-pi-again/2/">read</a> or <a href="http://programmingpraxis.codepad.org/XKfVY5Ua">run</a> a suggested solution, or to post your own solution or discuss the exercise in the comments below.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/programmingpraxis.wordpress.com/7194/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/programmingpraxis.wordpress.com/7194/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=programmingpraxis.com&#038;blog=6649073&#038;post=7194&#038;subd=programmingpraxis&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://programmingpraxis.com/2013/06/14/the-digits-of-pi-again/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/a4b14d4a7503a55cb803c049700ed83b?s=96&#38;d=http%3A%2F%2Fs0.wp.com%2Fi%2Fmu.gif" medium="image">
			<media:title type="html">programmingpraxis</media:title>
		</media:content>
	</item>
		<item>
		<title>Longest Substring Of Two Unique Characters</title>
		<link>http://programmingpraxis.com/2013/06/11/longest-substring-of-two-unique-characters/</link>
		<comments>http://programmingpraxis.com/2013/06/11/longest-substring-of-two-unique-characters/#comments</comments>
		<pubDate>Tue, 11 Jun 2013 09:00:53 +0000</pubDate>
		<dc:creator>programmingpraxis</dc:creator>
				<category><![CDATA[Exercises]]></category>

		<guid isPermaLink="false">http://programmingpraxis.com/?p=7191</guid>
		<description><![CDATA[Today&#8217;s exercise comes from our unending list of interview questions: Find the longest run of consecutive characters in a string that contains only two unique characters; if there is a tie, return the rightmost. For instance, given input string abcabcabcbcbc, the longest run of two characters is the 6-character run of bcbcbc that ends the [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=programmingpraxis.com&#038;blog=6649073&#038;post=7191&#038;subd=programmingpraxis&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Today&#8217;s exercise comes from our unending list of interview questions:</p>
<blockquote><p>Find the longest run of consecutive characters in a string that contains only two unique characters; if there is a tie, return the rightmost. For instance, given input string <code>abcabcabcbcbc</code>, the longest run of two characters is the 6-character run of <code>bcbcbc</code> that ends the string.</p></blockquote>
<p>Your task is to write the requested program. When you are finished, you are welcome to <a href="/2013/06/11/longest-substring-of-two-unique-characters/2/">read</a> or <a href="http://programmingpraxis.codepad.org/ybpvp6gv">run</a> a suggested solution, or to post your own solution or discuss the exercise in the comments below.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/programmingpraxis.wordpress.com/7191/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/programmingpraxis.wordpress.com/7191/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=programmingpraxis.com&#038;blog=6649073&#038;post=7191&#038;subd=programmingpraxis&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://programmingpraxis.com/2013/06/11/longest-substring-of-two-unique-characters/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/a4b14d4a7503a55cb803c049700ed83b?s=96&#38;d=http%3A%2F%2Fs0.wp.com%2Fi%2Fmu.gif" medium="image">
			<media:title type="html">programmingpraxis</media:title>
		</media:content>
	</item>
		<item>
		<title>Sets</title>
		<link>http://programmingpraxis.com/2013/06/07/sets/</link>
		<comments>http://programmingpraxis.com/2013/06/07/sets/#comments</comments>
		<pubDate>Fri, 07 Jun 2013 09:00:27 +0000</pubDate>
		<dc:creator>programmingpraxis</dc:creator>
				<category><![CDATA[Exercises]]></category>

		<guid isPermaLink="false">http://programmingpraxis.com/?p=7180</guid>
		<description><![CDATA[Sets are ubiquitous in programming; we&#8217;ve used sets in several of our exercises. In most cases we made the sets from lists, which is good enough when the sets are small but quickly slows down when the sets get large. In today&#8217;s exercise we will write a generic library for sets. The sets that we [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=programmingpraxis.com&#038;blog=6649073&#038;post=7180&#038;subd=programmingpraxis&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Sets are ubiquitous in programming; we&#8217;ve used sets in several of our exercises. In most cases we made the sets from lists, which is good enough when the sets are small but quickly slows down when the sets get large. In today&#8217;s exercise we will write a generic library for sets.</p>
<p>The sets that we will consider are collections of items without duplicates. We will provide an <em>adjoin</em> function to add an item to a set if it is not already present and a <em>delete</em> function to remove an item from a set if it is present. A <em>member</em> function determines if a particular item is present in a set. The three set operations that are provided are <em>intersection</em>, <em>union</em> and <em>difference</em>; we will consider that the universe from which items are drawn is infinite, or at least too large to conveniently enumerate, so we will not provide a <em>complement</em> operation. For convenience, we will also provide functions to calculate the <em>cardinality</em> of a set and to create a <em>list</em> from the items present in the set.</p>
<p>Your task is to write a library for handling sets, based on the description given above. When you are finished, you are welcome to <a href="/2013/06/07/sets/2/">read</a> or <a href="http://programmingpraxis.codepad.org/i82KOZRE">run</a> a suggested solution, or to post your own solution or discuss the exercise in the comments below.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/programmingpraxis.wordpress.com/7180/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/programmingpraxis.wordpress.com/7180/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=programmingpraxis.com&#038;blog=6649073&#038;post=7180&#038;subd=programmingpraxis&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://programmingpraxis.com/2013/06/07/sets/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/a4b14d4a7503a55cb803c049700ed83b?s=96&#38;d=http%3A%2F%2Fs0.wp.com%2Fi%2Fmu.gif" medium="image">
			<media:title type="html">programmingpraxis</media:title>
		</media:content>
	</item>
		<item>
		<title>Egyptian Fractions</title>
		<link>http://programmingpraxis.com/2013/06/04/egyptian-fractions/</link>
		<comments>http://programmingpraxis.com/2013/06/04/egyptian-fractions/#comments</comments>
		<pubDate>Tue, 04 Jun 2013 09:00:41 +0000</pubDate>
		<dc:creator>programmingpraxis</dc:creator>
				<category><![CDATA[Exercises]]></category>

		<guid isPermaLink="false">http://programmingpraxis.com/?p=7174</guid>
		<description><![CDATA[One topic that has always fascinated me is the mathematical sophistication of the ancients. The Pythagorean theorem is twenty-five hundred years old. Euclid&#8217;s Elements of 300BC remained in use as a geometry textbook for over two millenia, and is still in print today (you can buy the Dover edition for $1). Eratosthenes was sieving prime [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=programmingpraxis.com&#038;blog=6649073&#038;post=7174&#038;subd=programmingpraxis&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>One topic that has always fascinated me is the mathematical sophistication of the ancients. The Pythagorean theorem is twenty-five hundred years old. Euclid&#8217;s <em>Elements</em> of 300BC remained in use as a geometry textbook for over two millenia, and is still in print today (you can buy the Dover edition for $1). Eratosthenes was sieving prime numbers two centuries before Christ.</p>
<p>Today&#8217;s topic is Egyptian fractions, which were mentioned in the Rhind Papyrus of 500BC as an &#8220;ancient method&#8221; and are thought to date to the time of the pyramids; they survived in use until the 17th or 18th century. Leonardo de Pisa&#8217;s famous book <em>Liber Abaci</em> of 1215AD, which introduced to European mathematicians the concept of zero, the Hindu-Arabic system of numeration, and the famous rabbit sequence (Leonardo&#8217;s nickname was Fibonacci).</p>
<p>An Egyptian fraction was written as a sum of unit fractions, meaning the numerator is always 1; further, no two denominators can be the same. As easy way to create an Egyptian fraction is to repeatedly take the largest unit fraction that will fit, subtract to find what remains, and repeat until the remainder is a unit fraction. For instance, 7 divided by 15 is less than 1/2 but more than 1/3, so the first unit fraction is 1/3 and the first remainder is 2/15. Then 2/15 is less than 1/7 but more than 1/8, so the second unit fraction is 1/8 and the second remainder is 1/120. That&#8217;s in unit form, so we are finished: 7 &divide; 15 = 1/3 + 1/8 + 1/120. There are other algorithms for finding Egyptian fractions, but there is no algorithm that guarantees a maximum number of terms or a minimum largest denominator; for instance, the greedy algorithm leads to 5 &divide; 121 = 1/25 + 1/757 + 1/763309 + 1/873960180913 + 1/1527612795642093418846225, but a simpler rendering of the same number is 1/33 + 1/121 + 1/363.</p>
<p>Your task is to write a program that calculates the ratio of two numbers as an Egyptian fraction. When you are finished, you are welcome to <a href="/2013/06/04/egyptian-fractions/2/">read</a> or <a href="http://programmingpraxis.codepad.org/eopIofz7">run</a> a suggested solution, or to post your own solution or discuss the exercise in the comments below.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/programmingpraxis.wordpress.com/7174/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/programmingpraxis.wordpress.com/7174/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=programmingpraxis.com&#038;blog=6649073&#038;post=7174&#038;subd=programmingpraxis&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://programmingpraxis.com/2013/06/04/egyptian-fractions/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/a4b14d4a7503a55cb803c049700ed83b?s=96&#38;d=http%3A%2F%2Fs0.wp.com%2Fi%2Fmu.gif" medium="image">
			<media:title type="html">programmingpraxis</media:title>
		</media:content>
	</item>
		<item>
		<title>The Seven Bridges of K&#246;nigsberg</title>
		<link>http://programmingpraxis.com/2013/05/31/the-seven-bridges-of-knigsberg/</link>
		<comments>http://programmingpraxis.com/2013/05/31/the-seven-bridges-of-knigsberg/#comments</comments>
		<pubDate>Fri, 31 May 2013 09:00:01 +0000</pubDate>
		<dc:creator>programmingpraxis</dc:creator>
				<category><![CDATA[Exercises]]></category>

		<guid isPermaLink="false">http://programmingpraxis.com/?p=7160</guid>
		<description><![CDATA[Our exercise today studies a famous problem solved by the great Swiss mathematician Leonhard Euler in 1735, which marked the beginning of what is now the &#8220;graph theory&#8221; branch of mathematics. Euler proved that it was impossible to cross all seven bridges once without retracing your path. He proved that a complete circuit, returning to [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=programmingpraxis.com&#038;blog=6649073&#038;post=7160&#038;subd=programmingpraxis&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p><img src="http://programmingpraxis.files.wordpress.com/2013/05/koenigsberg.png?w=460" align="right" width="30%" />Our exercise today studies a famous problem solved by the great Swiss mathematician Leonhard Euler in 1735, which marked the beginning of what is now the &#8220;graph theory&#8221; branch of mathematics. Euler proved that it was impossible to cross all seven bridges once without retracing your path. He proved that a complete circuit, returning to the starting point, is possible only if all vertices connect to an even number of neighbors, and a complete path that doesn&#8217;t return to the starting point is possible if exactly two of the vertices have an odd number of neighbors, the rest being even, in which case the two odd-count vertices are the starting and ending points of the path.</p>
<p>The following algorithm to determine a eulerian path in a graph, if it exists, is ancient; I would be happy if anybody can provide a pointer to the source of the algorithm:</p>
<blockquote><p>1) If a complete circuit is possible, choose any vertex at random as the current vertex. If a complete path but not a circuit is possible, choose either of the two odd-degree vertices at random. Otherwise, quit.</p>
<p>2a) If the current vertex has neighbors, add it to the stack, choose any of its neighbors as the new current vertex, and remove the edge between the two vertices.</p>
<p>2b) If the current vertex has no neighbors, add it to the path, remove the last vertex from the stack and make it the current vertex.</p>
<p>3)  Repeat step 2 until the current vertex has no neighbors and the stack is empty.</p>
<p>4)  Add the current vertex to the path and quit.</p></blockquote>
<p>Your task is to write programs that determine if a graph is an eulerian circuit or eulerian path and, if it is, determine the path. When you are finished, you are welcome to <a href="/2013/05/31/the-seven-bridges-of-knigsberg/2/">read</a> or <a href="http://programmingpraxis.codepad.org/TE084ryW">run</a> a suggested solution, or to post your own solution or discuss the exercise in the comments below.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/programmingpraxis.wordpress.com/7160/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/programmingpraxis.wordpress.com/7160/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=programmingpraxis.com&#038;blog=6649073&#038;post=7160&#038;subd=programmingpraxis&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://programmingpraxis.com/2013/05/31/the-seven-bridges-of-knigsberg/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/a4b14d4a7503a55cb803c049700ed83b?s=96&#38;d=http%3A%2F%2Fs0.wp.com%2Fi%2Fmu.gif" medium="image">
			<media:title type="html">programmingpraxis</media:title>
		</media:content>

		<media:content url="http://programmingpraxis.files.wordpress.com/2013/05/koenigsberg.png" medium="image" />
	</item>
		<item>
		<title>Modular Multiplication Without Overflow</title>
		<link>http://programmingpraxis.com/2013/05/28/modular-multiplication-without-overflow/</link>
		<comments>http://programmingpraxis.com/2013/05/28/modular-multiplication-without-overflow/#comments</comments>
		<pubDate>Tue, 28 May 2013 09:00:00 +0000</pubDate>
		<dc:creator>programmingpraxis</dc:creator>
				<category><![CDATA[Exercises]]></category>

		<guid isPermaLink="false">http://programmingpraxis.com/?p=7155</guid>
		<description><![CDATA[We discussed modular arithmetic in a previous exercise. In a comment on that exercise, Matthew asked what you can do when you have a fixed size integer datatype (say 64-bit unsigned integers) and a modulus that is close to the maximum. That&#8217;s a good question, which we will answer today. The answer is straight forward [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=programmingpraxis.com&#038;blog=6649073&#038;post=7155&#038;subd=programmingpraxis&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>We discussed modular arithmetic in a <a href="/2009/07/07/modular-arithmetic/">previous exercise</a>. In a comment on that exercise, Matthew asked what you can do when you have a fixed size integer datatype (say 64-bit unsigned integers) and a modulus that is close to the maximum. That&#8217;s a good question, which we will answer today. The answer is straight forward if the modulus is at least one bit less than the maximum, and rather trickier if it&#8217;s not. We&#8217;ll look at multiplication, but the other operations are all similar.</p>
<p>As long as the modulus is at least one bit less than the maximum, the solution is to split the numbers in low-bit and high-bit halves, then perform the arithmetic piecemeal, kind of like grade-school multiplication where you multiply by the one&#8217;s digit, then shift the sum and multiply by the ten&#8217;s digit, and so on, except that the &#8220;digits&#8221; are the size of the square root of the maximum number than can be represented in the integer datatype.</p>
<p>If <em>m</em> is within one bit of the maximum for the integer data type, things get messier; the basic answer is to split into three parts, compute the intermediate products, and recombine. But we won&#8217;t bother with that.</p>
<p>Your task is to write a function that performs modular multiplication without overflow. When you are finished, you are welcome to <a href="/2013/05/28/modular-multiplication-without-overflow/2/">read</a> or <a href="http://programmingpraxis.codepad.org/psbpa4MC">run</a> a suggested solution, or to post your own solution or discuss the exercise in the comments below.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/programmingpraxis.wordpress.com/7155/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/programmingpraxis.wordpress.com/7155/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=programmingpraxis.com&#038;blog=6649073&#038;post=7155&#038;subd=programmingpraxis&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://programmingpraxis.com/2013/05/28/modular-multiplication-without-overflow/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/a4b14d4a7503a55cb803c049700ed83b?s=96&#38;d=http%3A%2F%2Fs0.wp.com%2Fi%2Fmu.gif" medium="image">
			<media:title type="html">programmingpraxis</media:title>
		</media:content>
	</item>
		<item>
		<title>Coin Change, Part 3</title>
		<link>http://programmingpraxis.com/2013/05/24/coin-change-part-3/</link>
		<comments>http://programmingpraxis.com/2013/05/24/coin-change-part-3/#comments</comments>
		<pubDate>Fri, 24 May 2013 09:00:19 +0000</pubDate>
		<dc:creator>programmingpraxis</dc:creator>
				<category><![CDATA[Exercises]]></category>

		<guid isPermaLink="false">http://programmingpraxis.com/?p=7150</guid>
		<description><![CDATA[Our exercise today is similar to the two previous exercises on the coin change problem, but doesn&#8217;t directly involve coins. But we&#8217;ll still call it a coin change problem because I can&#8217;t think of a better name. As in the coin change problems, we are given a target to which a set of coins is [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=programmingpraxis.com&#038;blog=6649073&#038;post=7150&#038;subd=programmingpraxis&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Our exercise today is similar to the two <a href="/2013/05/17/coin-change-part-1/">previous</a> <a href="/2013/05/21/coin-change-part-2/">exercises</a> on the coin change problem, but doesn&#8217;t directly involve coins. But we&#8217;ll still call it a coin change problem because I can&#8217;t think of a better name.</p>
<p>As in the coin change problems, we are given a target to which a set of coins is intended to sum. But instead of a list of coins, we are given a number <em>n</em> and assume that we have one of each denomination of coin from 1 through <em>n</em>. Unlike the coin change problems, we have only one of each coin, not an infinite number of them.</p>
<p>The problem is to figure out all the ways to combine coins to reach the desired sum. For instance, if the sum <em>s</em> is 10 and <em>n</em> is 10, the ways to combine the coins are (10), (9 1), (8 2), (7 3), (7 2 1), (6 4), (6 3 1), (5 4 1), (5 3 2), and (4 3 2 1). Note that a set like (6 2 2) is not possible because only one 2-coin is available. As in the two previous exercises, we are interested in both a function to make a list of all the solutions and a separate function that simply counts them.</p>
<p>Your task is to write the two programs described above. When you are finished, you are welcome to <a href="/2013/05/24/coin-change-part-3/2/">read</a> or <a href="http://programmingpraxis.codepad.org/Y70sHPc0">run</a> a suggested solution, or to post your own solution or discuss the exercise in the comments below.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/programmingpraxis.wordpress.com/7150/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/programmingpraxis.wordpress.com/7150/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=programmingpraxis.com&#038;blog=6649073&#038;post=7150&#038;subd=programmingpraxis&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://programmingpraxis.com/2013/05/24/coin-change-part-3/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/a4b14d4a7503a55cb803c049700ed83b?s=96&#38;d=http%3A%2F%2Fs0.wp.com%2Fi%2Fmu.gif" medium="image">
			<media:title type="html">programmingpraxis</media:title>
		</media:content>
	</item>
		<item>
		<title>Coin Change, Part 2</title>
		<link>http://programmingpraxis.com/2013/05/21/coin-change-part-2/</link>
		<comments>http://programmingpraxis.com/2013/05/21/coin-change-part-2/#comments</comments>
		<pubDate>Tue, 21 May 2013 09:00:35 +0000</pubDate>
		<dc:creator>programmingpraxis</dc:creator>
				<category><![CDATA[Exercises]]></category>

		<guid isPermaLink="false">http://programmingpraxis.com/?p=7135</guid>
		<description><![CDATA[In the previous exercise we studied the classic coin change problem to find all the ways to make a given amount of change using a given set of coins. Sometimes the problem in a different way: find the minimum set of coins needed to make a given amount of change. As with the prior exercise, [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=programmingpraxis.com&#038;blog=6649073&#038;post=7135&#038;subd=programmingpraxis&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>In the <a href="/2013/05/17/coin-change-part-1">previous exercise</a> we studied the classic coin change problem to find all the ways to make a given amount of change using a given set of coins. Sometimes the problem in a different way: find the minimum set of coins needed to make a given amount of change. As with the prior exercise, sometimes the task is to find just the count and sometimes the task is to find the actual set of coins.</p>
<p>Your task is to write the two programs described above. When you are finished, you are welcome to <a href="/2013/05/21/coin-change-part-2/2/">read</a> or <a href="http://programmingpraxis.codepad.org/NeueOC2I">run</a> a suggested solution, or to post your own solution or discuss the exercise in the comments below.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/programmingpraxis.wordpress.com/7135/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/programmingpraxis.wordpress.com/7135/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=programmingpraxis.com&#038;blog=6649073&#038;post=7135&#038;subd=programmingpraxis&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://programmingpraxis.com/2013/05/21/coin-change-part-2/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/a4b14d4a7503a55cb803c049700ed83b?s=96&#38;d=http%3A%2F%2Fs0.wp.com%2Fi%2Fmu.gif" medium="image">
			<media:title type="html">programmingpraxis</media:title>
		</media:content>
	</item>
		<item>
		<title>Coin Change, Part 1</title>
		<link>http://programmingpraxis.com/2013/05/17/coin-change-part-1/</link>
		<comments>http://programmingpraxis.com/2013/05/17/coin-change-part-1/#comments</comments>
		<pubDate>Fri, 17 May 2013 09:00:15 +0000</pubDate>
		<dc:creator>programmingpraxis</dc:creator>
				<category><![CDATA[Exercises]]></category>

		<guid isPermaLink="false">http://programmingpraxis.com/?p=7131</guid>
		<description><![CDATA[It is a classic problem from computer science to determine the number of ways in which coins can be combined to form a particular target; as an example, there are 31 ways to form 40 cents from an unlimited supply of pennies (1 cent), nickels (5 cents), dimes (10 cents) and quarters (25 cents), ranging [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=programmingpraxis.com&#038;blog=6649073&#038;post=7131&#038;subd=programmingpraxis&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>It is a classic problem from computer science to determine the number of ways in which coins can be combined to form a particular target; as an example, there are 31 ways to form 40 cents from an unlimited supply of pennies (1 cent), nickels (5 cents), dimes (10 cents) and quarters (25 cents), ranging from the 3-coin set (5 10 25) to the 40-coin set consisting only of pennies.</p>
<p>The solution is usually stated in recursive form: if <em>c</em> is the first coin in the set of coins <em>cs</em> and <em>n</em> is the target, the solution is the number of ways to reach the target after removing <em>c</em> from <em>cs</em> plus the number of ways to reach <em>n</em> &minus; <em>c</em> using all the coins in <em>cs</em>. The algorithm to make a list of the coins, instead of the count, is the same, but keeping track of the list of coins instead of the count.</p>
<p>Your task is to write two functions, one to determine the count and one to determine the list of coins. When you are finished, you are welcome to <a href="/2013/05/17/coin-change-part-1/2/">read</a> or <a href="http://programmingpraxis.codepad.org/RekApsRy">run</a> a suggested solution, or to post your own solution or discuss the exercise in the comments below.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/programmingpraxis.wordpress.com/7131/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/programmingpraxis.wordpress.com/7131/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=programmingpraxis.com&#038;blog=6649073&#038;post=7131&#038;subd=programmingpraxis&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://programmingpraxis.com/2013/05/17/coin-change-part-1/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/a4b14d4a7503a55cb803c049700ed83b?s=96&#38;d=http%3A%2F%2Fs0.wp.com%2Fi%2Fmu.gif" medium="image">
			<media:title type="html">programmingpraxis</media:title>
		</media:content>
	</item>
	</channel>
</rss>
