<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Matthew van Eerde's web log : Math</title><link>http://blogs.msdn.com/matthew_van_eerde/archive/tags/Math/default.aspx</link><description>Tags: Math</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Rules for making change</title><link>http://blogs.msdn.com/matthew_van_eerde/archive/2009/11/25/rules-for-making-change.aspx</link><pubDate>Wed, 25 Nov 2009 18:40:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9928722</guid><dc:creator>Maurits</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/matthew_van_eerde/comments/9928722.aspx</comments><wfw:commentRss>http://blogs.msdn.com/matthew_van_eerde/commentrss.aspx?PostID=9928722</wfw:commentRss><description>
&lt;p&gt;It happens a million times a day.&amp;nbsp; Somebody pays cash for something and gets change.&amp;nbsp; But there are rules, and &lt;a href="http://blogs.msdn.com/oldnewthing/archive/2009/11/24/9927970.aspx" mce_href="http://blogs.msdn.com/oldnewthing/archive/2009/11/24/9927970.aspx"&gt;Raymond's friend seems to like to break those rules&lt;/a&gt;.&amp;nbsp; Luckily for Raymond's friend, one of the rules is &lt;i&gt;the customer is always right&lt;/i&gt;... or as Mr. Krabs would say, &lt;i&gt;the money is always right&lt;/i&gt;.&amp;nbsp; This blog post is about a couple of the more mathematical rules.&lt;br&gt;&lt;/p&gt;
&lt;b&gt;Rule for the cashier:&lt;/b&gt; &lt;i&gt;you must give "canonical" change.&lt;/i&gt;&lt;br&gt;
&lt;blockquote&gt;
&lt;p&gt;Whether to honor &lt;i&gt;requests&lt;/i&gt; for non-canonical change is a complicated morass I will not attempt to tackle in this blog post as too much depends on context.&amp;nbsp; But the initial offering must be canonical.&lt;/p&gt;

&lt;p&gt;By "canonical change", I mean the cashier must give change using the &lt;i&gt;smallest possible number of canonical coins and bills.&lt;/i&gt;&amp;nbsp; For example, (one quarter + one nickel) is a rendering of $0.30 using only two coins, while (three dimes) is three coins, so a cashier must never give change that includes three dimes.&lt;/p&gt;

&lt;p&gt;By "canonical coins and bills" I mean (in the U.S.) this precise list:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;penny&lt;/li&gt;

&lt;li&gt;nickel&lt;/li&gt;

&lt;li&gt;dime&lt;/li&gt;

&lt;li&gt;quarter&lt;/li&gt;

&lt;li&gt;$1 bill&lt;/li&gt;

&lt;li&gt;$5 bill&lt;/li&gt;

&lt;li&gt;$10 bill&lt;/li&gt;

&lt;li&gt;$20 bill&lt;/li&gt;
&lt;/ul&gt;
I discuss usage of non-canonical (unusual) coins and bills later. &lt;br&gt;&lt;/blockquote&gt;

&lt;p&gt;&lt;b&gt;Rule 1 for the customer:&lt;/b&gt; &lt;i&gt;"Tap, tap, no takebacks"&lt;/i&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;i&gt;The tender and canonical change cannot intersect.&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;&lt;i&gt;&lt;/i&gt;Don't give the cashier anything they would just hand back to you directly. &lt;br&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;This is the rule that Raymond's friend broke - it just wastes everyone's time to play musical chairs with the money.&lt;/blockquote&gt;

&lt;blockquote&gt;Actually, this rule is just a corollary of the more general rule: &lt;br&gt;&lt;/blockquote&gt;

&lt;p&gt;&lt;b&gt;Rule 2 for the customer: &lt;/b&gt;&lt;i&gt;"No side transactions."&lt;/i&gt; &lt;br&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;i&gt;No subset of the tender can have the same value as any subset of canonical change for that tender.&lt;/i&gt;&lt;br&gt;&lt;/p&gt;

&lt;p&gt;Don't give the cashier anything that they would just hand back to you in coalesced form.&lt;br&gt;&lt;/p&gt;

&lt;p&gt;If you want to consolidate your change, go to a bank or use one of those big machines.&lt;br&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;i&gt;Corollary 1: it is always OK to pay with a single large bill.&lt;br&gt;
Corollary 2: it is always OK to give exact change.&lt;/i&gt; &lt;br&gt;&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Unusual denominations:&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;The two-dollar bill, the dollar coin (in any of its forms), the half-dollar coin, and bills higher than $20 are "unusual" denominations that complicate the situation slightly.&lt;br&gt;&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Rule for the cashier: &lt;/b&gt;&lt;i&gt;Unusual denominations stay in the drawer.&lt;/i&gt;&lt;br&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Accept them (unless there are security rules at your place of business) but they are explicitly not part of any canonical change. &lt;br&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;b&gt;Rule for the customer:&lt;/b&gt; &lt;i&gt;Go right ahead.&lt;br&gt;&lt;/i&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;You can pay with an unusual denomination provided that you do not break the "no side transactions" rule or any large-denomination security rule at the business in question.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;b&gt;Examples:&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;In the following examples, "total" is the amount owed; "tender" is the amount the customer gives to the cashier; and "change" is what the cashier gives back to the customer.&lt;/p&gt;

&lt;table border="1"&gt;
&lt;tbody&gt;
&lt;tr style="font-weight: bold;"&gt;
	
&lt;td&gt;Total&lt;/td&gt;
	
&lt;td&gt;Tender&lt;/td&gt;
	
&lt;td&gt;Change&lt;/td&gt;
	
&lt;td&gt;Notes&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
	
&lt;td style=""&gt;$5.20&lt;/td&gt;
	
&lt;td&gt;$5 bill + &lt;span style="background-color: rgb(204, 255, 204);"&gt;$2 bill&lt;/span&gt;&lt;/td&gt;
	
&lt;td&gt;$1 bill + three quarters + one nickel&lt;/td&gt;
	
&lt;td&gt;OK - cashier correctly accepted the $2&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
	
&lt;td style=""&gt;$5.20&lt;/td&gt;
	
&lt;td&gt;
&lt;p&gt;$5 bill + $1 bill&lt;/p&gt;
&lt;/td&gt;
	
&lt;td&gt;two quarters + &lt;span style="background-color: rgb(255, 204, 204);"&gt;three dimes&lt;/span&gt;&lt;/td&gt;
	
&lt;td&gt;Cashier gave out more coins than necessary&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
	
&lt;td style=""&gt;$5.20&lt;/td&gt;
	
&lt;td&gt;$5 bill + $1 bill&lt;/td&gt;
	
&lt;td&gt;&lt;span style="background-color: rgb(255, 204, 204);"&gt;half-dollar&lt;/span&gt; + one quarter + one nickel&lt;/td&gt;
	
&lt;td&gt;Cashier gave out non-canonical half-dollar&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
	
&lt;td style=""&gt;$5.20&lt;/td&gt;
	
&lt;td&gt;$5 bill + &lt;span style="background-color: rgb(255, 204, 204);"&gt;four quarters&lt;/span&gt;&lt;/td&gt;
	
&lt;td&gt;&lt;span style="background-color: rgb(255, 204, 204);"&gt;three quarters&lt;/span&gt; + one nickel&lt;/td&gt;
	
&lt;td&gt;Customer gave three "takeback" quarters&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
	
&lt;td style=""&gt;$5.20&lt;/td&gt;
	
&lt;td&gt;$5 bill + quarter + nickel&lt;/td&gt;
	
&lt;td&gt;dime&lt;/td&gt;
	
&lt;td&gt;OK&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
	
&lt;td style=""&gt;$5.20&lt;/td&gt;
	
&lt;td&gt;$5 bill + quarter + five pennies&lt;/td&gt;
	
&lt;td&gt;dime&lt;/td&gt;
	
&lt;td&gt;OK&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
	
&lt;td style=""&gt;$5.20&lt;/td&gt;
	
&lt;td&gt;$5 bill + quarter + &lt;span style="background-color: rgb(255, 204, 204);"&gt;ten pennies&lt;/span&gt;&lt;/td&gt;
	
&lt;td&gt;dime + &lt;span style="background-color: rgb(255, 204, 204);"&gt;nickel&lt;/span&gt;&lt;/td&gt;
	
&lt;td&gt;Customer got nickel for five pennies in "side transaction"&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;

&lt;p&gt;In the last row, you can think of the "side transaction" as either five pennies for a nickel or ten pennies for a dime.  I prefer the former.&lt;/p&gt;
&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9928722" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/matthew_van_eerde/archive/tags/Math/default.aspx">Math</category><category domain="http://blogs.msdn.com/matthew_van_eerde/archive/tags/Philosophy/default.aspx">Philosophy</category></item><item><title>How to calculate a sine sweep - the right way</title><link>http://blogs.msdn.com/matthew_van_eerde/archive/2009/08/07/how-to-calculate-a-sine-sweep-the-right-way.aspx</link><pubDate>Sat, 08 Aug 2009 01:12:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9860732</guid><dc:creator>Maurits</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/matthew_van_eerde/comments/9860732.aspx</comments><wfw:commentRss>http://blogs.msdn.com/matthew_van_eerde/commentrss.aspx?PostID=9860732</wfw:commentRss><description>&lt;p&gt;Three quarters are better than a dollar because they make noise!&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; -- Lilly, &lt;i&gt;Lilly's Purple Plastic Purse&lt;/i&gt;&lt;br&gt;&lt;/p&gt;
&lt;p&gt;Last time I talked about &lt;a href="http://blogs.msdn.com/matthew_van_eerde/archive/2009/08/03/how-to-calculate-a-sine-sweep-the-wrong-way.aspx" mce_href="http://blogs.msdn.com/matthew_van_eerde/archive/2009/08/03/how-to-calculate-a-sine-sweep-the-wrong-way.aspx"&gt;how to calculate a sine sweep mathematically&lt;/a&gt;.&amp;nbsp; There's a closed-form solution which is quite elegant but, alas, useless in a practical computer implementation due to the very big numbers that are being fed to the sin(...) function.&lt;/p&gt;
&lt;p&gt;A computer implementation will care only about the discrete samples that need to be generated.&amp;nbsp; The integral in the previous post turns out to be counterproductive - we're much more interested in the infinite sequence of &lt;i&gt;φ&lt;/i&gt;&lt;sub&gt;&lt;i&gt;i&lt;/i&gt;&lt;/sub&gt;, and more particularly in their residue mod 2π.&lt;/p&gt;
&lt;p&gt;Here are Matlab scripts that implement a sine sweep both ways:&lt;/p&gt;
&lt;p&gt;First, the naïve mathematical solution that just calculates the exponential:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;function signal = sinesweep_nostate( ...&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; start, ...&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; finish, ...&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; seconds, ...&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; sample_rate, ...&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; amplitude, ...&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; initial_phase, ...&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; dc ...&lt;br&gt;)&lt;br&gt;% sinesweep_nostate returns a single-channel sine logarithmic sweep&lt;br&gt;%&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; DO NOT USE THIS FUNCTION IN PRODUCTION&lt;br&gt;%&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; THIS IS A PEDAGOGICAL EXERCISE ONLY&lt;br&gt;%&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; THE SIGNAL THIS PRODUCES GRADUALLY DISTORTS&lt;br&gt;%&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; INSTEAD USE sinesweep&lt;br&gt;%&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br&gt;%&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; start: starting frequency in Hz&lt;br&gt;%&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; finish: ending frequency in Hz&lt;br&gt;%&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; seconds: duration of sweep in seconds&lt;br&gt;%&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; samplerate: samples per second&lt;br&gt;%&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; amplitude: amplitude&lt;br&gt;%&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; initial_phase: starting phase&lt;br&gt;%&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; dc: dc&lt;br&gt;&lt;br&gt;% echo these interesting intermediate values to the console&lt;br&gt;R = (finish / start) ^ (1 / seconds)&lt;br&gt;B = 2 * pi * start / log(R)&lt;br&gt;A = initial_phase - B&lt;br&gt;&lt;br&gt;time = 0 : 1 / sample_rate : seconds;&lt;br&gt;phase = A + B * R .^ time;&lt;br&gt;signal = amplitude * sin(phase) + dc;&lt;br&gt;&lt;br&gt;end % sinesweep_nostate&lt;br&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Now, the iterative version that adds up the little Δ&lt;i&gt;φ&lt;/i&gt;s to calculate &lt;i&gt;φ&lt;/i&gt;&lt;sub&gt;&lt;i&gt;i&lt;/i&gt;&lt;/sub&gt; iteratively:&lt;/p&gt;&lt;blockquote&gt;&lt;p&gt;function signal = sinesweep( ...&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; start, ...&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; finish, ...&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; seconds, ...&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; sample_rate, ...&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; amplitude, ...&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; initial_phase, ...&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; dc ...&lt;br&gt;)&lt;br&gt;% sinesweep returns a single-channel sine logarithmic sweep&lt;br&gt;%&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; start: starting frequency in Hz&lt;br&gt;%&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; finish: ending frequency in Hz&lt;br&gt;%&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; seconds: duration of sweep in seconds&lt;br&gt;%&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; samplerate: samples per second&lt;br&gt;%&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; amplitude: amplitude&lt;br&gt;%&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; initial_phase: starting phase&lt;br&gt;%&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; dc: dc&lt;br&gt;&lt;br&gt;time = 0 : 1 / sample_rate : seconds;&lt;br&gt;frequency = exp( ...&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; log(start) * (1 - time / seconds) + ...&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; log(finish) * (time / seconds) ...&lt;br&gt;);&lt;br&gt;&lt;br&gt;phase = 0 * time;&lt;br&gt;phase(1) = initial_phase;&lt;br&gt;for i = 2:length(phase)&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; phase(i) = ...&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; mod(phase(i - 1) + 2 * pi * frequency(i) / sample_rate, 2 * pi);&lt;br&gt;end&lt;br&gt;&lt;br&gt;signal = amplitude * sin(phase) + dc;&lt;br&gt;&lt;br&gt;end % sinesweep&lt;br&gt;&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;If anything, one would expect the first to be more accurate, since it is mathematically precise, and the second is only an approximation.&amp;nbsp; But let's see how the signal produced by each of them holds up.&lt;/p&gt;&lt;p&gt;I calculate, and plot, the same sweep both ways, with a slight dc offset to facilitate seeing the different sweeps on the same plot.&amp;nbsp; In particular this is a logarithmic sweep from 20 kHz to 21 kHz, over 30 seconds, using 44100 samples per second, an amplitude of 0.2, an initial phase of π, and a dc of 0.25 for one and 0.26 for the other:&lt;br&gt;&lt;/p&gt;&lt;p&gt;plot(...&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; x, sinesweep(20000, 21000, 30, 44100, 0.2, pi, 0.2&lt;b&gt;5&lt;/b&gt;), 'd', ...&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; x, sinesweep_nostate(20000, 21000, 30, 44100, 0.2, pi, 0.2&lt;b&gt;6&lt;/b&gt;), 's' ...&lt;br&gt;)&lt;/p&gt;&lt;p&gt;(x is just 1:30*44100 - necessary to allow a single plot statement.)&lt;br&gt;&lt;/p&gt;&lt;p&gt;Note that the approximate method is plotted in diamonds, and will be 0.01 &lt;i&gt;lower&lt;/i&gt; on the resulting graph than the "exact" method, plotted in squares.&lt;/p&gt;&lt;p&gt;(This takes a while to plot because there are a heckuva lot of points...)&lt;/p&gt;&lt;p&gt;Ah, a nice solid mass of green and blue.&amp;nbsp; I won't show it here.&lt;/p&gt;&lt;p&gt;OK, let's zoom in to a very early chunk of the signal - I expect these to match up closely, with only the dc offset separating the points:&lt;/p&gt;&lt;p&gt;&lt;img src="http://blogs.msdn.com/photos/matthew_van_eerde/images/9860730/original.aspx" mce_src="http://blogs.msdn.com/photos/matthew_van_eerde/images/9860730/original.aspx" height="391" width="473"&gt;&lt;/p&gt;&lt;p&gt;Yup.&amp;nbsp; Looks pretty good - give or take a pixel (quantization error in the display.)&lt;br&gt;&lt;/p&gt;&lt;p&gt;Now let's see what happens to the signal towards the end of the 30 seconds.&lt;/p&gt;&lt;p&gt;&lt;img src="http://blogs.msdn.com/photos/matthew_van_eerde/images/9860731/original.aspx" mce_src="http://blogs.msdn.com/photos/matthew_van_eerde/images/9860731/original.aspx" height="389" width="470"&gt;&lt;/p&gt;&lt;p&gt;Ick.&amp;nbsp; Something's rotten in the state of Denmark.&amp;nbsp; This distortion is so bad that it's &lt;i&gt;visible&lt;/i&gt; - you don't even have to take an FFT to see it.&lt;/p&gt;&lt;p&gt;&lt;i&gt;Exercise:&lt;/i&gt; take an FFT and look at the distortion.&lt;/p&gt;&lt;p&gt;&lt;i&gt;Advanced Exercise:&lt;/i&gt; demonstrate that the distortion is, in fact, in the "exact" method and not in the iterative method... that is to say, show which clock is right.&lt;/p&gt;&lt;p&gt;EDIT: On second glance it looks like the second picture is just showing a horizontal shift between the two signals, which is expected.&amp;nbsp; I'll need to dig deeper if I'm to prove my assertion that the iterative method produces less distortion than the "exact" method. &lt;br&gt;&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9860732" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/matthew_van_eerde/archive/tags/Math/default.aspx">Math</category><category domain="http://blogs.msdn.com/matthew_van_eerde/archive/tags/Audio/default.aspx">Audio</category></item><item><title>How to calculate a sine sweep - the wrong way</title><link>http://blogs.msdn.com/matthew_van_eerde/archive/2009/08/03/how-to-calculate-a-sine-sweep-the-wrong-way.aspx</link><pubDate>Tue, 04 Aug 2009 00:07:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9856522</guid><dc:creator>Maurits</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/matthew_van_eerde/comments/9856522.aspx</comments><wfw:commentRss>http://blogs.msdn.com/matthew_van_eerde/commentrss.aspx?PostID=9856522</wfw:commentRss><description>&lt;p&gt;Suppose you want to generate a continuous sine-sweep from time &lt;i&gt;t&lt;/i&gt;&lt;sub&gt;start&lt;/sub&gt; to time &lt;i&gt;t&lt;/i&gt;&lt;sub&gt;end&lt;/sub&gt;.  You want the starting frequency to be &lt;i&gt;ω&lt;/i&gt;&lt;sub&gt;start&lt;/sub&gt;, and the ending frequency to be &lt;i&gt;ω&lt;/i&gt;&lt;sub&gt;end&lt;/sub&gt;; you want the sweep to be logarithmic, so that octaves are swept out in equal times.  (The alternative would be to sweep linearly, but usually logarithmic sweeping is what you want.)  For today we're going to have continuous time and sample values, so sample rate and bit depth will not be part of this discussion; in particular, the units of &lt;i&gt;t&lt;/i&gt; will be seconds, and our sample values will go from -1 to 1.

&lt;/p&gt;&lt;p&gt;Here's a picture of want we want to end up with:
&lt;/p&gt;&lt;p&gt;&lt;img src="http://blogs.msdn.com/photos/matthew_van_eerde/images/9856518/original.aspx" mce_src="http://blogs.msdn.com/photos/matthew_van_eerde/images/9856518/original.aspx" height="447" width="454"&gt;

&lt;/p&gt;&lt;p&gt;Without loss of generality, set &lt;i&gt;t&lt;/i&gt;&lt;sub&gt;start&lt;/sub&gt; = 0.  This simplifies some of the math.
&lt;/p&gt;&lt;p&gt;Let &lt;i&gt;ω&lt;/i&gt;(&lt;i&gt;t&lt;/i&gt;) be the "instantaneous frequency" at time &lt;i&gt;t&lt;/i&gt;.  &lt;i&gt;Advanced Exercise:&lt;/i&gt; Define "instantaneous frequency" mathematically.
&lt;/p&gt;&lt;p&gt;Sweeping &lt;i&gt;ω&lt;/i&gt;(&lt;i&gt;t&lt;/i&gt;) logarithmically means that log &lt;i&gt;ω&lt;/i&gt;(&lt;i&gt;t&lt;/i&gt;) is swept &lt;i&gt;linearly&lt;/i&gt; from log &lt;i&gt;ω&lt;/i&gt;&lt;sub&gt;start&lt;/sub&gt; to log &lt;i&gt;ω&lt;/i&gt;&lt;sub&gt;end&lt;/sub&gt;.  So
&lt;/p&gt;&lt;p&gt;&lt;img src="http://blogs.msdn.com/photos/matthew_van_eerde/images/9856509/original.aspx" mce_src="http://blogs.msdn.com/photos/matthew_van_eerde/images/9856509/original.aspx" height="332" width="457"&gt;
&lt;/p&gt;&lt;p&gt;Quick check: &lt;i&gt;ω&lt;/i&gt;(0) = &lt;i&gt;ω&lt;/i&gt;&lt;sub&gt;start&lt;/sub&gt;, &lt;i&gt;ω&lt;/i&gt;(&lt;i&gt;t&lt;/i&gt;&lt;sub&gt;end&lt;/sub&gt;) = &lt;i&gt;ω&lt;/i&gt;&lt;sub&gt;end&lt;/sub&gt;.  Yup.
&lt;/p&gt;&lt;p&gt;Let's define &lt;i&gt;R&lt;/i&gt; (the rate of change of the frequency) as (&lt;i&gt;ω&lt;/i&gt;&lt;sub&gt;end&lt;/sub&gt; / &lt;i&gt;ω&lt;/i&gt;&lt;sub&gt;start&lt;/sub&gt;)&lt;sup&gt;1 / &lt;i&gt;t&lt;/i&gt;&lt;sub&gt;end&lt;/sub&gt;&lt;/sup&gt;.  This formula reduces to
&lt;/p&gt;&lt;p&gt;&lt;i&gt;ω&lt;/i&gt;(&lt;i&gt;t&lt;/i&gt;) = &lt;i&gt;ω&lt;/i&gt;&lt;sub&gt;start&lt;/sub&gt; &lt;i&gt;R&lt;/i&gt;&lt;sup&gt;&lt;i&gt;t&lt;/i&gt;&lt;/sup&gt;
&lt;/p&gt;&lt;p&gt;Next step is to look at phase.  For a sine wave of &lt;i&gt;constant&lt;/i&gt; frequency &lt;i&gt;ω&lt;/i&gt;, phase progresses linearly with &lt;i&gt;t&lt;/i&gt; on the circular interval [0, 2π):&lt;br&gt;
&lt;i&gt;φ&lt;/i&gt; = &lt;i&gt;k&lt;/i&gt; &lt;i&gt;ω&lt;/i&gt; &lt;i&gt;t&lt;/i&gt;&lt;br&gt;
for some constant &lt;i&gt;k&lt;/i&gt;.  If the frequency of a sine wave was 1 cycle per second, the phase would go from 0 to 2π in a single second - this reveals that the correct value of &lt;i&gt;k&lt;/i&gt; is 2π:&lt;br&gt;
&lt;i&gt;φ&lt;/i&gt; = 2π &lt;i&gt;ω&lt;/i&gt; &lt;i&gt;t&lt;/i&gt;
&lt;/p&gt;&lt;p&gt;What happens with a variable frequency?  A little calculus provides the answer.  Consider a small change Δ&lt;i&gt;t&lt;/i&gt;, small enough so that &lt;i&gt;ω&lt;/i&gt;(&lt;i&gt;t&lt;/i&gt;) is almost constant over the interval:&lt;br&gt;
Δ&lt;i&gt;φ&lt;/i&gt; ≈ 2π &lt;i&gt;ω&lt;/i&gt;(&lt;i&gt;t&lt;/i&gt;) Δ&lt;i&gt;t&lt;/i&gt;&lt;br&gt;
because over that interval the sine sweep function is well approximated by a sine wave of &lt;i&gt;constant&lt;/i&gt; frequency &lt;i&gt;ω&lt;/i&gt;(&lt;i&gt;t&lt;/i&gt;).
&lt;/p&gt;&lt;p&gt;&lt;img src="http://blogs.msdn.com/photos/matthew_van_eerde/images/9856519/original.aspx" mce_src="http://blogs.msdn.com/photos/matthew_van_eerde/images/9856519/original.aspx" height="447" width="454"&gt;
&lt;/p&gt;&lt;p&gt;Starting from an initial phase &lt;i&gt;φ&lt;/i&gt;&lt;sub&gt;start&lt;/sub&gt;, let Δ&lt;i&gt;t&lt;/i&gt; → 0, and:
&lt;/p&gt;&lt;p&gt;&lt;img src="http://blogs.msdn.com/photos/matthew_van_eerde/images/9856511/original.aspx" mce_src="http://blogs.msdn.com/photos/matthew_van_eerde/images/9856511/original.aspx" height="127" width="238"&gt;
&lt;/p&gt;&lt;p&gt;
Substitute:&lt;br&gt;
&lt;i&gt;u&lt;/i&gt; = &lt;i&gt;τ&lt;/i&gt; log &lt;i&gt;R&lt;/i&gt;&lt;br&gt;
&lt;i&gt;τ&lt;/i&gt; = &lt;i&gt;u&lt;/i&gt; / log &lt;i&gt;R&lt;/i&gt;&lt;br&gt;
&lt;i&gt;dτ&lt;/i&gt; = &lt;i&gt;du&lt;/i&gt; / log &lt;i&gt;R&lt;/i&gt;&lt;br&gt;
&lt;/p&gt;&lt;p&gt;&lt;img src="http://blogs.msdn.com/photos/matthew_van_eerde/images/9856513/original.aspx" mce_src="http://blogs.msdn.com/photos/matthew_van_eerde/images/9856513/original.aspx" height="328" width="346"&gt;
&lt;/p&gt;&lt;p&gt;Define:&lt;br&gt;
&lt;i&gt;B&lt;/i&gt; = 2π &lt;i&gt;ω&lt;/i&gt;&lt;sub&gt;start&lt;/sub&gt; / log &lt;i&gt;R&lt;/i&gt;&lt;br&gt;
&lt;i&gt;A&lt;/i&gt; = &lt;i&gt;φ&lt;/i&gt;&lt;sub&gt;start&lt;/sub&gt; - &lt;i&gt;B&lt;/i&gt;&lt;br&gt;
and we have:
&lt;/p&gt;&lt;p&gt;&lt;i&gt;φ&lt;/i&gt;(&lt;i&gt;t&lt;/i&gt;) = &lt;i&gt;A&lt;/i&gt; + &lt;i&gt;B&lt;/i&gt;&lt;i&gt;R&lt;/i&gt;&lt;sup&gt;&lt;i&gt;t&lt;/i&gt;&lt;/sup&gt;
&lt;/p&gt;&lt;p&gt;Just for fun, here's the explicit solution without intermediate values:
&lt;/p&gt;&lt;p&gt;&lt;img src="http://blogs.msdn.com/photos/matthew_van_eerde/images/9856514/original.aspx" mce_src="http://blogs.msdn.com/photos/matthew_van_eerde/images/9856514/original.aspx" height="133" width="495"&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;Now, the payoff: the signal is just the sine of the phase, so our sine sweep is:
&lt;/p&gt;&lt;p&gt;&lt;img src="http://blogs.msdn.com/photos/matthew_van_eerde/images/9856515/original.aspx" mce_src="http://blogs.msdn.com/photos/matthew_van_eerde/images/9856515/original.aspx" height="38" width="335"&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;or, more explicitly:
&lt;/p&gt;&lt;p&gt;&lt;img src="http://blogs.msdn.com/photos/matthew_van_eerde/images/9856516/original.aspx" mce_src="http://blogs.msdn.com/photos/matthew_van_eerde/images/9856516/original.aspx" height="178" width="581"&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;Ah, beautiful mathematically.
&lt;/p&gt;&lt;p&gt;But useless practically.
&lt;/p&gt;&lt;p&gt;Why useless?
&lt;/p&gt;&lt;p&gt;Well, note that our equation for phase is an exponential.  That expression inside the sin(...) gets very big, very quickly.  &lt;a href="http://blogs.msdn.com/matthew_van_eerde/archive/2008/01/18/generating-a-sine-wave-with-phase.aspx" mce_href="http://blogs.msdn.com/matthew_van_eerde/archive/2008/01/18/generating-a-sine-wave-with-phase.aspx"&gt;Any practical implementation of sin(...) is going to be utterly useless once its argument gets beyond a certain threshold&lt;/a&gt; - you'll get ugly chunky steps before you're halfway into the sweep.
&lt;/p&gt;&lt;p&gt;Nonetheless, there is a practical way to generate (pretty good) sine sweeps.  More on that next time.&lt;/p&gt;&lt;p&gt;EDIT: all logs are base &lt;i&gt;e&lt;/i&gt;. &lt;br&gt;&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9856522" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/matthew_van_eerde/archive/tags/Math/default.aspx">Math</category><category domain="http://blogs.msdn.com/matthew_van_eerde/archive/tags/Audio/default.aspx">Audio</category></item><item><title>Why square waves have ears: Gibbs' phenomenon (Wilbraham's phenomenon)</title><link>http://blogs.msdn.com/matthew_van_eerde/archive/2009/05/07/why-square-waves-have-ears-gibbs-phenomenon-wilbraham-s-phenomenon.aspx</link><pubDate>Thu, 07 May 2009 22:30:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9594775</guid><dc:creator>Maurits</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/matthew_van_eerde/comments/9594775.aspx</comments><wfw:commentRss>http://blogs.msdn.com/matthew_van_eerde/commentrss.aspx?PostID=9594775</wfw:commentRss><description>&lt;p&gt;In &lt;a href="http://blogs.msdn.com/matthew_van_eerde/archive/2009/04/07/why-a-full-scale-sine-wave-has-an-intensity-of-3-db-fs.aspx" mce_href="http://blogs.msdn.com/matthew_van_eerde/archive/2009/04/07/why-a-full-scale-sine-wave-has-an-intensity-of-3-db-fs.aspx"&gt;a recent post&lt;/a&gt; I sung the praises of square waves as a way to get a heckuva lot of power (3 dB more power than a sine wave) into a sample-range-limited signal.&amp;nbsp; It's time to take them down a notch now.&lt;/p&gt;

&lt;p&gt;A problem with square waves is they're impossible to generate in the analog domain.&amp;nbsp; In fact, you can't even get close.&lt;/p&gt;

&lt;p&gt;Signals generated in the analog domain are subject to physical laws regarding continuity (no teleportation) and whatnot. A common way to model these is to express (periodic) analog signals using a basis of sine waves with integral periods.&amp;nbsp; Suppose I want to generate the square wave:&lt;/p&gt;

&lt;blockquote&gt;
&lt;table&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td rowspan="3" style=""&gt;&lt;i&gt;f&lt;/i&gt;(&lt;i&gt;x&lt;/i&gt;) = &lt;/td&gt;

&lt;td&gt;1, -π &amp;lt; &lt;i&gt;x&lt;/i&gt; &amp;lt; 0&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;-1, 0 &amp;lt; &lt;i&gt;x&lt;/i&gt; &amp;lt; π&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/blockquote&gt;

&lt;p&gt;Graphed below are some approximations of this function using sine waves as a basis.&amp;nbsp; Note that only odd values of &lt;i&gt;n&lt;/i&gt; are used in the sums of sin(&lt;i&gt;nx&lt;/i&gt;).&lt;br&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src="http://blogs.msdn.com/photos/matthew_van_eerde/images/9594637/original.aspx" mce_src="http://blogs.msdn.com/photos/matthew_van_eerde/images/9594637/original.aspx" height="392" width="479"&gt;&lt;br&gt;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;The sums converge to +/-1 quite well, but there are definite "ears" at &lt;i&gt;x&lt;/i&gt; near 0 where there's overshoot.&amp;nbsp; This doesn't appear to die down.&amp;nbsp; A closeup of one of the "ears":&lt;br&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src="http://blogs.msdn.com/photos/matthew_van_eerde/images/9594787/original.aspx" mce_src="http://blogs.msdn.com/photos/matthew_van_eerde/images/9594787/original.aspx" height="382" width="475"&gt;&lt;br&gt;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;If anything, rather than dying down, the "ears" converge to max(&lt;i&gt;f&lt;sub&gt;n&lt;/sub&gt;&lt;/i&gt;) → about 1.18, or about 9% of the "jump" from -1 to 1.&amp;nbsp; (Dym and McKean, in their 1972 book &lt;a href="http://www.ams.org/mathscinet/search/publdoc.html?pg1=INDI&amp;amp;s1=61225&amp;amp;vfpref=html&amp;amp;r=109&amp;amp;mx-pid=442564" mce_href="http://www.ams.org/mathscinet/search/publdoc.html?pg1=INDI&amp;amp;s1=61225&amp;amp;vfpref=html&amp;amp;r=109&amp;amp;mx-pid=442564"&gt;Fourier Series and Integrals&lt;/a&gt;, get the 9% right but incorrectly assert that the convergence is to 1.09.)&lt;/p&gt;

&lt;p&gt;This mathematical phenomenon - &lt;a href="http://en.wikipedia.org/wiki/Gibbs_phenomenon" mce_href="http://en.wikipedia.org/wiki/Gibbs_phenomenon"&gt;Gibbs' phenomenon&lt;/a&gt; - is a good illustration of the difference between &lt;i&gt;convergence&lt;/i&gt; of a series of functions and &lt;i&gt;uniform convergence&lt;/i&gt;.&lt;/p&gt;

&lt;p&gt;In this case, the series of partial sums &lt;i&gt;pointwise converge&lt;/i&gt; to the square wave... for any given point &lt;i&gt;x&lt;/i&gt; &amp;gt; 0 and ε &amp;gt; 0, the ear will eventually move to the left, and I can choose an &lt;i&gt;N&lt;/i&gt; such that &lt;i&gt;f&lt;sub&gt;n&lt;/sub&gt;&lt;/i&gt;(&lt;i&gt;x&lt;/i&gt;) is within of ε of 1 for all &lt;i&gt;n&lt;/i&gt; &amp;gt; &lt;i&gt;N...&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;... but the series does not &lt;i&gt;uniformly&lt;/i&gt; converge to the square wave.&amp;nbsp; The following assertion is &lt;i&gt;false:&lt;/i&gt; "for any given ε &amp;gt; 0, I can pick an &lt;i&gt;N&lt;/i&gt; such that &lt;i&gt;f&lt;sub&gt;n&lt;/sub&gt;&lt;/i&gt;(&lt;i&gt;x&lt;/i&gt;) is within of ε of 1 for all &lt;i&gt;n&lt;/i&gt; &amp;gt; &lt;i&gt;N&lt;/i&gt; and all &lt;i&gt;x&lt;/i&gt; &amp;gt; 0."&amp;nbsp; This can be demonstrated by picking ε = 0.17, say.&amp;nbsp; For any &lt;i&gt;n&lt;/i&gt;, even, say, &lt;i&gt;n&lt;/i&gt; = 10&lt;sup&gt;100&lt;/sup&gt;, there is an &lt;i&gt;x&lt;/i&gt; close to 0 where &lt;i&gt;f&lt;/i&gt;&lt;sub&gt;1e100&lt;/sub&gt;(&lt;i&gt;x&lt;/i&gt;) &amp;gt; 1.17.
&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9594775" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/matthew_van_eerde/archive/tags/Math/default.aspx">Math</category><category domain="http://blogs.msdn.com/matthew_van_eerde/archive/tags/Audio/default.aspx">Audio</category></item><item><title>Second cousins, cousins once removed; relationships by generations to common ancestor</title><link>http://blogs.msdn.com/matthew_van_eerde/archive/2009/04/27/second-cousins-cousins-once-removed-relationships-by-generations-to-common-ancestor.aspx</link><pubDate>Mon, 27 Apr 2009 21:04:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9571659</guid><dc:creator>Maurits</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/matthew_van_eerde/comments/9571659.aspx</comments><wfw:commentRss>http://blogs.msdn.com/matthew_van_eerde/commentrss.aspx?PostID=9571659</wfw:commentRss><description>
&lt;p&gt;Raymond Chen explains some common terms for blood relatives of varying distance across cultures in his blog post "&lt;a href="http://blogs.msdn.com/oldnewthing/archive/2009/04/27/9570320.aspx" mce_href="http://blogs.msdn.com/oldnewthing/archive/2009/04/27/9570320.aspx"&gt;What kind of uncle am I?&lt;/a&gt;" &lt;/p&gt;

&lt;p&gt;He links to &lt;a href="http://www.genealogy.com/16_cousn.html" mce_href="http://www.genealogy.com/16_cousn.html"&gt;a diagram on genealogy.com&lt;/a&gt; that I felt was lacking something... so here's my version, with consanguinary colors.&lt;br&gt;&lt;/p&gt;

&lt;p&gt;Red means "marriage is almost certainly legally prohibited."&lt;/p&gt;

&lt;p&gt;Yellow means "marriage may be legally prohibited - check your region's laws."&lt;/p&gt;

&lt;p&gt;Green means "marriage is amost certainly legal."&lt;/p&gt;

&lt;p&gt;&lt;br&gt;&lt;/p&gt;

&lt;table style="z-index: 1; background-color: white;" border="1"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td style="text-align: center; font-weight: bold;" colspan="8"&gt;Relationship by generations to common ancestor&lt;/td&gt;
&lt;/tr&gt;

&lt;tr style="font-weight: bold; text-align: center;"&gt;
	
&lt;td&gt;#&lt;/td&gt;
	
&lt;td&gt;0&lt;/td&gt;
	
&lt;td&gt;1&lt;/td&gt;
	
&lt;td&gt;2&lt;/td&gt;
	
&lt;td&gt;3&lt;/td&gt;
	
&lt;td&gt;4&lt;/td&gt;
	
&lt;td&gt;...&lt;/td&gt;
	
&lt;td&gt;&lt;i&gt;m&lt;/i&gt;&lt;/td&gt;
&lt;/tr&gt;

&lt;tr style="vertical-align: top;"&gt;
	
&lt;td style="font-weight: bold; text-align: center;"&gt;0&lt;/td&gt;
	
&lt;td&gt;self&lt;/td&gt;
	
&lt;td style="background-color: rgb(255, 204, 204);"&gt;father/mother&lt;/td&gt;
	
&lt;td style="background-color: rgb(255, 204, 204);"&gt;grand&lt;br&gt;(father/mother)&lt;/td&gt;
	
&lt;td style="background-color: rgb(255, 204, 204);"&gt;great‑&lt;br&gt;grand&lt;br&gt;(father/mother)&lt;/td&gt;
	
&lt;td style="background-color: rgb(255, 204, 204);"&gt;(great‑)&lt;sup&gt;2&lt;/sup&gt;&lt;br&gt;grand&lt;br&gt;(father/mother)&lt;/td&gt;
	
&lt;td&gt;&lt;br&gt;&lt;/td&gt;
	
&lt;td style="background-color: rgb(255, 204, 204);"&gt;(great-)&lt;sup&gt;&lt;i&gt;m&lt;/i&gt;&amp;nbsp;‑&amp;nbsp;2&lt;/sup&gt;&lt;br&gt;grand&lt;br&gt;(father/mother)&lt;/td&gt;
&lt;/tr&gt;

&lt;tr style="vertical-align: top;"&gt;
	
&lt;td style="font-weight: bold; text-align: center;"&gt;1&lt;/td&gt;
	
&lt;td style="background-color: rgb(255, 204, 204);"&gt;son/daughter&lt;/td&gt;
	
&lt;td style="background-color: rgb(255, 204, 204);"&gt;brother/sister&lt;/td&gt;
	
&lt;td style="background-color: rgb(255, 204, 204);"&gt;aunt/uncle&lt;/td&gt;
	
&lt;td style="background-color: yellow;"&gt;grand&lt;br&gt;(aunt/uncle)&lt;/td&gt;
	
&lt;td style="background-color: yellow;"&gt;great‑&lt;br&gt;grand&lt;br&gt;(aunt/uncle)&lt;/td&gt;
	
&lt;td&gt;&lt;br&gt;&lt;/td&gt;
	
&lt;td style="background-color: yellow;"&gt;(great‑)&lt;sup&gt;&lt;i&gt;m&lt;/i&gt;&amp;nbsp;‑&amp;nbsp;3&lt;/sup&gt;&lt;br&gt;grand&lt;br&gt;(aunt/uncle)&lt;/td&gt;
&lt;/tr&gt;

&lt;tr style="vertical-align: top;"&gt;
	
&lt;td style="font-weight: bold; text-align: center;"&gt;2&lt;/td&gt;
	
&lt;td style="background-color: rgb(255, 204, 204);"&gt;grand&lt;br&gt;(son/daughter)&lt;/td&gt;
	
&lt;td style="background-color: rgb(255, 204, 204);"&gt;niece/nephew&lt;/td&gt;
	
&lt;td style="background-color: yellow;"&gt;(first) cousin&lt;/td&gt;
	
&lt;td style="background-color: yellow;"&gt;first cousin,&lt;br&gt;once removed&lt;/td&gt;
	
&lt;td style="background-color: yellow;"&gt;first cousin,&lt;br&gt;twice removed&lt;/td&gt;
	
&lt;td&gt;&lt;br&gt;&lt;/td&gt;
	
&lt;td style="background-color: yellow;"&gt;first cousin,&lt;br&gt;(&lt;i&gt;m&lt;/i&gt;&amp;nbsp;‑&amp;nbsp;2) times removed&lt;/td&gt;
&lt;/tr&gt;

&lt;tr style="vertical-align: top;"&gt;
	
&lt;td style="font-weight: bold; text-align: center;"&gt;3&lt;/td&gt;
	
&lt;td style="background-color: rgb(255, 204, 204);"&gt;great‑&lt;br&gt;grand&lt;br&gt;(son/daughter)&lt;/td&gt;
	
&lt;td style="background-color: yellow;"&gt;grand&lt;br&gt;(niece/nephew)&lt;/td&gt;
	
&lt;td style="background-color: yellow;"&gt;first cousin,&lt;br&gt;once removed&lt;/td&gt;
	
&lt;td style="background-color: rgb(204, 255, 204);"&gt;second cousin&lt;/td&gt;
	
&lt;td style="background-color: rgb(204, 255, 204);"&gt;second cousin,&lt;br&gt;once removed&lt;/td&gt;
	
&lt;td&gt;&lt;br&gt;&lt;/td&gt;
	
&lt;td style="background-color: rgb(204, 255, 204);"&gt;second cousin,&lt;br&gt;(&lt;i&gt;m&lt;/i&gt;&amp;nbsp;‑&amp;nbsp;3) times removed&lt;/td&gt;
&lt;/tr&gt;

&lt;tr style="vertical-align: top;"&gt;
	
&lt;td style="font-weight: bold; text-align: center;"&gt;4&lt;/td&gt;
	
&lt;td style="background-color: rgb(255, 204, 204);"&gt;(great‑)&lt;sup&gt;2&lt;/sup&gt;&lt;br&gt;grand&lt;br&gt;(son/daughter)&lt;/td&gt;
	
&lt;td style="background-color: yellow;"&gt;great-&lt;br&gt;grand&lt;br&gt;(niece/nephew)&lt;/td&gt;
	
&lt;td style="background-color: yellow;"&gt;first cousin,&lt;br&gt;twice removed&lt;/td&gt;
	
&lt;td style="background-color: rgb(204, 255, 204);"&gt;second cousin,&lt;br&gt;once removed&lt;/td&gt;
	
&lt;td style="background-color: rgb(204, 255, 204);"&gt;third cousin&lt;/td&gt;
	
&lt;td&gt;&lt;br&gt;&lt;/td&gt;
	
&lt;td style="background-color: rgb(204, 255, 204);"&gt;third cousin,&lt;br&gt;(&lt;i&gt;m&lt;/i&gt;&amp;nbsp;‑&amp;nbsp;4) times removed&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
	
&lt;td style="font-weight: bold; text-align: center;"&gt;...&lt;/td&gt;
&lt;/tr&gt;

&lt;tr style="vertical-align: top;"&gt;
	
&lt;td style="font-weight: bold; text-align: center;"&gt;&lt;i&gt;n&lt;/i&gt;&lt;/td&gt;
	
&lt;td style="background-color: rgb(255, 204, 204);"&gt;(great‑)&lt;sup&gt;&lt;i&gt;n&lt;/i&gt;&amp;nbsp;‑&amp;nbsp;2&lt;/sup&gt;&lt;br&gt;grand&lt;br&gt;(son/daughter)&lt;/td&gt;
	
&lt;td style="background-color: yellow;"&gt;(great‑)&lt;sup&gt;&lt;i&gt;n&lt;/i&gt;&amp;nbsp;‑&amp;nbsp;3&lt;/sup&gt;&lt;br&gt;grand&lt;br&gt;(niece/nephew)&lt;/td&gt;
	
&lt;td style="background-color: yellow;"&gt;first cousin,&lt;br&gt;(&lt;i&gt;n&lt;/i&gt;&amp;nbsp;‑&amp;nbsp;2) times removed&lt;/td&gt;
	
&lt;td style="background-color: rgb(204, 255, 204);"&gt;second cousin,&lt;br&gt;(&lt;i&gt;n&lt;/i&gt;&amp;nbsp;‑&amp;nbsp;3) times removed&lt;/td&gt;
	
&lt;td style="background-color: rgb(204, 255, 204);"&gt;third cousin,&lt;br&gt;(&lt;i&gt;n&lt;/i&gt;&amp;nbsp;‑&amp;nbsp;4) times removed&lt;/td&gt;
	
&lt;td&gt;&lt;br&gt;&lt;/td&gt;
	
&lt;td style="background-color: rgb(204, 255, 204);"&gt;(&lt;i&gt;n&lt;/i&gt; == &lt;i&gt;m&lt;/i&gt;) ?&lt;br&gt;
		((&lt;i&gt;n&lt;/i&gt;&amp;nbsp;‑&amp;nbsp;1)th cousin) :&lt;br&gt;
		((min(&lt;i&gt;n&lt;/i&gt;, &lt;i&gt;m&lt;/i&gt;)&amp;nbsp;‑&amp;nbsp;1)th cousin, |&lt;i&gt;n&lt;/i&gt;&amp;nbsp;‑&amp;nbsp;&lt;i&gt;m&lt;/i&gt;| times removed)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9571659" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/matthew_van_eerde/archive/tags/Math/default.aspx">Math</category></item><item><title>Why a full-scale sine wave has an intensity of -3 dB FS</title><link>http://blogs.msdn.com/matthew_van_eerde/archive/2009/04/07/why-a-full-scale-sine-wave-has-an-intensity-of-3-db-fs.aspx</link><pubDate>Wed, 08 Apr 2009 01:20:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9536922</guid><dc:creator>Maurits</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/matthew_van_eerde/comments/9536922.aspx</comments><wfw:commentRss>http://blogs.msdn.com/matthew_van_eerde/commentrss.aspx?PostID=9536922</wfw:commentRss><description>&lt;p&gt;I was asked one day why this full-scale sine wave was being measured by our signal analysis tools as -3 dB FS, even though it hits the maximum and the minimum sample values:&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;img src="http://blogs.msdn.com/photos/matthew_van_eerde/images/9536821/original.aspx" mce_src="http://blogs.msdn.com/photos/matthew_van_eerde/images/9536821/original.aspx" width="750" height="565"&gt;&lt;/p&gt;

&lt;p&gt;The answer is "because it's a sine wave, not a square wave."&amp;nbsp; The intensity of a signal can be calculated from the following formula:&lt;/p&gt;

&lt;p&gt;&lt;img src="http://blogs.msdn.com/photos/matthew_van_eerde/images/9536816/original.aspx" mce_src="http://blogs.msdn.com/photos/matthew_van_eerde/images/9536816/original.aspx" width="456" height="234"&gt;&lt;/p&gt;

&lt;p&gt;The inner integral does not depend on &lt;i&gt;t&lt;/i&gt; - it's just the average sample value - so it's usually precalculated:&lt;/p&gt;

&lt;p&gt;&lt;img src="http://blogs.msdn.com/photos/matthew_van_eerde/images/9536805/original.aspx" mce_src="http://blogs.msdn.com/photos/matthew_van_eerde/images/9536805/original.aspx"&gt;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;img src="http://blogs.msdn.com/photos/matthew_van_eerde/images/9536819/original.aspx" mce_src="http://blogs.msdn.com/photos/matthew_van_eerde/images/9536819/original.aspx" width="321" height="127"&gt;&lt;br&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="http://blogs.msdn.com/audiofool/archive/2007/06/21/you-can-t-hear-dc.aspx" mce_href="http://blogs.msdn.com/audiofool/archive/2007/06/21/you-can-t-hear-dc.aspx"&gt;The importance of taking dc into account during analysis&lt;/a&gt; can be appreciated if you try to calculate the intensity of a signal with a high dc.&lt;/p&gt;

&lt;p&gt;&lt;i&gt;Exercise:&lt;/i&gt; calculate the intensity of &lt;i&gt;x&lt;/i&gt;(&lt;i&gt;t&lt;/i&gt;) ≡ -0.5 using the formulas above; calculate the "naïve intensity" by using the last formula above and omitting dc. Note the difference.&lt;/p&gt;

&lt;p&gt;Now that we have the necessary formulas, let's analyze our full-scale sine wave signal.&amp;nbsp; Plugging in a full-scale sine wave and analyzing over a full period we get:&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;img src="http://blogs.msdn.com/photos/matthew_van_eerde/images/9536793/original.aspx" mce_src="http://blogs.msdn.com/photos/matthew_van_eerde/images/9536793/original.aspx" width="285" height="381"&gt;&lt;/p&gt;

&lt;p&gt;As expected, the average value of the signal over a single period is 0.&lt;/p&gt;

&lt;p&gt;Evaluating the intensity requires finding the antiderivative of (sin(&lt;span style="font-style: italic;"&gt;t&lt;/span&gt;))&lt;sup&gt;2&lt;/sup&gt;. This can be ascertained most easily by plotting a few values and realizing that (sin(&lt;span style="font-style: italic;"&gt;t&lt;/span&gt;))&lt;sup&gt;2&lt;/sup&gt; = (1 - cos(2&lt;span style="font-style: italic;"&gt;t&lt;/span&gt;))/2:&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;img src="http://blogs.msdn.com/photos/matthew_van_eerde/images/9536822/original.aspx" mce_src="http://blogs.msdn.com/photos/matthew_van_eerde/images/9536822/original.aspx"&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src="http://blogs.msdn.com/photos/matthew_van_eerde/images/9536823/original.aspx" mce_src="http://blogs.msdn.com/photos/matthew_van_eerde/images/9536823/original.aspx" width="479" height="389"&gt;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;img src="http://blogs.msdn.com/photos/matthew_van_eerde/images/9536813/original.aspx" mce_src="http://blogs.msdn.com/photos/matthew_van_eerde/images/9536813/original.aspx" width="408" height="657"&gt;&lt;/p&gt;

&lt;p&gt;This is a dimensionless number that ranges from 0 (signal is a flat line) to 1 (... we'll get to that later.)&lt;/p&gt;

&lt;p&gt;We can convert this into a dB FS measurement using the formula &lt;span style="font-style: italic;"&gt;I&lt;/span&gt;&lt;sub&gt;dB FS&lt;/sub&gt; = 20 log&lt;sub&gt;10&lt;/sub&gt; &lt;span style="font-style: italic;"&gt;I&lt;/span&gt;&lt;sub&gt;RMS&lt;/sub&gt;:&lt;/p&gt;
&lt;p&gt;&lt;img src="http://blogs.msdn.com/photos/matthew_van_eerde/images/9536814/original.aspx" mce_src="http://blogs.msdn.com/photos/matthew_van_eerde/images/9536814/original.aspx" width="455" height="58"&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-style: italic;"&gt;Et voilà&lt;/span&gt; - &lt;span style="font-style: italic;"&gt;that's&lt;/span&gt; where the -3 dB comes from.&lt;/p&gt;
&lt;p&gt;In contrast to a sine wave, a full-scale &lt;span style="font-style: italic;"&gt;square&lt;/span&gt; wave centered at 0 has an intensity of 0 dB FS:&lt;/p&gt;
&lt;p&gt;&lt;img src="http://blogs.msdn.com/photos/matthew_van_eerde/images/9536824/original.aspx" mce_src="http://blogs.msdn.com/photos/matthew_van_eerde/images/9536824/original.aspx" width="750" height="565"&gt;&lt;/p&gt;
&lt;p&gt;To finish up, a couple of advanced exercises:&lt;/p&gt;
&lt;p&gt;&lt;span style="font-style: italic;"&gt;Advanced Exercise:&lt;/span&gt; prove that, provided &lt;span style="font-style: italic;"&gt;t&lt;/span&gt;&lt;sub&gt;1&lt;/sub&gt; and &lt;span style="font-style: italic;"&gt;t&lt;/span&gt;&lt;sub&gt;2&lt;/sub&gt; are sufficiently far apart, the intensity of a general sine wave &lt;span style="font-style: italic;"&gt;x&lt;/span&gt;(&lt;span style="font-style: italic;"&gt;t&lt;/span&gt;) = &lt;span style="font-style: italic;"&gt;a&lt;/span&gt; sin(&lt;span style="font-style: italic;"&gt;ωt&lt;/span&gt; + &lt;span style="font-style: italic;"&gt;φ&lt;/span&gt;) + &lt;span style="font-style: italic;"&gt;c&lt;/span&gt; depends only on &lt;span style="font-style: italic;"&gt;a&lt;/span&gt; and not on &lt;span style="font-style: italic;"&gt;ω&lt;/span&gt;, &lt;span style="font-style: italic;"&gt;φ&lt;/span&gt;, or &lt;span style="font-style: italic;"&gt;c&lt;/span&gt;.&lt;br&gt;&lt;br&gt;&lt;span style="font-style: italic;"&gt;Advanced Exercise:&lt;/span&gt; completely characterize the set of digital signals that achieve 0 dB FS intensity.&amp;nbsp; If you have, say, 1000 samples of mono 16-bit integer audio to play with, how many distinct signals achieve 0 dB FS intensity?&lt;br&gt;&lt;/p&gt;
&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9536922" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/matthew_van_eerde/archive/tags/Math/default.aspx">Math</category><category domain="http://blogs.msdn.com/matthew_van_eerde/archive/tags/Audio/default.aspx">Audio</category></item><item><title>Rotating a matrix redux</title><link>http://blogs.msdn.com/matthew_van_eerde/archive/2008/09/12/rotating-a-matrix-redux.aspx</link><pubDate>Fri, 12 Sep 2008 19:39:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8947583</guid><dc:creator>Maurits</dc:creator><slash:comments>2</slash:comments><comments>http://blogs.msdn.com/matthew_van_eerde/comments/8947583.aspx</comments><wfw:commentRss>http://blogs.msdn.com/matthew_van_eerde/commentrss.aspx?PostID=8947583</wfw:commentRss><description>&lt;P&gt;&lt;A href="http://polymathprogrammer.com/2008/09/08/rotating-a-matrix-cannot-be-done-with-matrix-multiplication/" mce_href="http://polymathprogrammer.com/2008/09/08/rotating-a-matrix-cannot-be-done-with-matrix-multiplication/"&gt;Vincent Tan picked up&lt;/A&gt; on &lt;A href="http://blogs.msdn.com/oldnewthing/archive/2008/09/02/8918130.aspx" mce_href="http://blogs.msdn.com/oldnewthing/archive/2008/09/02/8918130.aspx"&gt;Raymond Chen's "rotate a matrix" interview&lt;/A&gt; post.&lt;/P&gt;
&lt;P&gt;As Vincent Tan points out, there is no way to create an &lt;I&gt;n&lt;/I&gt; x &lt;I&gt;n&lt;/I&gt; matrix &lt;I&gt;R&lt;/I&gt; such that &lt;I&gt;RA&lt;/I&gt; is a rotated version of &lt;I&gt;A&lt;/I&gt; - for example, in the 2 x 2 case:&lt;BR&gt;&lt;/P&gt;
&lt;DIV&gt;There is no 2 x 2 matrix &lt;I&gt;R&lt;/I&gt; such that &lt;PRE style="FONT-FAMILY: monospace"&gt;R [ a b ] = [ b a ]&lt;BR&gt;  [ c d ]   [ c d ]&lt;BR&gt;&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;Vincent Tan points out that such an &lt;I&gt;R&lt;/I&gt; would entail hidden assumptions but asks for a simpler proof.&lt;/P&gt;
&lt;P&gt;Here it is.&amp;nbsp; I'll go back to the &lt;I&gt;n&lt;/I&gt; by &lt;I&gt;n&lt;/I&gt; case, assuming &lt;I&gt;n&lt;/I&gt; &amp;gt;= 2.&amp;nbsp; (The 0 x 0 case and the 1 x 1 case are actually trivially solvable using the identity matrix.)&lt;/P&gt;
&lt;P&gt;Assume there is such a matrix &lt;I&gt;R&lt;/I&gt; = (&lt;I&gt;r&lt;SUB&gt;ij&lt;/SUB&gt;&lt;/I&gt;)&lt;SUB&gt;&lt;I&gt;i&lt;/I&gt;,&lt;I&gt;j&lt;/I&gt;∈{1...&lt;I&gt;n&lt;/I&gt;}&lt;/SUB&gt; which has the property that &lt;I&gt;RA&lt;/I&gt; = &lt;I&gt;B &lt;/I&gt;= (&lt;I&gt;b&lt;SUB&gt;ij&lt;/SUB&gt;&lt;/I&gt;)&lt;SUB&gt;&lt;I&gt;i&lt;/I&gt;,&lt;I&gt;j&lt;/I&gt;∈{1...&lt;I&gt;n&lt;/I&gt;} &lt;/SUB&gt;is the rotated version of &lt;I&gt;A&lt;/I&gt; for any &lt;I&gt;n&lt;/I&gt; x &lt;I&gt;n&lt;/I&gt; matrix &lt;I&gt;A = &lt;/I&gt;(&lt;I&gt;a&lt;SUB&gt;ij&lt;/SUB&gt;&lt;/I&gt;)&lt;SUB&gt;&lt;I&gt;i&lt;/I&gt;,&lt;I&gt;j&lt;/I&gt;∈{1...&lt;I&gt;n&lt;/I&gt;}&lt;/SUB&gt;&lt;I&gt;.&lt;/I&gt;&lt;/P&gt;
&lt;P&gt;To see that this is impossible, consider the matrix with a 1 in the upper-left-hand corner and zeros everywhere else; that is, &lt;I&gt;a&lt;/I&gt;&lt;SUB&gt;1,1&lt;/SUB&gt; = 1, and &lt;I&gt;a&lt;SUB&gt;ij&lt;/SUB&gt;&lt;/I&gt; = 0 for all other combinations of &lt;I&gt;i&lt;/I&gt; and &lt;I&gt;j&lt;/I&gt;.&lt;/P&gt;
&lt;P&gt;To be the rotated version of this matrix, &lt;I&gt;B&lt;/I&gt;&lt;SUB&gt;&lt;/SUB&gt; should have &lt;I&gt;b&lt;SUB&gt;n&lt;/SUB&gt;&lt;/I&gt;&lt;SUB&gt;1&lt;/SUB&gt; = 1 and &lt;I&gt;b&lt;SUB&gt;ij&lt;/SUB&gt;&lt;/I&gt; = 0 for all other combinations of &lt;I&gt;i&lt;/I&gt; and &lt;I&gt;j&lt;/I&gt;.&amp;nbsp; (My coordinates are such that &lt;I&gt;b&lt;SUB&gt;n&lt;/SUB&gt;&lt;/I&gt;&lt;SUB&gt;1 &lt;/SUB&gt;is the number in the top right corner of the matrix.)&lt;/P&gt;
&lt;P&gt;But how did that 1 get in the top right hand corner?&amp;nbsp; The rules of matrix multiplication imply that &lt;I&gt;b&lt;SUB&gt;n&lt;/SUB&gt;&lt;/I&gt;&lt;SUB&gt;1&lt;/SUB&gt; = &lt;I&gt;r&lt;/I&gt;&lt;SUB&gt;1,1&lt;/SUB&gt;&lt;I&gt;a&lt;SUB&gt;n&lt;/SUB&gt;&lt;/I&gt;&lt;SUB&gt;1&lt;/SUB&gt; + &lt;I&gt;r&lt;/I&gt;&lt;SUB&gt;2,1&lt;/SUB&gt;&lt;I&gt;a&lt;SUB&gt;n&lt;/SUB&gt;&lt;/I&gt;&lt;SUB&gt;2&lt;/SUB&gt; + &lt;I&gt;... + r&lt;/I&gt;&lt;SUB&gt;&lt;I&gt;n&lt;/I&gt;1&lt;/SUB&gt;&lt;I&gt;a&lt;SUB&gt;nn&lt;/SUB&gt;&lt;/I&gt;. At first blush, this looks fine... until we realize that &lt;I&gt;b&lt;SUB&gt;n&lt;/SUB&gt;&lt;/I&gt;&lt;SUB&gt;1 &lt;/SUB&gt;is 1, and all of the &lt;I&gt;a&lt;SUB&gt;nj &lt;/SUB&gt;&lt;/I&gt;are 0... so we have the contradiction&lt;/P&gt;
&lt;P&gt;1 = &lt;I&gt;r&lt;/I&gt;&lt;SUB&gt;1,1&lt;/SUB&gt;0 + &lt;I&gt;r&lt;/I&gt;&lt;SUB&gt;2,1&lt;/SUB&gt;0 + &lt;I&gt;... + r&lt;/I&gt;&lt;SUB&gt;&lt;I&gt;n&lt;/I&gt;1&lt;/SUB&gt;0 = 0&lt;/P&gt;
&lt;P&gt;QED.&lt;/P&gt;
&lt;P&gt;But this is not how rotation matrices are applied.&amp;nbsp; Rotation matrices &lt;I&gt;R&lt;/I&gt; are not applied as &lt;I&gt;RA&lt;/I&gt; = &lt;I&gt;B&lt;/I&gt;; instead, they're applied as &lt;I&gt;RAR&lt;/I&gt;&lt;SUP&gt;-1&lt;/SUP&gt; = &lt;I&gt;B&lt;/I&gt;.&amp;nbsp; Not that this helps our hapless interviewee; the operation being asked of him ("rotate a matrix") can't be done by a rotation matrix anyway.&amp;nbsp; That is, there is no magic matrix &lt;I&gt;R&lt;/I&gt; that works under this operation either.&lt;/P&gt;
&lt;P&gt;EDIT: A simple way to prove that &lt;EM&gt;RAR&lt;/EM&gt;&lt;SUP&gt;-1&lt;/SUP&gt; = &lt;I&gt;B &lt;/I&gt;doesn't work either (for &lt;EM&gt;n&lt;/EM&gt; &amp;gt;= 2)&amp;nbsp;is to think about what happens when you choose &lt;EM&gt;A&lt;/EM&gt; to be the identity matrix &lt;EM&gt;I&lt;/EM&gt; (the matrix with 1's on the main diagonal&amp;nbsp;[&lt;EM&gt;i&lt;/EM&gt; = &lt;EM&gt;j&lt;/EM&gt;]&amp;nbsp;and 0's everywhere else.)&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;RIR&lt;/EM&gt;&lt;SUP&gt;-1&lt;/SUP&gt; = &lt;EM&gt;RR&lt;/EM&gt;&lt;SUP&gt;-1&lt;/SUP&gt;&amp;nbsp;= &lt;EM&gt;I&lt;/EM&gt;; and for &lt;EM&gt;n&lt;/EM&gt; &amp;gt;= 2, &lt;EM&gt;I&lt;/EM&gt; is not its own "rotation."&lt;/P&gt;
&lt;P&gt;We therefore arrive at the paradoxical result that you can't rotate a matrix via matrix rotation.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8947583" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/matthew_van_eerde/archive/tags/Math/default.aspx">Math</category></item><item><title>The largest prime ever - you saw it here first</title><link>http://blogs.msdn.com/matthew_van_eerde/archive/2008/08/29/the-largest-prime-ever-you-saw-it-here-first.aspx</link><pubDate>Sat, 30 Aug 2008 01:34:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8907161</guid><dc:creator>Maurits</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/matthew_van_eerde/comments/8907161.aspx</comments><wfw:commentRss>http://blogs.msdn.com/matthew_van_eerde/commentrss.aspx?PostID=8907161</wfw:commentRss><description>&lt;p&gt;&lt;a href="http://mersenne.org/prime.htm" mce_href="http://mersenne.org/prime.htm"&gt;The GIMPS project says they've found the largest prime number ever&lt;/a&gt;, but they're keeping quiet about what it is until they've verified it (they expect to be done in a couple of weeks.)&lt;br&gt;&lt;/p&gt;

&lt;p&gt;Pshaw.&amp;nbsp; I can tell you right now what their prime number is.&lt;/p&gt;

&lt;p&gt;Since I'm a computer scientist I'll write it down in binary.&lt;/p&gt;

&lt;p&gt;GIMPS' LARGEST PRIME NUMBER IS (scroll down / highlight to view:)&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;0b&lt;span style="color: black; background-color: black;"&gt;&lt;br&gt;111111111111111111111111111111111111111111111111&lt;br&gt;111111111111111111111111111111111111111111111111&lt;br&gt;111111111111111111111111111111111111111111111111&lt;br&gt;111111111111111111111111111111111111111111111111&lt;br&gt;111111111111111111111111111111111111111111111111&lt;br&gt;111111111111111111111111111111111111111111111111&lt;br&gt;111111111111111111111111111111111111111111111111&lt;br&gt;111111111111111111111111111111111111111111111111&lt;br&gt;111111111111111111111111111111111111111111111111&lt;br&gt;&lt;/span&gt;...&lt;br&gt;&lt;span style="color: black; background-color: black;"&gt;111111111111111111111111111111111111111111111111&lt;br&gt;111111111111111111111111111111111111111111111111&lt;br&gt;111111111111111111111111111111111111111111111111&lt;br&gt;111111111111111111111111111111111111111111111111&lt;br&gt;111111111111111111111111111111111111111111111111&lt;br&gt;111111111111111111111111111111111111111111111111&lt;br&gt;111111111111111111111111111111111111111111111111&lt;br&gt;111111111111111111111111111111111111111111111111&lt;br&gt;111111111111111111111111111111111111111111111111&lt;br&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;EDIT: On Saturday 9/6 another prime was found.&amp;nbsp; They're also keeping mum about what this one is.&amp;nbsp; But I know this one too...&lt;/p&gt;&lt;p&gt;EDIT2: Every new Mersenne prime also means a new perfect number is discovered; counting these two new Mersenne primes, there are now 46 known perfect numbers, all of them even.&amp;nbsp; (It is conjectured, but not proven, that &lt;i&gt;all&lt;/i&gt; perfect numbers are even.)&amp;nbsp; To go from a Mersenne prime (which is of the form 0b111...11, where there are a prime number of 1's) to its corresponding perfect number, tack on one fewer number of 0's onto the end of the number: e.g., 0b11 (3, the first Mersenne prime) becomes 0b110 (6, the first perfect number;) 0b111 (7, the second Mersenne prime) becomes 0b11100 (28, the second perfect number) etc. (The proof that such numbers are perfect is simple; there is a more complicated proof that &lt;i&gt;all&lt;/i&gt; even perfect numbers are of this form.)&lt;/p&gt;&lt;p&gt;EDIT: I can now reveal that the number of 1s is 43,112,609.&lt;br&gt;&lt;/p&gt;
&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8907161" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/matthew_van_eerde/archive/tags/Math/default.aspx">Math</category></item><item><title>Solution: x! + y! + z! = x * y * z</title><link>http://blogs.msdn.com/matthew_van_eerde/archive/2008/08/08/solution-x-y-z-x-y-z.aspx</link><pubDate>Sat, 09 Aug 2008 02:08:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8844571</guid><dc:creator>Maurits</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/matthew_van_eerde/comments/8844571.aspx</comments><wfw:commentRss>http://blogs.msdn.com/matthew_van_eerde/commentrss.aspx?PostID=8844571</wfw:commentRss><description>Last time I discussed the fact that &lt;a href="http://blogs.msdn.com/matthew_van_eerde/archive/2008/08/07/interview-question-find-solution-to-x-y-z-x-y-z.aspx" mce_href="http://blogs.msdn.com/matthew_van_eerde/archive/2008/08/07/interview-question-find-solution-to-x-y-z-x-y-z.aspx"&gt;there is a single, unique, solution to the formula &lt;i&gt;x&lt;/i&gt; ! + &lt;i&gt;y&lt;/i&gt; ! + &lt;i&gt;z&lt;/i&gt; ! = &lt;i&gt;x&lt;/i&gt; * &lt;i&gt;y&lt;/i&gt; * z, where &lt;i&gt;x&lt;/i&gt;, &lt;i&gt;y&lt;/i&gt;, and &lt;i&gt;z&lt;/i&gt; are non-negative integers&lt;/a&gt;.&amp;nbsp; I asked for a computer program to find the solution given that &lt;i&gt;x&lt;/i&gt;, &lt;i&gt;y&lt;/i&gt;, and &lt;i&gt;z&lt;/i&gt; are all &amp;lt;= 9 but I wondered aloud whether the solution was unique without that constraint. &lt;br&gt;

&lt;p&gt;The part where a computer can't help is the uniqueness.&amp;nbsp; Here is a mathematical proof that there are no solutions if any of &lt;i&gt;x&lt;/i&gt;, &lt;i&gt;y&lt;/i&gt;, or &lt;i&gt;z&lt;/i&gt; are &amp;gt;= 6: &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Assume without loss of generality that &lt;i&gt;x&lt;/i&gt; &amp;gt;= &lt;i&gt;y&lt;/i&gt; =&amp;gt; &lt;i&gt;z&lt;/i&gt;.&amp;nbsp; The following two inequalities hold trivially, since we're dealing with non-negative numbers:&lt;br&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;i&gt;x&lt;/i&gt; ! + &lt;i&gt;y&lt;/i&gt; ! + &lt;i&gt;z&lt;/i&gt; ! &amp;gt; &lt;i&gt;x&lt;/i&gt; !&lt;br&gt;&lt;i&gt;x&lt;/i&gt; * &lt;i&gt;y&lt;/i&gt; * &lt;i&gt;z&lt;/i&gt; &amp;lt;= &lt;i&gt;x&lt;/i&gt; &lt;sup&gt;3&lt;/sup&gt;&lt;br&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Here's where 6 comes in: &lt;br&gt;&lt;/p&gt;

&lt;p&gt;&lt;i&gt;Lemma&lt;/i&gt;: for &lt;i&gt;x&lt;/i&gt; &amp;gt;= 6, &lt;i&gt;x&lt;/i&gt; ! &amp;gt; &lt;i&gt;x&lt;/i&gt; &lt;sup&gt;3&lt;/sup&gt; (proof momentarily.)&lt;/p&gt;

&lt;p&gt;If this lemma were true, then we'd be done... for &lt;i&gt;x&lt;/i&gt; &amp;gt;= 6,&lt;/p&gt;

&lt;blockquote&gt;&lt;i&gt;x&lt;/i&gt; ! + &lt;i&gt;y&lt;/i&gt; ! + &lt;i&gt;z&lt;/i&gt; ! &amp;gt; &lt;b&gt;&lt;i&gt;x&lt;/i&gt; ! &amp;gt; &lt;i&gt;x&lt;/i&gt; &lt;sup&gt;3&lt;/sup&gt;&lt;/b&gt; &amp;gt;= &lt;i&gt;x&lt;/i&gt; * &lt;i&gt;y&lt;/i&gt; * &lt;i&gt;z&lt;/i&gt;&lt;br&gt;&lt;/blockquote&gt;

&lt;p&gt;So a search for solutions for &lt;i&gt;x&lt;/i&gt; ! + &lt;i&gt;y&lt;/i&gt; ! + &lt;i&gt;z&lt;/i&gt; ! = &lt;i&gt;x&lt;/i&gt; * &lt;i&gt;y&lt;/i&gt; * &lt;i&gt;z&lt;/i&gt; can be restricted to 5 &amp;gt;= &lt;i&gt;x&lt;/i&gt; &amp;gt;= &lt;i&gt;y&lt;/i&gt; &amp;gt;= &lt;i&gt;z&lt;/i&gt;.&amp;nbsp; This is computationally feasible.&lt;br&gt;By inspection, there is a unique solution (see later in this post.)&lt;br&gt;&lt;br&gt;&lt;i&gt;Proof of lemma&lt;/i&gt;: first, look at values of &lt;i&gt;x&lt;/i&gt; ! and &lt;i&gt;x&lt;/i&gt; &lt;sup&gt;3&lt;/sup&gt; for &lt;i&gt;x = &lt;/i&gt;0 through 7:&lt;br&gt;&lt;br&gt;&lt;/p&gt;

&lt;blockquote&gt;0! = 1 &amp;gt; 0 = 0&lt;sup&gt;3&lt;/sup&gt;&lt;br&gt;1! = 1 = 1 = 1&lt;sup&gt;3&lt;/sup&gt;&lt;br&gt;2! = 2 &amp;lt; 8 = 2&lt;sup&gt;3&lt;/sup&gt;&lt;br&gt;3! = 6 &amp;lt; 27 = 3&lt;sup&gt;3&lt;/sup&gt;&lt;br&gt;4! = 24 &amp;lt; 64 = 4&lt;sup&gt;3&lt;/sup&gt;&lt;br&gt;5! = 120 &amp;lt; 125 = 5&lt;sup&gt;3&lt;/sup&gt;&lt;br&gt;6! = 720 &amp;gt; 216 = 6&lt;sup&gt;3&lt;/sup&gt;&lt;br&gt;7! = 5040 &amp;gt; 343 = 7&lt;sup&gt;3&lt;/sup&gt;&lt;br&gt;&lt;/blockquote&gt;

&lt;p&gt;&lt;br&gt;It's not immediately clear how to progress.&amp;nbsp; The key idea, as often in geometric-ish progressions: look at successive ratios.&amp;nbsp; The initial 0 messes things up a bit, so we'll start with &lt;i&gt;x&lt;/i&gt; = 1.&lt;br&gt;&lt;br&gt;On the left, numbers advance by a factor of:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;(&lt;i&gt;x&lt;/i&gt; + 1)! / &lt;i&gt;x&lt;/i&gt; ! = &lt;i&gt;x &lt;/i&gt;+ 1&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Note this ratio gets bigger and bigger.&lt;/p&gt;

&lt;p&gt;On the right, numbers advance by a factor of&lt;/p&gt;

&lt;blockquote&gt;(&lt;i&gt;x&lt;/i&gt; + 1)&lt;sup&gt;3 &lt;/sup&gt;/ &lt;i&gt;x&lt;/i&gt; &lt;sup&gt;3&lt;/sup&gt; = ((&lt;i&gt;x&lt;/i&gt; + 1) / &lt;i&gt;x&lt;/i&gt;)&lt;sup&gt;3&lt;/sup&gt; = (1 + (1 / &lt;i&gt;x&lt;/i&gt;))&lt;sup&gt;3&lt;/sup&gt;&lt;/blockquote&gt;
Note this rate of increase actually gets &lt;i&gt;smaller&lt;/i&gt;.&lt;br&gt;
&lt;p&gt;The crossover point - where &lt;i&gt;x&lt;/i&gt; ! stops losing ground and begins to catch up - is &lt;i&gt;x&lt;/i&gt; = 3;&lt;br&gt;&lt;br&gt;1 + 1 = 2 &amp;lt; 8 = (1 + (1/1))&lt;sup&gt;3&lt;/sup&gt;&lt;br&gt;2 + 1 = 3 &amp;lt; 3.375 = (1 + (1/2))&lt;sup&gt;3&lt;/sup&gt;&lt;br&gt;3 + 1 = 4 &amp;gt; 2.370370... = (1 + (1/3))&lt;sup&gt;3&lt;/sup&gt;&lt;br&gt;&lt;br&gt;By &lt;i&gt;x&lt;/i&gt; = 6, the lost ground has been made up, and &lt;span style="font-style: italic;"&gt;x&lt;/span&gt; ! never looks back. QED.&lt;br&gt;&lt;/p&gt;
&lt;/blockquote&gt;


&lt;p&gt;By the proof above, it only remains to look at the finite number of cases where &lt;span style="font-style: italic;"&gt;x&lt;/span&gt;, &lt;span style="font-style: italic;"&gt;y&lt;/span&gt;, and &lt;span style="font-style: italic;"&gt;z&lt;/span&gt; are all &amp;lt;= 5.&amp;nbsp; Here they all are: note that in addition to the unique solution for the equality&lt;/p&gt;

&lt;p style="margin-left: 40px;"&gt;&lt;span style="font-style: italic;"&gt;x&lt;/span&gt; ! + &lt;span style="font-style: italic;"&gt;y&lt;/span&gt; ! + &lt;span style="font-style: italic;"&gt;z&lt;/span&gt; ! = &lt;span style="font-style: italic;"&gt;x&lt;/span&gt; * &lt;span style="font-style: italic;"&gt;y&lt;/span&gt; * &lt;span style="font-style: italic;"&gt;z&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;there are only four solutions to the inequality&lt;/p&gt;

&lt;p style="margin-left: 40px;"&gt;&lt;span style="font-style: italic;"&gt;x&lt;/span&gt; ! + &lt;span style="font-style: italic;"&gt;y&lt;/span&gt; ! + &lt;span style="font-style: italic;"&gt;z&lt;/span&gt; ! &amp;lt; &lt;span style="font-style: italic;"&gt;x&lt;/span&gt; * &lt;span style="font-style: italic;"&gt;y&lt;/span&gt; * &lt;span style="font-style: italic;"&gt;z&lt;/span&gt; &lt;/p&gt;

&lt;p&gt;All of the other cases have&lt;/p&gt;

&lt;p style="margin-left: 40px;"&gt;&lt;span style="font-style: italic;"&gt;x&lt;/span&gt; ! + &lt;span style="font-style: italic;"&gt;y&lt;/span&gt; ! + &lt;span style="font-style: italic;"&gt;z&lt;/span&gt; ! &amp;gt; &lt;span style="font-style: italic;"&gt;x&lt;/span&gt; * &lt;span style="font-style: italic;"&gt;y&lt;/span&gt; * &lt;span style="font-style: italic;"&gt;z&lt;/span&gt;.&lt;br&gt;&lt;/p&gt;

&lt;style&gt;
	table#fac td { font-size: 8pt; border: 1px solid black; text-align: center; padding: 3px; }
	table#fac td.break { border: none; }
&lt;/style&gt;
&lt;table id="fac"&gt;
	&lt;tbody&gt;
&lt;tr&gt;
		
&lt;td&gt;0!&amp;nbsp;+&amp;nbsp;0!&amp;nbsp;+&amp;nbsp;0!&lt;br&gt;=&amp;nbsp;3&amp;nbsp;&amp;gt;&amp;nbsp;0&amp;nbsp;=&lt;br&gt;0&amp;nbsp;*&amp;nbsp;0&amp;nbsp;*&amp;nbsp;0&lt;/td&gt;
	&lt;/tr&gt;
	
&lt;tr&gt;
&lt;td colspan="6" class="break"&gt;&amp;nbsp;&lt;/td&gt;
&lt;/tr&gt;
	
&lt;tr&gt;
		
&lt;td&gt;1!&amp;nbsp;+&amp;nbsp;0!&amp;nbsp;+&amp;nbsp;0!&lt;br&gt;=&amp;nbsp;3&amp;nbsp;&amp;gt;&amp;nbsp;0&amp;nbsp;=&lt;br&gt;1&amp;nbsp;*&amp;nbsp;0&amp;nbsp;*&amp;nbsp;0&lt;/td&gt;
	&lt;/tr&gt;
	
&lt;tr&gt;
		
&lt;td&gt;1!&amp;nbsp;+&amp;nbsp;1!&amp;nbsp;+&amp;nbsp;0!&lt;br&gt;=&amp;nbsp;3&amp;nbsp;&amp;gt;&amp;nbsp;0&amp;nbsp;=&lt;br&gt;1&amp;nbsp;*&amp;nbsp;1&amp;nbsp;*&amp;nbsp;0&lt;/td&gt;
		
&lt;td&gt;1!&amp;nbsp;+&amp;nbsp;1!&amp;nbsp;+&amp;nbsp;1!&lt;br&gt;=&amp;nbsp;3&amp;nbsp;&amp;gt;&amp;nbsp;1&amp;nbsp;=&lt;br&gt;1&amp;nbsp;*&amp;nbsp;1&amp;nbsp;*&amp;nbsp;1&lt;/td&gt;
	&lt;/tr&gt;
	
&lt;tr&gt;
&lt;td colspan="6" class="break"&gt;&amp;nbsp;&lt;/td&gt;
&lt;/tr&gt;
	
&lt;tr&gt;
		
&lt;td&gt;2!&amp;nbsp;+&amp;nbsp;0!&amp;nbsp;+&amp;nbsp;0!&lt;br&gt;=&amp;nbsp;4&amp;nbsp;&amp;gt;&amp;nbsp;0&amp;nbsp;=&lt;br&gt;2&amp;nbsp;*&amp;nbsp;0&amp;nbsp;*&amp;nbsp;0&lt;/td&gt;
	&lt;/tr&gt;
	
&lt;tr&gt;
		
&lt;td&gt;2!&amp;nbsp;+&amp;nbsp;1!&amp;nbsp;+&amp;nbsp;0!&lt;br&gt;=&amp;nbsp;4&amp;nbsp;&amp;gt;&amp;nbsp;0&amp;nbsp;=&lt;br&gt;2&amp;nbsp;*&amp;nbsp;1&amp;nbsp;*&amp;nbsp;0&lt;/td&gt;
		
&lt;td&gt;2!&amp;nbsp;+&amp;nbsp;1!&amp;nbsp;+&amp;nbsp;1!&lt;br&gt;=&amp;nbsp;4&amp;nbsp;&amp;gt;&amp;nbsp;2&amp;nbsp;=&lt;br&gt;2&amp;nbsp;*&amp;nbsp;1&amp;nbsp;*&amp;nbsp;1&lt;/td&gt;
	&lt;/tr&gt;
	
&lt;tr&gt;
		
&lt;td&gt;2!&amp;nbsp;+&amp;nbsp;2!&amp;nbsp;+&amp;nbsp;0!&lt;br&gt;=&amp;nbsp;5&amp;nbsp;&amp;gt;&amp;nbsp;0&amp;nbsp;=&lt;br&gt;2&amp;nbsp;*&amp;nbsp;2&amp;nbsp;*&amp;nbsp;0&lt;/td&gt;
		
&lt;td&gt;2!&amp;nbsp;+&amp;nbsp;2!&amp;nbsp;+&amp;nbsp;1!&lt;br&gt;=&amp;nbsp;5&amp;nbsp;&amp;gt;&amp;nbsp;4&amp;nbsp;=&lt;br&gt;2&amp;nbsp;*&amp;nbsp;2&amp;nbsp;*&amp;nbsp;1&lt;/td&gt;
		
&lt;td style="background-color: rgb(238, 238, 238);"&gt;2!&amp;nbsp;+&amp;nbsp;2!&amp;nbsp;+&amp;nbsp;2!&lt;br&gt;=&amp;nbsp;6&amp;nbsp;&amp;lt;&amp;nbsp;8&amp;nbsp;=&lt;br&gt;2&amp;nbsp;*&amp;nbsp;2&amp;nbsp;*&amp;nbsp;2&lt;/td&gt;
	&lt;/tr&gt;
	
&lt;tr&gt;
&lt;td colspan="6" class="break"&gt;&amp;nbsp;&lt;/td&gt;
&lt;/tr&gt;
	
&lt;tr&gt;
		
&lt;td&gt;3!&amp;nbsp;+&amp;nbsp;0!&amp;nbsp;+&amp;nbsp;0!&lt;br&gt;=&amp;nbsp;8&amp;nbsp;&amp;gt;&amp;nbsp;0&amp;nbsp;=&lt;br&gt;3&amp;nbsp;*&amp;nbsp;0&amp;nbsp;*&amp;nbsp;0&lt;/td&gt;
	&lt;/tr&gt;
	
&lt;tr&gt;
		
&lt;td&gt;3!&amp;nbsp;+&amp;nbsp;1!&amp;nbsp;+&amp;nbsp;0!&lt;br&gt;=&amp;nbsp;8&amp;nbsp;&amp;gt;&amp;nbsp;0&amp;nbsp;=&lt;br&gt;3&amp;nbsp;*&amp;nbsp;1&amp;nbsp;*&amp;nbsp;0&lt;/td&gt;
		
&lt;td&gt;3!&amp;nbsp;+&amp;nbsp;1!&amp;nbsp;+&amp;nbsp;1!&lt;br&gt;=&amp;nbsp;8&amp;nbsp;&amp;gt;&amp;nbsp;3&amp;nbsp;=&lt;br&gt;3&amp;nbsp;*&amp;nbsp;1&amp;nbsp;*&amp;nbsp;1&lt;/td&gt;
	&lt;/tr&gt;
	
&lt;tr&gt;
		
&lt;td&gt;3!&amp;nbsp;+&amp;nbsp;2!&amp;nbsp;+&amp;nbsp;0!&lt;br&gt;=&amp;nbsp;9&amp;nbsp;&amp;gt;&amp;nbsp;0&amp;nbsp;=&lt;br&gt;3&amp;nbsp;*&amp;nbsp;2&amp;nbsp;*&amp;nbsp;0&lt;/td&gt;
		
&lt;td&gt;3!&amp;nbsp;+&amp;nbsp;2!&amp;nbsp;+&amp;nbsp;1!&lt;br&gt;=&amp;nbsp;9&amp;nbsp;&amp;gt;&amp;nbsp;6&amp;nbsp;=&lt;br&gt;3&amp;nbsp;*&amp;nbsp;2&amp;nbsp;*&amp;nbsp;1&lt;/td&gt;
		
&lt;td style="background-color: rgb(238, 238, 238);"&gt;3!&amp;nbsp;+&amp;nbsp;2!&amp;nbsp;+&amp;nbsp;2!&lt;br&gt;=&amp;nbsp;10&amp;nbsp;&amp;lt;&amp;nbsp;12&amp;nbsp;=&lt;br&gt;3&amp;nbsp;*&amp;nbsp;2&amp;nbsp;*&amp;nbsp;2&lt;/td&gt;
	&lt;/tr&gt;
	
&lt;tr&gt;
		
&lt;td&gt;3!&amp;nbsp;+&amp;nbsp;3!&amp;nbsp;+&amp;nbsp;0!&lt;br&gt;=&amp;nbsp;13&amp;nbsp;&amp;gt;&amp;nbsp;0&amp;nbsp;=&lt;br&gt;3&amp;nbsp;*&amp;nbsp;3&amp;nbsp;*&amp;nbsp;0&lt;/td&gt;
		
&lt;td&gt;3!&amp;nbsp;+&amp;nbsp;3!&amp;nbsp;+&amp;nbsp;1!&lt;br&gt;=&amp;nbsp;13&amp;nbsp;&amp;gt;&amp;nbsp;9&amp;nbsp;=&lt;br&gt;3&amp;nbsp;*&amp;nbsp;3&amp;nbsp;*&amp;nbsp;1&lt;/td&gt;
		
&lt;td style="background-color: rgb(238, 238, 238);"&gt;3!&amp;nbsp;+&amp;nbsp;3!&amp;nbsp;+&amp;nbsp;2!&lt;br&gt;=&amp;nbsp;14&amp;nbsp;&amp;lt;&amp;nbsp;18&amp;nbsp;=&lt;br&gt;3&amp;nbsp;*&amp;nbsp;3&amp;nbsp;*&amp;nbsp;2&lt;/td&gt;
		
&lt;td style="background-color: rgb(238, 238, 238);"&gt;3!&amp;nbsp;+&amp;nbsp;3!&amp;nbsp;+&amp;nbsp;3!&lt;br&gt;=&amp;nbsp;18&amp;nbsp;&amp;lt;&amp;nbsp;27&amp;nbsp;=&lt;br&gt;3&amp;nbsp;*&amp;nbsp;3&amp;nbsp;*&amp;nbsp;3&lt;/td&gt;
	&lt;/tr&gt;
	
&lt;tr&gt;
&lt;td colspan="6" class="break"&gt;&amp;nbsp;&lt;/td&gt;
&lt;/tr&gt;
	
&lt;tr&gt;
		
&lt;td&gt;4!&amp;nbsp;+&amp;nbsp;0!&amp;nbsp;+&amp;nbsp;0!&lt;br&gt;=&amp;nbsp;26&amp;nbsp;&amp;gt;&amp;nbsp;0&amp;nbsp;=&lt;br&gt;4&amp;nbsp;*&amp;nbsp;0&amp;nbsp;*&amp;nbsp;0&lt;/td&gt;
	&lt;/tr&gt;
	
&lt;tr&gt;
		
&lt;td&gt;4!&amp;nbsp;+&amp;nbsp;1!&amp;nbsp;+&amp;nbsp;0!&lt;br&gt;=&amp;nbsp;26&amp;nbsp;&amp;gt;&amp;nbsp;0&amp;nbsp;=&lt;br&gt;4&amp;nbsp;*&amp;nbsp;1&amp;nbsp;*&amp;nbsp;0&lt;/td&gt;
		
&lt;td&gt;4!&amp;nbsp;+&amp;nbsp;1!&amp;nbsp;+&amp;nbsp;1!&lt;br&gt;=&amp;nbsp;26&amp;nbsp;&amp;gt;&amp;nbsp;4&amp;nbsp;=&lt;br&gt;4&amp;nbsp;*&amp;nbsp;1&amp;nbsp;*&amp;nbsp;1&lt;/td&gt;
	&lt;/tr&gt;
	
&lt;tr&gt;
		
&lt;td&gt;4!&amp;nbsp;+&amp;nbsp;2!&amp;nbsp;+&amp;nbsp;0!&lt;br&gt;=&amp;nbsp;27&amp;nbsp;&amp;gt;&amp;nbsp;0&amp;nbsp;=&lt;br&gt;4&amp;nbsp;*&amp;nbsp;2&amp;nbsp;*&amp;nbsp;0&lt;/td&gt;
		

&lt;td&gt;4!&amp;nbsp;+&amp;nbsp;2!&amp;nbsp;+&amp;nbsp;1!&lt;br&gt;=&amp;nbsp;27&amp;nbsp;&amp;gt;&amp;nbsp;8&amp;nbsp;=&lt;br&gt;4&amp;nbsp;*&amp;nbsp;2&amp;nbsp;*&amp;nbsp;1&lt;/td&gt;
		
&lt;td&gt;4!&amp;nbsp;+&amp;nbsp;2!&amp;nbsp;+&amp;nbsp;2!&lt;br&gt;=&amp;nbsp;28&amp;nbsp;&amp;gt;&amp;nbsp;16&amp;nbsp;=&lt;br&gt;4&amp;nbsp;*&amp;nbsp;2&amp;nbsp;*&amp;nbsp;2&lt;/td&gt;
	&lt;/tr&gt;
	
&lt;tr&gt;
		
&lt;td&gt;4!&amp;nbsp;+&amp;nbsp;3!&amp;nbsp;+&amp;nbsp;0!&lt;br&gt;=&amp;nbsp;31&amp;nbsp;&amp;gt;&amp;nbsp;0&amp;nbsp;=&lt;br&gt;4&amp;nbsp;*&amp;nbsp;3&amp;nbsp;*&amp;nbsp;0&lt;/td&gt;
		
&lt;td&gt;4!&amp;nbsp;+&amp;nbsp;3!&amp;nbsp;+&amp;nbsp;1!&lt;br&gt;=&amp;nbsp;31&amp;nbsp;&amp;gt;&amp;nbsp;12&amp;nbsp;=&lt;br&gt;4&amp;nbsp;*&amp;nbsp;3&amp;nbsp;*&amp;nbsp;1&lt;/td&gt;
		
&lt;td&gt;4!&amp;nbsp;+&amp;nbsp;3!&amp;nbsp;+&amp;nbsp;2!&lt;br&gt;=&amp;nbsp;32&amp;nbsp;&amp;gt;&amp;nbsp;24&amp;nbsp;=&lt;br&gt;4&amp;nbsp;*&amp;nbsp;3&amp;nbsp;*&amp;nbsp;2&lt;/td&gt;
		
&lt;td style="background-color: yellow; font-weight: bold;"&gt;4!&amp;nbsp;+&amp;nbsp;3!&amp;nbsp;+&amp;nbsp;3!&lt;br&gt;=&amp;nbsp;36&amp;nbsp;=&amp;nbsp;36&amp;nbsp;=&lt;br&gt;4&amp;nbsp;*&amp;nbsp;3&amp;nbsp;*&amp;nbsp;3&lt;/td&gt;
	&lt;/tr&gt;
	
&lt;tr&gt;
		
&lt;td&gt;4!&amp;nbsp;+&amp;nbsp;4!&amp;nbsp;+&amp;nbsp;0!&lt;br&gt;=&amp;nbsp;49&amp;nbsp;&amp;gt;&amp;nbsp;0&amp;nbsp;=&lt;br&gt;4&amp;nbsp;*&amp;nbsp;4&amp;nbsp;*&amp;nbsp;0&lt;/td&gt;
		
&lt;td&gt;4!&amp;nbsp;+&amp;nbsp;4!&amp;nbsp;+&amp;nbsp;1!&lt;br&gt;=&amp;nbsp;49&amp;nbsp;&amp;gt;&amp;nbsp;16&amp;nbsp;=&lt;br&gt;4&amp;nbsp;*&amp;nbsp;4&amp;nbsp;*&amp;nbsp;1&lt;/td&gt;
		
&lt;td&gt;4!&amp;nbsp;+&amp;nbsp;4!&amp;nbsp;+&amp;nbsp;2!&lt;br&gt;=&amp;nbsp;50&amp;nbsp;&amp;gt;&amp;nbsp;32&amp;nbsp;=&lt;br&gt;4&amp;nbsp;*&amp;nbsp;4&amp;nbsp;*&amp;nbsp;2&lt;/td&gt;
		
&lt;td&gt;4!&amp;nbsp;+&amp;nbsp;4!&amp;nbsp;+&amp;nbsp;3!&lt;br&gt;=&amp;nbsp;54&amp;nbsp;&amp;gt;&amp;nbsp;48&amp;nbsp;=&lt;br&gt;4&amp;nbsp;*&amp;nbsp;4&amp;nbsp;*&amp;nbsp;3&lt;/td&gt;
		
&lt;td&gt;4!&amp;nbsp;+&amp;nbsp;4!&amp;nbsp;+&amp;nbsp;4!&lt;br&gt;=&amp;nbsp;72&amp;nbsp;&amp;gt;&amp;nbsp;64&amp;nbsp;=&lt;br&gt;4&amp;nbsp;*&amp;nbsp;4&amp;nbsp;*&amp;nbsp;4&lt;/td&gt;
	&lt;/tr&gt;
	
&lt;tr&gt;
&lt;td colspan="6" class="break"&gt;&amp;nbsp;&lt;/td&gt;
&lt;/tr&gt;
	
&lt;tr&gt;
		
&lt;td&gt;5!&amp;nbsp;+&amp;nbsp;0!&amp;nbsp;+&amp;nbsp;0!&lt;br&gt;=&amp;nbsp;122&amp;nbsp;&amp;gt;&amp;nbsp;0&amp;nbsp;=&lt;br&gt;5&amp;nbsp;*&amp;nbsp;0&amp;nbsp;*&amp;nbsp;0&lt;/td&gt;
	&lt;/tr&gt;
	
&lt;tr&gt;
		
&lt;td&gt;5!&amp;nbsp;+&amp;nbsp;1!&amp;nbsp;+&amp;nbsp;0!&lt;br&gt;=&amp;nbsp;122&amp;nbsp;&amp;gt;&amp;nbsp;0&amp;nbsp;=&lt;br&gt;5&amp;nbsp;*&amp;nbsp;1&amp;nbsp;*&amp;nbsp;0&lt;/td&gt;
		
&lt;td&gt;5!&amp;nbsp;+&amp;nbsp;1!&amp;nbsp;+&amp;nbsp;1!&lt;br&gt;=&amp;nbsp;122&amp;nbsp;&amp;gt;&amp;nbsp;5&amp;nbsp;=&lt;br&gt;5&amp;nbsp;*&amp;nbsp;1&amp;nbsp;*&amp;nbsp;1&lt;/td&gt;
	&lt;/tr&gt;
	
&lt;tr&gt;
		
&lt;td&gt;5!&amp;nbsp;+&amp;nbsp;2!&amp;nbsp;+&amp;nbsp;0!&lt;br&gt;=&amp;nbsp;123&amp;nbsp;&amp;gt;&amp;nbsp;0&amp;nbsp;=&lt;br&gt;5&amp;nbsp;*&amp;nbsp;2&amp;nbsp;*&amp;nbsp;0&lt;/td&gt;
		
&lt;td&gt;5!&amp;nbsp;+&amp;nbsp;2!&amp;nbsp;+&amp;nbsp;1!&lt;br&gt;=&amp;nbsp;123&amp;nbsp;&amp;gt;&amp;nbsp;10&amp;nbsp;=&lt;br&gt;5&amp;nbsp;*&amp;nbsp;2&amp;nbsp;*&amp;nbsp;1&lt;/td&gt;
		
&lt;td&gt;5!&amp;nbsp;+&amp;nbsp;2!&amp;nbsp;+&amp;nbsp;2!&lt;br&gt;=&amp;nbsp;124&amp;nbsp;&amp;gt;&amp;nbsp;20&amp;nbsp;=&lt;br&gt;5&amp;nbsp;*&amp;nbsp;2&amp;nbsp;*&amp;nbsp;2&lt;/td&gt;
	&lt;/tr&gt;
	
&lt;tr&gt;
		
&lt;td&gt;5!&amp;nbsp;+&amp;nbsp;3!&amp;nbsp;+&amp;nbsp;0!&lt;br&gt;=&amp;nbsp;127&amp;nbsp;&amp;gt;&amp;nbsp;0&amp;nbsp;=&lt;br&gt;5&amp;nbsp;*&amp;nbsp;3&amp;nbsp;*&amp;nbsp;0&lt;/td&gt;
		
&lt;td&gt;5!&amp;nbsp;+&amp;nbsp;3!&amp;nbsp;+&amp;nbsp;1!&lt;br&gt;=&amp;nbsp;127&amp;nbsp;&amp;gt;&amp;nbsp;15&amp;nbsp;=&lt;br&gt;5&amp;nbsp;*&amp;nbsp;3&amp;nbsp;*&amp;nbsp;1&lt;/td&gt;
		
&lt;td&gt;5!&amp;nbsp;+&amp;nbsp;3!&amp;nbsp;+&amp;nbsp;2!&lt;br&gt;=&amp;nbsp;128&amp;nbsp;&amp;gt;&amp;nbsp;30&amp;nbsp;=&lt;br&gt;5&amp;nbsp;*&amp;nbsp;3&amp;nbsp;*&amp;nbsp;2&lt;/td&gt;
		
&lt;td&gt;5!&amp;nbsp;+&amp;nbsp;3!&amp;nbsp;+&amp;nbsp;3!&lt;br&gt;=&amp;nbsp;132&amp;nbsp;&amp;gt;&amp;nbsp;45&amp;nbsp;=&lt;br&gt;5&amp;nbsp;*&amp;nbsp;3&amp;nbsp;*&amp;nbsp;3&lt;/td&gt;
	&lt;/tr&gt;
	
&lt;tr&gt;
		
&lt;td&gt;5!&amp;nbsp;+&amp;nbsp;4!&amp;nbsp;+&amp;nbsp;0!&lt;br&gt;=&amp;nbsp;145&amp;nbsp;&amp;gt;&amp;nbsp;0&amp;nbsp;=&lt;br&gt;5&amp;nbsp;*&amp;nbsp;4&amp;nbsp;*&amp;nbsp;0&lt;/td&gt;
		
&lt;td&gt;5!&amp;nbsp;+&amp;nbsp;4!&amp;nbsp;+&amp;nbsp;1!&lt;br&gt;=&amp;nbsp;145&amp;nbsp;&amp;gt;&amp;nbsp;20&amp;nbsp;=&lt;br&gt;5&amp;nbsp;*&amp;nbsp;4&amp;nbsp;*&amp;nbsp;1&lt;/td&gt;
		
&lt;td&gt;5!&amp;nbsp;+&amp;nbsp;4!&amp;nbsp;+&amp;nbsp;2!&lt;br&gt;=&amp;nbsp;146&amp;nbsp;&amp;gt;&amp;nbsp;40&amp;nbsp;=&lt;br&gt;5&amp;nbsp;*&amp;nbsp;4&amp;nbsp;*&amp;nbsp;2&lt;/td&gt;
		
&lt;td&gt;5!&amp;nbsp;+&amp;nbsp;4!&amp;nbsp;+&amp;nbsp;3!&lt;br&gt;=&amp;nbsp;150&amp;nbsp;&amp;gt;&amp;nbsp;60&amp;nbsp;=&lt;br&gt;5&amp;nbsp;*&amp;nbsp;4&amp;nbsp;*&amp;nbsp;3&lt;/td&gt;
		
&lt;td&gt;5!&amp;nbsp;+&amp;nbsp;4!&amp;nbsp;+&amp;nbsp;4!&lt;br&gt;=&amp;nbsp;168&amp;nbsp;&amp;gt;&amp;nbsp;80&amp;nbsp;=&lt;br&gt;5&amp;nbsp;*&amp;nbsp;4&amp;nbsp;*&amp;nbsp;4&lt;/td&gt;
	&lt;/tr&gt;
	
&lt;tr&gt;
		
&lt;td&gt;5!&amp;nbsp;+&amp;nbsp;5!&amp;nbsp;+&amp;nbsp;0!&lt;br&gt;=&amp;nbsp;241&amp;nbsp;&amp;gt;&amp;nbsp;0&amp;nbsp;=&lt;br&gt;5&amp;nbsp;*&amp;nbsp;5&amp;nbsp;*&amp;nbsp;0&lt;/td&gt;
		
&lt;td&gt;5!&amp;nbsp;+&amp;nbsp;5!&amp;nbsp;+&amp;nbsp;1!&lt;br&gt;=&amp;nbsp;241&amp;nbsp;&amp;gt;&amp;nbsp;25&amp;nbsp;=&lt;br&gt;5&amp;nbsp;*&amp;nbsp;5&amp;nbsp;*&amp;nbsp;1&lt;/td&gt;
		
&lt;td&gt;5!&amp;nbsp;+&amp;nbsp;5!&amp;nbsp;+&amp;nbsp;2!&lt;br&gt;=&amp;nbsp;242&amp;nbsp;&amp;gt;&amp;nbsp;50&amp;nbsp;=&lt;br&gt;5&amp;nbsp;*&amp;nbsp;5&amp;nbsp;*&amp;nbsp;2&lt;/td&gt;
		
&lt;td&gt;5!&amp;nbsp;+&amp;nbsp;5!&amp;nbsp;+&amp;nbsp;3!&lt;br&gt;=&amp;nbsp;246&amp;nbsp;&amp;gt;&amp;nbsp;75&amp;nbsp;=&lt;br&gt;5&amp;nbsp;*&amp;nbsp;5&amp;nbsp;*&amp;nbsp;3&lt;/td&gt;
		
&lt;td&gt;5!&amp;nbsp;+&amp;nbsp;5!&amp;nbsp;+&amp;nbsp;4!&lt;br&gt;=&amp;nbsp;264&amp;nbsp;&amp;gt;&amp;nbsp;100&amp;nbsp;=&lt;br&gt;5&amp;nbsp;*&amp;nbsp;5&amp;nbsp;*&amp;nbsp;4&lt;/td&gt;
		
&lt;td&gt;5!&amp;nbsp;+&amp;nbsp;5!&amp;nbsp;+&amp;nbsp;5!&lt;br&gt;=&amp;nbsp;360&amp;nbsp;&amp;gt;&amp;nbsp;125&amp;nbsp;=&lt;br&gt;5&amp;nbsp;*&amp;nbsp;5&amp;nbsp;*&amp;nbsp;5&lt;/td&gt;
	&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;

&lt;p&gt;Here's the program I used to check the cases for &lt;span style="font-style: italic;"&gt;x&lt;/span&gt;, &lt;span style="font-style: italic;"&gt;y&lt;/span&gt;, &lt;span style="font-style: italic;"&gt;z&lt;/span&gt; &amp;lt;= 9.&amp;nbsp; I used perl.&amp;nbsp; Note the similarity to &lt;a href="http://blogs.msdn.com/matthew_van_eerde/archive/2008/08/07/interview-question-find-solution-to-x-y-z-x-y-z.aspx#8842239" mce_href="http://blogs.msdn.com/matthew_van_eerde/archive/2008/08/07/interview-question-find-solution-to-x-y-z-x-y-z.aspx#8842239"&gt;Zbyszek's solution&lt;/a&gt;.&lt;br&gt;&lt;/p&gt;

&lt;pre style="font-family: monospace; font-size: 8pt;"&gt;use strict;&lt;br&gt;&lt;br&gt;# initialize table of factorials with 0! = 1&lt;br&gt;my @fac_table = (1);&lt;br&gt;&lt;br&gt;# loop executes 10 times&lt;br&gt;# interesting fact - solution remains unique even if you use higher bases&lt;br&gt;# (e.g., change 9 to 15 for hex)&lt;br&gt;for my $i (0 .. 9) {&lt;br&gt;    $fac_table[$i + 1] = ($i + 1) * $fac_table[$i];&lt;br&gt;&lt;br&gt;    # loop executes 1 + 2 + ... + 9 + 10 = 55 times&lt;br&gt;    for my $j (0 .. $i) {&lt;br&gt;&lt;br&gt;        # inner loop executes&lt;br&gt;        # 1 +&lt;br&gt;        # 1 + 2 +&lt;br&gt;        # ...&lt;br&gt;        # 1 + 2 + ... + 9 +&lt;br&gt;        # 1 + 2 + ... + 9 + 10 times&lt;br&gt;        # &lt;br&gt;        # Here's how I counted that up:&lt;br&gt;        # if $i, $j, and $k are all different,&lt;br&gt;        # there are (10 choose 3) = 10 * 9 * 8 / 1 * 2 * 3 = 120 ways&lt;br&gt;        # if $i, $j, and $k are all the same,&lt;br&gt;        # there are 10 ways&lt;br&gt;        # if $i and $k are different, but $j is the same as one or the other,&lt;br&gt;        # there are (10 choose 2) ways to pick $i and $k = 10 * 9 / 1 * 2 = 45 ways&lt;br&gt;        # and two choices in each of these for $j so there are 90 ways&lt;br&gt;        # total = 120 + 10 + 90 = 220 iterations&lt;br&gt;        # (verified with a loop counter)&lt;br&gt;        for my $k (0 .. $j) {&lt;br&gt;            # note $i &amp;gt;= $j &amp;gt;= $k&lt;br&gt;&lt;br&gt;            if ($fac_table[$i] + $fac_table[$j] + $fac_table[$k] == $i * $j * $k) {&lt;br&gt;                print qq($i! + $j! + $k! == $i * $j * $k\n);&lt;br&gt;            }&lt;br&gt;        }&lt;br&gt;    }&lt;br&gt;}&lt;br&gt;&lt;/pre&gt;

&lt;p&gt;A cute little exercise.&lt;br&gt;&lt;/p&gt;
&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8844571" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/matthew_van_eerde/archive/tags/Math/default.aspx">Math</category></item><item><title>Interview question: find solution to x! + y! + z! = x * y * z</title><link>http://blogs.msdn.com/matthew_van_eerde/archive/2008/08/07/interview-question-find-solution-to-x-y-z-x-y-z.aspx</link><pubDate>Fri, 08 Aug 2008 01:54:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8841888</guid><dc:creator>Maurits</dc:creator><slash:comments>7</slash:comments><comments>http://blogs.msdn.com/matthew_van_eerde/comments/8841888.aspx</comments><wfw:commentRss>http://blogs.msdn.com/matthew_van_eerde/commentrss.aspx?PostID=8841888</wfw:commentRss><description>&lt;p&gt;I stumbled on &lt;a href="http://blogs.msdn.com/hmlee/archive/2006/03/01/541628.aspx" mce_href="http://blogs.msdn.com/hmlee/archive/2006/03/01/541628.aspx"&gt;hmlee's algorithm chart&lt;/a&gt; quite by chance.&amp;nbsp; A lot of good questions in there.&lt;/p&gt;&lt;p&gt;Question 53 caught my eye as a mathematician.&lt;br&gt;&lt;/p&gt;&lt;blockquote&gt;&lt;p&gt;Q53: Say you have three integers between 0 - 9. You have the equation: A! + B! + 
C! = ABC (where ABS is a three digit numbers, not A * B * C). Find A, B, and C 
that satisfies this equation.&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;Interesting. But I'd like to modify the question a little.&lt;/p&gt;&lt;blockquote&gt;&lt;p&gt;Q53': Say you have three integers between 0 - 9. You have the equation: A! + B! + 
C! = &lt;b&gt;A * B * C&lt;/b&gt;.&lt;br&gt;Find A, B, and C 
that satisfies this equation.&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;Astute readers will notice that A, B, and C are interchangeable.&amp;nbsp; Nonetheless there is a unique solution (modulo swapping A, B, and C.)&lt;/p&gt;&lt;p&gt;I'm much less interested in the answer to this question (I know the answer) than I am in the quality of the program used to find the answer.&amp;nbsp; (I tried finding the answer by hunt-and-peck, then gave up and wrote a program - I find the program to be more interesting than the answer.)&amp;nbsp; Try to find the solution as efficiently as possible (without cheating.)&lt;br&gt;&lt;/p&gt;&lt;p&gt;I'll post my program later.&lt;/p&gt;&lt;p&gt;&lt;a href="http://blogs.msdn.com/matthew_van_eerde/archive/2007/12/18/if-you-see-a-fact-try-to-see-it-as-intuitively-as-possible.aspx" mce_href="http://blogs.msdn.com/matthew_van_eerde/archive/2007/12/18/if-you-see-a-fact-try-to-see-it-as-intuitively-as-possible.aspx"&gt;If you see a fact, try to see it as intuitively as possible.&lt;/a&gt;&lt;/p&gt;&lt;p&gt;I &lt;i&gt;suspect&lt;/i&gt; that the following even more general problem has the same unique solution, which would be very interesting indeed.&amp;nbsp; This is the kind of thing where programs fail, and the mathematical mind becomes necessary again: &lt;br&gt;&lt;/p&gt;&lt;blockquote&gt;
  &lt;p&gt;Q53'': Say you have three integers &lt;b&gt;that are &amp;gt;= 0&lt;/b&gt;. You have the equation: A! + B! + 
C! = A * B * C.&lt;br&gt;
Find A, B, and C 
that satisfies this equation.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8841888" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/matthew_van_eerde/archive/tags/Math/default.aspx">Math</category></item><item><title>Triangular primes: or, the dangers of non-rigorous proofs</title><link>http://blogs.msdn.com/matthew_van_eerde/archive/2008/06/15/triangular-primes-or-the-dangers-of-non-rigorous-proofs.aspx</link><pubDate>Sun, 15 Jun 2008 20:41:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8601604</guid><dc:creator>Maurits</dc:creator><slash:comments>7</slash:comments><comments>http://blogs.msdn.com/matthew_van_eerde/comments/8601604.aspx</comments><wfw:commentRss>http://blogs.msdn.com/matthew_van_eerde/commentrss.aspx?PostID=8601604</wfw:commentRss><description>
&lt;div&gt;Beware of bugs in the above code; I have only proved it correct, not tried it.&lt;/div&gt;&lt;div style="text-align: right;"&gt;-- Donald Knuth&lt;/div&gt;
&lt;p&gt;If you've bowled, you know the arrangement of the bowling pins forms a triangle.&lt;/p&gt;&lt;p&gt;&lt;img src="http://blogs.msdn.com/photos/matthew_van_eerde/images/8601570/original.aspx" alt="http://blogs.msdn.com/photos/matthew_van_eerde/images/8601570/original.aspx"&gt;&lt;/p&gt;&lt;p&gt;(Image courtesy of the &lt;a href="http://www.downingpumpkins.com/bowling.html" mce_href="http://www.downingpumpkins.com/bowling.html"&gt;International Pumpkin Federation&lt;/a&gt;.) &lt;br&gt;&lt;/p&gt;&lt;p&gt;If you've played eight-ball, you know the arrangement of the fifteen billiard balls forms a triangle.&lt;/p&gt;&lt;p&gt;&lt;img src="http://blogs.msdn.com/photos/matthew_van_eerde/images/8601581/original.aspx" alt="http://blogs.msdn.com/photos/matthew_van_eerde/images/8601581/original.aspx"&gt; &lt;br&gt;&lt;/p&gt;&lt;p&gt;Ten, fifteen... what other numbers form a triangle? The common arrangement of nine-ball and ninepins doesn't count because it's a diamond, not a triangle.&lt;br&gt;&lt;/p&gt;&lt;p&gt;You can start with ten and add five balls to make a triangle of fifteen... then add six more to make a triangle of 21... then seven more to make a triangle of 28... and so on, with this sequence:&lt;/p&gt;&lt;p&gt;..., 10, 15, 21, 28, 36, 45, 55, 66, 78, 91, 105, 120, 136, 153, 171, 190, 210, 231...&lt;br&gt;&lt;/p&gt;&lt;p&gt;Start looking for patterns.&amp;nbsp; What do you see?&amp;nbsp; Nothing jumps out right away.&amp;nbsp; Are there any primes?&amp;nbsp; Ones that end in 0, 2, 4, 6, or 8 are obviously even and therefore not primes... ones that end in 5 are divisible by 5 and therefore not primes... the remaining ones fall due to specific cases (21 = 3 x 7; 91 = 7 x 13; 153 = 3 x 3 x 17; 171 = 3 x 3 x 19; 231 = 3 x 7 x 11...)&lt;/p&gt;&lt;p&gt;In fact, gosh darn it, it seems like &lt;i&gt;none&lt;/i&gt; of these numbers are prime, no matter how far we extend that "..." on the right!&amp;nbsp; Can this be a coincidence?&lt;/p&gt;&lt;p&gt;A few mental coruscations later I have an idea that it is &lt;i&gt;not&lt;/i&gt; a coincidence... that triangular numbers, by there very nature, &lt;i&gt;cannot be prime.&lt;/i&gt;&amp;nbsp; In fact I'm even willing to call it a "proof."&amp;nbsp; Here it is.&lt;/p&gt;&lt;p&gt;&lt;i&gt;"Theorem":&lt;/i&gt; there are no triangular primes.&lt;br&gt;&lt;/p&gt;

&lt;p&gt;First we need to generate a formula for the triangular numbers.&amp;nbsp; Note that if you take an &lt;i&gt;n&lt;/i&gt; x (&lt;i&gt;n&lt;/i&gt; + 1) rectangle and draw a zig-zag line like so, you get two triangles.&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;img src="http://blogs.msdn.com/photos/matthew_van_eerde/images/8601535/original.aspx" mce_src="http://blogs.msdn.com/photos/matthew_van_eerde/images/8601535/original.aspx" alt="http://blogs.msdn.com/photos/matthew_van_eerde/images/8601535/original.aspx"&gt;&lt;/p&gt;

&lt;p&gt;Each of these triangles is composed of &lt;i&gt;n&lt;/i&gt; diagonals, the shortest of which is 1, and the longest of which is &lt;i&gt;n&lt;/i&gt;.&amp;nbsp; That is to say, each of the triangles is composed of &lt;i&gt;t&lt;sub&gt;n&lt;/sub&gt;&lt;/i&gt; squares.&amp;nbsp; So we know that the total number of squares in the rectangle is 2&lt;i&gt;t&lt;sub&gt;n&lt;/sub&gt;&lt;/i&gt;.&lt;/p&gt;
&lt;p&gt;But we also know that the total number of squares is the base times the height, or &lt;i&gt;n&lt;/i&gt;(&lt;i&gt;n&lt;/i&gt; + 1).&amp;nbsp; This gives us 2&lt;i&gt;t&lt;sub&gt;n&lt;/sub&gt;&lt;/i&gt; = &lt;i&gt;n&lt;/i&gt;(&lt;i&gt;n&lt;/i&gt; + 1), or &lt;i&gt;t&lt;sub&gt;n&lt;/sub&gt;&lt;/i&gt; = &lt;i&gt;n&lt;/i&gt;(&lt;i&gt;n&lt;/i&gt; + 1)/2.&lt;/p&gt;
&lt;p&gt;The next part of the "proof" breaks down into case analysis.&amp;nbsp; &lt;i&gt;n&lt;/i&gt; can be odd (as in the diagram, where &lt;i&gt;n&lt;/i&gt; is 5) or even.&lt;/p&gt;
&lt;p&gt;&lt;i&gt;&lt;b&gt;Case where &lt;/b&gt;&lt;/i&gt;&lt;b&gt;n&lt;/b&gt;&lt;i&gt;&lt;b&gt; is even&lt;/b&gt;&lt;/i&gt;:&lt;/p&gt;
&lt;p&gt;&lt;i&gt;n&lt;/i&gt; is an even positive number.&amp;nbsp; Therefore &lt;i&gt;n&lt;/i&gt;/2 is a positive number (maybe odd, maybe even; doesn't matter.)&amp;nbsp; &lt;i&gt;t&lt;sub&gt;n&lt;/sub&gt;&lt;/i&gt; can be written as (&lt;i&gt;n&lt;/i&gt;/2)(&lt;i&gt;n&lt;/i&gt; + 1), and is therefore not prime, since it has at least &lt;i&gt;four&lt;/i&gt; factors: 1, &lt;i&gt;n&lt;/i&gt;/2, &lt;i&gt;n &lt;/i&gt;+ 1, and &lt;i&gt;t&lt;sub&gt;n&lt;/sub&gt;&lt;/i&gt;.&lt;br&gt;&lt;/p&gt;
&lt;p&gt;&lt;i&gt;&lt;b&gt;Case where &lt;/b&gt;&lt;/i&gt;&lt;b&gt;n&lt;/b&gt;&lt;i&gt;&lt;b&gt; is odd&lt;/b&gt;&lt;/i&gt;:&lt;/p&gt;
&lt;p&gt;&lt;i&gt;n&lt;/i&gt; is an odd positive number.&amp;nbsp; Therefore &lt;i&gt;n&lt;/i&gt; + 1 is an &lt;i&gt;even&lt;/i&gt; positive number.&amp;nbsp; Therefore (&lt;i&gt;n&lt;/i&gt; + 1)/2 is a positive number (maybe odd, maybe even; doesn't matter.)&amp;nbsp; &lt;i&gt;t&lt;sub&gt;n&lt;/sub&gt;&lt;/i&gt; can be written as (&lt;i&gt;n&lt;/i&gt;)([&lt;i&gt;n&lt;/i&gt; + 1]/2), and is therefore not prime, since it has at least &lt;i&gt;four&lt;/i&gt; factors: 1, &lt;i&gt;n&lt;/i&gt;, (&lt;i&gt;n&lt;/i&gt; + 1)/2, and &lt;i&gt;t&lt;sub&gt;n&lt;/sub&gt;&lt;/i&gt;.&lt;/p&gt;
&lt;p&gt;Note that the "proof" that &lt;i&gt;t&lt;sub&gt;n&lt;/sub&gt;&lt;/i&gt; is not prime inevitably concludes in a stronger result - that &lt;i&gt;t&lt;sub&gt;n&lt;/sub&gt;&lt;/i&gt; has at least &lt;i&gt;four&lt;/i&gt; factors... not only is &lt;i&gt;t&lt;sub&gt;n&lt;/sub&gt;&lt;/i&gt; not prime, it can't even have as few as &lt;i&gt;three&lt;/i&gt; factors.&amp;nbsp; (Some numbers with three factors: 4, 9, 25, 49...)&lt;/p&gt;
&lt;p&gt;&lt;i&gt;Exercise: &lt;/i&gt;what kind of numbers have exactly three factors?&lt;/p&gt;
&lt;p&gt;A beautiful proof.&amp;nbsp; Perhaps the two cases can be elegantly folded together to normalize it a bit better.&amp;nbsp; That is not a serious problem.&lt;/p&gt;
&lt;p&gt;There &lt;i&gt;is&lt;/i&gt; a serious problem.&lt;/p&gt;
&lt;p&gt;The proof of our result is &lt;i&gt;doomed&lt;/i&gt;.&lt;/p&gt;
&lt;p&gt;Why?&lt;/p&gt;
&lt;p&gt;&lt;i&gt;Because the result does not hold!&lt;/i&gt; There &lt;i&gt;is&lt;/i&gt; a triangular prime.&lt;/p&gt;
&lt;p&gt;&lt;i&gt;Exercise:&lt;/i&gt; find a triangular prime.&lt;/p&gt;
&lt;p&gt;After having recovered from the shocking revelation that, our beautiful proof to the contrary, a triangular prime is so rude as to &lt;i&gt;exist&lt;/i&gt;, a little self-examination is in order.&amp;nbsp; What is wrong with the proof?&amp;nbsp; &lt;i&gt;This...&lt;/i&gt; and &lt;i&gt;not&lt;/i&gt; the existence of triangular primes... is the lesson to be learned: that beauty is not always truth.&lt;br&gt;&lt;/p&gt;&lt;div&gt;The great tragedy of Science -- the slaying of a beautiful hypothesis by an ugly fact.&lt;br&gt;&lt;/div&gt;&lt;div style="text-align: right;"&gt;-- Thomas Huxley&lt;br&gt;&lt;/div&gt;

&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8601604" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/matthew_van_eerde/archive/tags/Math/default.aspx">Math</category></item><item><title>Unsolving the Rubik's Cube</title><link>http://blogs.msdn.com/matthew_van_eerde/archive/2008/05/16/unsolving-the-rubik-s-cube.aspx</link><pubDate>Sat, 17 May 2008 01:49:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8515602</guid><dc:creator>Maurits</dc:creator><slash:comments>5</slash:comments><comments>http://blogs.msdn.com/matthew_van_eerde/comments/8515602.aspx</comments><wfw:commentRss>http://blogs.msdn.com/matthew_van_eerde/commentrss.aspx?PostID=8515602</wfw:commentRss><description>Recently I was musing on Order vs. Chaos and toying with my &lt;a href="http://en.wikipedia.org/wiki/Rubik%27s_Cube" mce_href="http://en.wikipedia.org/wiki/Rubik%27s_Cube"&gt;Rubik's Cube&lt;/a&gt;.&amp;nbsp; I wondered, as a simple exercise, whether it would be possible to strongly &lt;i&gt;un&lt;/i&gt;solve the cube.&amp;nbsp; Obviously there is a single way to put the cube into a state of greatest possible &lt;i&gt;order&lt;/i&gt;... and a multitude of ways to put it into a state of moderate to severe &lt;i&gt;chaos&lt;/i&gt;... but is there a "most chaotic" state?

&lt;p&gt;Well, I mused, the "greatest possible order" state is achieved by bringing all the orange squares &lt;i&gt;together&lt;/i&gt;... and all the blue squares &lt;i&gt;together&lt;/i&gt;... and, in general, all the squares of any given color &lt;i&gt;together&lt;/i&gt;.&amp;nbsp; How homogenous... or xenophobic.&amp;nbsp; Ew.&lt;/p&gt;

&lt;p&gt;Might it not be interesting to intermingle the colors to as great an extent as possible?&amp;nbsp; Can I put the cube in a state where &lt;i&gt;no&lt;/i&gt; two orange squares are adjacent... and no two blue squares are adjacent... and, in general, no two adjacent squares are the same color?&lt;/p&gt;

&lt;p&gt;Of course, I responded.&amp;nbsp; In fact, I already &lt;i&gt;know&lt;/i&gt; how to do that... I learned that trick before I even learned the Restore Order solution.&lt;/p&gt;

&lt;p&gt;Twelve turns later, I had a solution to the Adjacent Squares Are Different problem.&lt;/p&gt;

&lt;p&gt;&lt;i&gt;Exercise:&lt;/i&gt; what are the twelve turns?&lt;/p&gt;

&lt;p&gt;Well, that was quick.&lt;/p&gt;

&lt;p&gt;But I wanted more.&lt;/p&gt;

&lt;p&gt;Yeah, OK, no two adjacent squares are the same color, by the &lt;i&gt;usual&lt;/i&gt; definition of adjacency (the squares share a common border.)&amp;nbsp; But this is still a fairly homogenous solution... each face (of nine squares) still consists of only two colors, and there's a very high incidence of diagonally-touching squares of the same color.&amp;nbsp; Can't we diversify this &lt;i&gt;even more?&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;That took me a couple of days.&amp;nbsp; But here's the solution I came up with: &lt;br&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src="http://blogs.msdn.com/photos/matthew_van_eerde/images/8515547/original.aspx" title="Unsolved Rubik's cube" alt="Unsolved Rubik's cube" mce_src="http://blogs.msdn.com/photos/matthew_van_eerde/images/8515547/original.aspx" height="362" width="494"&gt;&lt;/p&gt;

&lt;p&gt;Note that, as desired, no two adjacent squares are the same color... even if you consider squares that touch only at a corner to be adjacent... even if that corner lies on an edge, and the two squares in question lie on different faces of the cube.&lt;/p&gt;

&lt;p&gt;The method to achieving the solution was simple in the sense that it only requires two moves (starting from a solved cube) but is probably far from optimal in the "total number of turns" sense.&lt;/p&gt;

&lt;p&gt;There are two independent steps which can be done in either order:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Flip the orientation of all twelve of the "edge pieces" (the cubelets with two visible faces)&amp;nbsp;&lt;/li&gt;

&lt;li&gt;Migrate all eight "corner pieces" (the cubelets with three visible faces) to the diametrically opposite position on the cube&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Each requires knowledge of a &lt;i&gt;single move&lt;/i&gt; and a fair amount of courage.&lt;/p&gt;

&lt;p&gt;First, some syntax.&lt;/p&gt;

&lt;p&gt;Hold the cube facing you.&amp;nbsp; I will name the six faces of the cube:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;u&gt;F&lt;/u&gt;ore (the face closest to you - in the picture above, the face with the green center)&lt;/li&gt;

&lt;li&gt;&lt;u&gt;H&lt;/u&gt;ind (in the picture above, the face with the blue center)&lt;/li&gt;

&lt;li&gt;&lt;u&gt;T&lt;/u&gt;op (in the picture above, the face with the white center)&lt;/li&gt;

&lt;li&gt;&lt;u&gt;B&lt;/u&gt;ottom (in the picture above, the face with the yellow center)&lt;/li&gt;

&lt;li&gt;&lt;u&gt;L&lt;/u&gt;eft (in the picture above, the face with the orange center)&lt;/li&gt;

&lt;li&gt;&lt;u&gt;R&lt;/u&gt;ight (in the picture above, the face with the red center)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Each face has a local definition of "clockwise"... this is the direction a clock painted on the face would turn.&lt;br&gt;&lt;/p&gt;

&lt;p&gt;Cubelet syntax&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A combination of two letters (FR) means the edge cubelet common to both faces - in this case the Fore and Right faces.&amp;nbsp; In the picture above, this is the green and white cubelet.&lt;/li&gt;

&lt;li&gt;A combination of three letters (BHL) means the corner cubelet common to all three faces - in this case the Bottom, Hind, and Left faces.&amp;nbsp; In the picture above, this is the red-green-and-white cubelet.&lt;br&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Move syntax: a letter means turn that face clockwise by 90 degrees.&amp;nbsp; A letter with a subscripted -1 means turn it &lt;i&gt;counterclockwise&lt;/i&gt; by 90 degrees.&amp;nbsp; So a move "turn the Fore face clockwise, then turn the Left face counterclockwise, then turn the Top face counterclockwise" would be written:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;F L&lt;sup&gt;-1&lt;/sup&gt; T&lt;sup&gt;-1&lt;/sup&gt;&lt;br&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Start from a solved cube.&lt;br&gt;&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Flip the orientation of all twelve of the "edge pieces"&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;Pick your favorite color - say, red - and have that be the top face.&lt;/p&gt;

&lt;p&gt;The following move flips the orientation of the LT and TH edge pieces, and also disturbs the orientation of some corner pieces:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;L&lt;sup&gt;-1&lt;/sup&gt; T&lt;sup&gt;-1&lt;/sup&gt; L&lt;sup&gt;-1&lt;/sup&gt; L&lt;sup&gt;-1&lt;/sup&gt;&lt;br&gt;F&lt;sup&gt;-1&lt;/sup&gt; L&lt;sup&gt;-1&lt;/sup&gt; F&lt;sup&gt;-1&lt;/sup&gt; F&lt;sup&gt;-1&lt;/sup&gt;&lt;br&gt;T&lt;sup&gt;-1&lt;/sup&gt; F&lt;sup&gt;-1&lt;/sup&gt; T&lt;sup&gt;-1&lt;/sup&gt; T&lt;sup&gt;-1&lt;/sup&gt;&lt;br&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;Run the move once with red as the top face.&lt;/li&gt;

&lt;li&gt;Position the cube so that red is still as the top face but the two remaining unflipped edge pieces with red on them are now in the LT and TH position.&amp;nbsp; That is, turn the whole cube 180 degrees with an axis of rotation that goes through the center of the top and bottom faces.&lt;br&gt;&lt;/li&gt;

&lt;li&gt;Run the move again.&amp;nbsp; All four edge cubelets with red on them should now be flipped.&lt;/li&gt;

&lt;li&gt;Position the cube so that red is now the &lt;i&gt;front&lt;/i&gt; face.&lt;/li&gt;

&lt;li&gt;Run the move again four more times, keeping red as the front face but rotating the cube 90 degrees each time, so each execution of the move acts on two unflipped edge pieces.&lt;br&gt;&lt;/li&gt;
&lt;/ul&gt;
All twelve of the edge pieces are now flipped.&amp;nbsp; The corner pieces are still in their original positions, though they may be oriented incorrectly. That's OK.
&lt;p&gt;&lt;b&gt;Reposition all eight corner pieces to the opposite corner&lt;/b&gt;&lt;br&gt;&lt;/p&gt;

&lt;p&gt;The following move repositions FLT to TLH, TLH to TRH, and TRH to FLT:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;L&lt;sup&gt;-1&lt;/sup&gt; T R T&lt;sup&gt;-1&lt;/sup&gt;&lt;br&gt; L T R&lt;sup&gt;-1&lt;/sup&gt; T&lt;sup&gt;-1&lt;/sup&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;As a convenience, the following mirror-image move repositions FRT to TRH, TRH to TLH, and TLH to FRT: &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;R T&lt;sup&gt;-1&lt;/sup&gt; L&lt;sup&gt;-1&lt;/sup&gt; T&lt;br&gt;R&lt;sup&gt;-1&lt;/sup&gt; T&lt;sup&gt;-1&lt;/sup&gt; L T &lt;br&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Strictly speaking, you can get away with only memorizing one of these moves - each move is equivalent to holding the cube in a different position and executing the other move twice.&lt;br&gt;&lt;/p&gt;
&lt;p&gt;Pick three faces that share a common corner - say, the faces whose center cubes are red, white, and blue.&amp;nbsp; Position the cube so it is balancing on that corner.&amp;nbsp; Note that the corner cubelets now occupy four distinct strata:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;The "top" corner.&lt;/li&gt;
&lt;li&gt;The three corners that share a common edge with the "top" corner.&lt;/li&gt;
&lt;li&gt;The three corners that share a common edge with the "bottom" corner.&lt;/li&gt;
&lt;li&gt;The "bottom" corner.&amp;nbsp;&lt;/li&gt;
&lt;/ol&gt;
This part of unsolving the cube is completed in four distinct phases:&lt;br&gt;
&lt;ol&gt;
&lt;li&gt;Unsolve the "top" corner - that is, find the red/white/blue cubelet (it's on the bottom corner) and move it to the top.&amp;nbsp; This will require two moves, which will to a certain degree randomize the positions of all of the rest of the corners.&lt;br&gt;&lt;/li&gt;
&lt;li&gt;Unsolve the three corners that share a common edge with the "top" corner, &lt;i&gt;without disturbing the cubelet in the "top" corner, or any already-unsolved cubelets in this stratum.&lt;/i&gt;&lt;/li&gt;
&lt;li&gt;Unsolve &lt;i&gt;one&lt;/i&gt; of the three corners that share a common edge with the "bottom" corner, &lt;i&gt;without disturbing any of the cubelets in the two strata above&lt;/i&gt;.&lt;/li&gt;
&lt;li&gt;Finally, there are three remaining un-unsolved cubelets.&amp;nbsp; Unsolve all three of these simultaneously - this will take precisely one execution of one of the two moves above.&lt;br&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8515602" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/matthew_van_eerde/archive/tags/Math/default.aspx">Math</category></item><item><title>Generating a sine wave with phase - answers</title><link>http://blogs.msdn.com/matthew_van_eerde/archive/2008/03/31/generate-a-sine-wave-with-phase-answers.aspx</link><pubDate>Mon, 31 Mar 2008 23:56:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8346510</guid><dc:creator>Maurits</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/matthew_van_eerde/comments/8346510.aspx</comments><wfw:commentRss>http://blogs.msdn.com/matthew_van_eerde/commentrss.aspx?PostID=8346510</wfw:commentRss><description>&lt;p&gt;In &lt;a href="http://blogs.msdn.com/matthew_van_eerde/archive/2008/01/18/generating-a-sine-wave-with-phase.aspx" mce_href="http://blogs.msdn.com/matthew_van_eerde/archive/2008/01/18/generating-a-sine-wave-with-phase.aspx"&gt;a previous post&lt;/a&gt; I posed several mathematical problems... I'd like to go back and give some answers to them.&lt;/p&gt;

&lt;p&gt;To reiterate, we take a sine wave period and wrap it around a cylinder, giving us this:&lt;br&gt;
&lt;img src="http://blogs.msdn.com/photos/matthew_van_eerde/images/7152146/500x375.aspx" mce_src="http://blogs.msdn.com/photos/matthew_van_eerde/images/7152146/500x375.aspx" alt="Sine wave wrapped around a cylinder" height="375" width="500"&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;
The problems are to:
&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Prove that the curve is planar.&lt;/li&gt;

&lt;li&gt;Prove that the curve is an ellipse.&lt;/li&gt;

&lt;li&gt;Make a &lt;a href="http://en.wikipedia.org/wiki/Johannes_Kepler" mce_href="http://en.wikipedia.org/wiki/Johannes_Kepler" target="_blank"&gt;Kepler&lt;/a&gt;ian observation.
Johannes Kepler was a mathematician/astronomer who made several observations relating to planetary orbits.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let's define the curve.  Since we're going to be making analogies to planetary orbit, let's define the curve
parametrically.  The parametric point is the analog of a planet.
&lt;/p&gt;

&lt;p style="margin: 10px;"&gt;Peter piper picked a peck of pickled peppers. &lt;i&gt;-- Mother Goose&lt;/i&gt;&lt;/p&gt;

&lt;p style="margin: 10px;"&gt;Penny Pingleton, you know you are punished.
From now on you're wearing a giant P on your blouse EVERY DAY to school
so that the whole world knows that Penny Pingleton is permanently, positively, punished.&lt;br&gt;
&lt;i&gt;-- Prudence Pingleton, &lt;a href="http://www.imdb.com/title/tt0095270/" mce_href="http://www.imdb.com/title/tt0095270/" target="_blank"&gt;Hairspray&lt;/a&gt;&lt;/i&gt;
&lt;/p&gt;

&lt;p&gt;OK, now that we have that out of the way...
I want the parametric point to sweep its way around the unit cylinder with constant angular velocity,
as viewed from above.  So if we project the path of the point into the unit cylinder with &lt;i&gt;z&lt;/i&gt; = 0, then
&lt;i&gt;r&lt;/i&gt;(&lt;i&gt;t&lt;/i&gt;) = 1 for all &lt;i&gt;t&lt;/i&gt; and &lt;i&gt;θ&lt;/i&gt;(&lt;i&gt;t&lt;/i&gt;) is linear in &lt;i&gt;t&lt;/i&gt;.  In fact let's have
&lt;i&gt;θ&lt;/i&gt;(&lt;i&gt;t&lt;/i&gt;) be &lt;i&gt;t&lt;/i&gt;, so at time &lt;i&gt;t&lt;/i&gt; = 2&lt;i&gt;π&lt;/i&gt;
our parametric point has made a complete cycle.
&lt;/p&gt;

&lt;p&gt;
Now we'll add the &lt;i&gt;z&lt;/i&gt; component in.  The curve is a wrapping of sin(&lt;i&gt;t&lt;/i&gt;), so &lt;i&gt;z&lt;/i&gt; = sin(&lt;i&gt;t&lt;/i&gt;); we aren't affecting
the vertical component of the paper by wrapping it horizontally around a cylinder.
&lt;/p&gt;

&lt;p&gt;We're actually, in a sense, done.  We have the formalized equations:
&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;i&gt;r&lt;/i&gt;(&lt;i&gt;t&lt;/i&gt;) = 1&lt;/li&gt;

&lt;li&gt;&lt;i&gt;θ&lt;/i&gt;(&lt;i&gt;t&lt;/i&gt;) = &lt;i&gt;t&lt;/i&gt;&lt;/li&gt;

&lt;li&gt;&lt;i&gt;z&lt;/i&gt;(&lt;i&gt;t&lt;/i&gt;) = sin(&lt;i&gt;t&lt;/i&gt;)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But let's convert to Cartesian
coordinates, since we'll be dealing with planes.  The relevant formulae are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;i&gt;x&lt;/i&gt;&lt;sup&gt;2&lt;/sup&gt; + &lt;i&gt;y&lt;/i&gt;&lt;sup&gt;2&lt;/sup&gt; = &lt;i&gt;r&lt;/i&gt;&lt;sup&gt;2&lt;/sup&gt;&lt;/li&gt;

&lt;li&gt;tan(&lt;i&gt;θ&lt;/i&gt;) = &lt;i&gt;y&lt;/i&gt;/&lt;i&gt;x&lt;/i&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A little intuition, checked by back-substitution, gives us these for x and y:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;i&gt;x&lt;/i&gt;(&lt;i&gt;t&lt;/i&gt;) = cos(&lt;i&gt;t&lt;/i&gt;)&lt;/li&gt;

&lt;li&gt;&lt;i&gt;y&lt;/i&gt;(&lt;i&gt;t&lt;/i&gt;) = sin(&lt;i&gt;t&lt;/i&gt;)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Putting these together with our formula for z, we have:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;i&gt;x&lt;/i&gt;(&lt;i&gt;t&lt;/i&gt;) = cos(&lt;i&gt;t&lt;/i&gt;)&lt;/li&gt;

&lt;li&gt;&lt;i&gt;y&lt;/i&gt;(&lt;i&gt;t&lt;/i&gt;) = sin(&lt;i&gt;t&lt;/i&gt;)&lt;/li&gt;

&lt;li&gt;&lt;i&gt;z&lt;/i&gt;(&lt;i&gt;t&lt;/i&gt;) = sin(&lt;i&gt;t&lt;/i&gt;)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Note that, no matter what &lt;i&gt;t&lt;/i&gt; is, &lt;i&gt;y&lt;/i&gt;(&lt;i&gt;t&lt;/i&gt;) = &lt;i&gt;z&lt;/i&gt;(&lt;i&gt;t&lt;/i&gt;)... that is, the parametric point travels entirely in
the plane given by the equation &lt;i&gt;y&lt;/i&gt; = &lt;i&gt;z&lt;/i&gt;.  This proves that the parametric path is planar.
&lt;b&gt;&lt;a href="http://en.wikipedia.org/wiki/Q.E.D." mce_href="http://en.wikipedia.org/wiki/Q.E.D." target="_blank"&gt;QED&lt;/a&gt; #1&lt;/b&gt;.
&lt;/p&gt;

&lt;p&gt;To tackle #2 we have to prove that this planar curve is, in fact, an ellipse.  One way to do that would
be to come up with &lt;i&gt;x'&lt;/i&gt; and &lt;i&gt;y'&lt;/i&gt; coordinates in the plane of the curve, transform the parametric equations into
the new coordinate system, and verify that &lt;i&gt;a&lt;/i&gt;(&lt;i&gt;x'&lt;/i&gt;)&lt;sup&gt;2&lt;/sup&gt; + &lt;i&gt;b&lt;/i&gt;(&lt;i&gt;y'&lt;/i&gt;)&lt;sup&gt;2&lt;/sup&gt; = &lt;i&gt;c&lt;/i&gt;&lt;sup&gt;2&lt;/sup&gt;
for some &lt;i&gt;a&lt;/i&gt;, &lt;i&gt;b&lt;/i&gt;, and &lt;i&gt;c&lt;/i&gt;... but when I came up
with the problem I had a much simpler proof in mind.  There's also a third way of doing this, where you
note that the curve is the result of an affine transformation of the unit circle... and an affine transformation
of a circle is demonstratably an ellipse... but I hadn't thought of that.
&lt;/p&gt;

&lt;p&gt;Another astronomer/mathematician, &lt;a href="http://en.wikipedia.org/wiki/Apollonius_of_Perga" mce_href="http://en.wikipedia.org/wiki/Apollonius_of_Perga" target="_blank"&gt;Appollonius of Perga&lt;/a&gt;, made a study of "conic sections"... that is, the intersection of a plane with
a double-headed cone.  He came up with an exhaustive list of the kinds of curves that resulted.&lt;/p&gt;

&lt;p&gt;Basic conic sections:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="http://blogs.msdn.com/photos/matthew_van_eerde/images/8346327/original.aspx" mce_href="http://blogs.msdn.com/photos/matthew_van_eerde/images/8346327/original.aspx" target="_blank"&gt;&lt;img src="http://blogs.msdn.com/photos/matthew_van_eerde/images/8346327/thumb.aspx" alt="Ellipse" mce_src="http://blogs.msdn.com/photos/matthew_van_eerde/images/8346327/thumb.aspx" height="77" width="100"&gt;&lt;/a&gt;
	An ellipse, if the angle that the plane makes is shallower than the edge of the cone;
&lt;/li&gt;

&lt;li&gt;&lt;a href="http://blogs.msdn.com/photos/matthew_van_eerde/images/8346330/original.aspx" mce_href="http://blogs.msdn.com/photos/matthew_van_eerde/images/8346330/original.aspx" target="_blank"&gt;&lt;img src="http://blogs.msdn.com/photos/matthew_van_eerde/images/8346330/thumb.aspx" alt="Hyperbola" mce_src="http://blogs.msdn.com/photos/matthew_van_eerde/images/8346330/thumb.aspx" height="77" width="100"&gt;&lt;/a&gt;
	A hyperbola, if the angle that the plane makes is steeper than the edge of the cone;
&lt;/li&gt;

&lt;li&gt;&lt;a href="http://blogs.msdn.com/photos/matthew_van_eerde/images/8346336/original.aspx" mce_href="http://blogs.msdn.com/photos/matthew_van_eerde/images/8346336/original.aspx" target="_blank"&gt;&lt;img src="http://blogs.msdn.com/photos/matthew_van_eerde/images/8346336/thumb.aspx" alt="Parabola" mce_src="http://blogs.msdn.com/photos/matthew_van_eerde/images/8346336/thumb.aspx" height="80" width="100"&gt;&lt;/a&gt;
	And a parabola, if the plane is parallel to the edge of the cone.
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For complicated reasons (to wit, that gravity diminishes as the square of the distance between two objects,)
all heavenly bodies trace either an ellipse or a hyperbola as they go around (or past) another heavenly body.&lt;br&gt;
Parabolae are theoretically possible but would smack heavily of "design"; as yet none have been observed.
&lt;/p&gt;

&lt;p&gt;For completeness, there are other "designed" ways to intersect a double-headed cone with a plane in a coincidental fashion:&lt;/p&gt;

&lt;p&gt;Degenerate conic sections:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="http://blogs.msdn.com/photos/matthew_van_eerde/images/8346319/original.aspx" mce_href="http://blogs.msdn.com/photos/matthew_van_eerde/images/8346319/original.aspx" target="_blank"&gt;&lt;img src="http://blogs.msdn.com/photos/matthew_van_eerde/images/8346319/thumb.aspx" alt="Circle" mce_src="http://blogs.msdn.com/photos/matthew_van_eerde/images/8346319/thumb.aspx" height="75" width="100"&gt;&lt;/a&gt;
	A circle - early astronomers were convinced that the heavenly bodies moved only in circles.
        It took a long time for us to believe our measurements (which showed uncircular orbits) and acknowledge that
	the orbits are in fact elliptical.  Instead, great efforts were made to show that the deviation could be made
	up of other, smaller, circular sub-orbits.  Eventually we admitted that the orbits were elliptical
	(ellipses are more complicated than circles, but much simpler than the proposed circles-within-circles)
	and not until Newton did we understand &lt;i&gt;why&lt;/i&gt; planets move in ellipses.
&lt;/li&gt;

&lt;li&gt;&lt;a href="http://blogs.msdn.com/photos/matthew_van_eerde/images/8346344/original.aspx" mce_href="http://blogs.msdn.com/photos/matthew_van_eerde/images/8346344/original.aspx" target="_blank"&gt;&lt;img src="http://blogs.msdn.com/photos/matthew_van_eerde/images/8346344/thumb.aspx" alt="Straight Line" mce_src="http://blogs.msdn.com/photos/matthew_van_eerde/images/8346344/thumb.aspx" height="75" width="100"&gt;&lt;/a&gt;
	A straight line - this is a degenerate hyperbola.  This is one solution to the "one body" problem.
&lt;/li&gt;

&lt;li&gt;&lt;a href="http://blogs.msdn.com/photos/matthew_van_eerde/images/8346332/original.aspx" mce_href="http://blogs.msdn.com/photos/matthew_van_eerde/images/8346332/original.aspx" target="_blank"&gt;&lt;img src="http://blogs.msdn.com/photos/matthew_van_eerde/images/8346332/thumb.aspx" alt="Two Straight Lines" mce_src="http://blogs.msdn.com/photos/matthew_van_eerde/images/8346332/thumb.aspx" height="83" width="100"&gt;&lt;/a&gt;
	Two intersecting straight lines - a hyperbola, as seen from infinitely far off.
&lt;/li&gt;

&lt;li&gt;&lt;a href="http://blogs.msdn.com/photos/matthew_van_eerde/images/8346342/original.aspx" mce_href="http://blogs.msdn.com/photos/matthew_van_eerde/images/8346342/original.aspx" target="_blank"&gt;&lt;img src="http://blogs.msdn.com/photos/matthew_van_eerde/images/8346342/thumb.aspx" alt="Single Point" mce_src="http://blogs.msdn.com/photos/matthew_van_eerde/images/8346342/thumb.aspx" height="77" width="100"&gt;&lt;/a&gt;
	A single point - the other solution to the "one body" problem.
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Since our curve is the intersection of a plane and a cylinder, we're done!  &lt;del&gt;&lt;b&gt;QED #2&lt;/b&gt;&lt;/del&gt;.  Well, not quite.&lt;/p&gt;

&lt;p&gt;Appolonius' results hold for cones.  We have a cylinder.  Those aren't quite the same thing.  Right?&lt;/p&gt;

&lt;p&gt;They aren't, quite... but a cylinder can be viewed as a degenerate form of cone.  To wit, &lt;i&gt;a cylinder is a cone whose
apex is infinitely far off.&lt;/i&gt;  I'll get back to a "proof" of this in a minute, but if you allow this, then we really are done.
&lt;/p&gt;

&lt;p&gt;Instead of the seven intersections of a double-headed cone and a plane, there are
only four ways a cylinder and a plane can intersect.  Two of these are new.&lt;/p&gt;

&lt;p&gt;Cylindrical conics:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="http://blogs.msdn.com/photos/matthew_van_eerde/images/8346328/original.aspx" mce_href="http://blogs.msdn.com/photos/matthew_van_eerde/images/8346328/original.aspx" target="_blank"&gt;&lt;img src="http://blogs.msdn.com/photos/matthew_van_eerde/images/8346328/thumb.aspx" alt="Ellipse in a Cylinder" mce_src="http://blogs.msdn.com/photos/matthew_van_eerde/images/8346328/thumb.aspx" height="77" width="100"&gt;&lt;/a&gt;
	An ellipse - though it remains to be proven that this really is an ellipse.
	A circle is a special form of this... I won't show it again.
&lt;/li&gt;

&lt;li&gt;&lt;a href="http://blogs.msdn.com/photos/matthew_van_eerde/images/8346345/original.aspx" mce_href="http://blogs.msdn.com/photos/matthew_van_eerde/images/8346345/original.aspx" target="_blank"&gt;&lt;img src="http://blogs.msdn.com/photos/matthew_van_eerde/images/8346345/thumb.aspx" alt="Straight Line in a Cylinder" mce_src="http://blogs.msdn.com/photos/matthew_van_eerde/images/8346345/thumb.aspx" height="79" width="100"&gt;&lt;/a&gt;
	A straight line - the single body problem again.
&lt;/li&gt;

&lt;li&gt;&lt;a href="http://blogs.msdn.com/photos/matthew_van_eerde/images/8346340/original.aspx" mce_href="http://blogs.msdn.com/photos/matthew_van_eerde/images/8346340/original.aspx" target="_blank"&gt;&lt;img src="http://blogs.msdn.com/photos/matthew_van_eerde/images/8346340/thumb.aspx" alt="Parallel Lines in a Cylinder" mce_src="http://blogs.msdn.com/photos/matthew_van_eerde/images/8346340/thumb.aspx" height="83" width="100"&gt;&lt;/a&gt;
	Two straight lines - parallel this time.  This is something new.
&lt;/li&gt;

&lt;li&gt;&lt;a href="http://blogs.msdn.com/photos/matthew_van_eerde/images/8346334/original.aspx" mce_href="http://blogs.msdn.com/photos/matthew_van_eerde/images/8346334/original.aspx" target="_blank"&gt;&lt;img src="http://blogs.msdn.com/photos/matthew_van_eerde/images/8346334/thumb.aspx" alt="Empty Intersection with a Cylinder" mce_src="http://blogs.msdn.com/photos/matthew_van_eerde/images/8346334/thumb.aspx" height="78" width="100"&gt;&lt;/a&gt;
	No intersection at all.  The &lt;a href="http://en.wikipedia.org/wiki/K%C5%8Dan" target="_blank" mce_href="http://en.wikipedia.org/wiki/K%C5%8Dan"&gt;kōan&lt;/a&gt; of conics.
	This is new, but whether it is something is a question I will not attempt to address.
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now for the proof that cylindrical intersection #1 really is an ellipse.  Place two double-headed cones
coaxially with the cylinder.  Arrange them so that:
&lt;/p&gt;

&lt;ul&gt;
	
&lt;li&gt;The apexes of both cones are in the "up" direction, from anywhere on the curve in question.&lt;/li&gt;
	
&lt;li&gt;The "top" cone intersects the highest point on the curve in question.&lt;/li&gt;
	
&lt;li&gt;The "bottom" cone intersects the lowest point on the curve in question.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;
Diagram: &lt;br&gt;
&lt;img src="http://blogs.msdn.com/photos/matthew_van_eerde/images/8346851/original.aspx" mce_src="http://blogs.msdn.com/photos/matthew_van_eerde/images/8346851/original.aspx" alt="Auxiliary cones" height="443" width="236"&gt;
&lt;/p&gt;

&lt;p&gt;Notice that the entire curve lies in between the two cones.  Consider the intersection of the plane that
determines the curve with each of the cones.  We know both of these are ellipses; we know that the intersection
of the plane with the "upper" cone lies wholly outside our unknown curve; and we know that the intersection of
the plane with the "inner" cone lies wholly inside our unknown curve.
&lt;/p&gt;

&lt;p&gt;
Diagram: &lt;br&gt;
&lt;img src="http://blogs.msdn.com/photos/matthew_van_eerde/images/8346853/original.aspx" mce_src="http://blogs.msdn.com/photos/matthew_van_eerde/images/8346853/original.aspx" alt="Auxiliary cones - cross section" height="398" width="490"&gt;
&lt;/p&gt;

&lt;p&gt;
The final stage of the proof lies in pushing the apexes of both cones to infinity, which "squeezes" our unknown curve
in between two known ellipses.  Since the horizontal distance between the cones goes to zero as 2&lt;sup&gt;3/2&lt;/sup&gt; / tan(&lt;i&gt;θ&lt;/i&gt;), our
unknown curve cannot help but be elliptical-ish... &lt;i&gt;to any degree of precision&lt;/i&gt;... and it is, thus, an ellipse.  &lt;b&gt;QED #2&lt;/b&gt;.
&lt;/p&gt;

&lt;p&gt;For the final problem, we must make a "Keplerian observation."  The observation in question is that our parametric point,
like the planets, sweeps out &lt;a href="http://en.wikipedia.org/wiki/Kepler%27s_laws_of_planetary_motion#Second_law" mce_href="http://en.wikipedia.org/wiki/Kepler%27s_laws_of_planetary_motion#Second_law" target="_blank"&gt;equal areas in equal times&lt;/a&gt;.  What makes this interesting is that the parametric point does &lt;i&gt;not&lt;/i&gt; move in the same way
that a planet does... so it's a little odd that such a similar result should hold... but it does.&lt;/p&gt;

&lt;p&gt;Let's talk a little about Kepler's second law.  A planet moves around the sun in an elliptical orbit.  Fine.
The sun lies at one of the foci of the ellipse.  An imaginary line between the planet and the sun will sweep out area at a constant rate.
That is to say, when the planet is far from the sun, that line will be long... and it will move correspondingly slowly.
When the planet is near to the sun, that line will be short... and it will move correspondingly quickly.
&lt;br&gt;
&lt;img src="http://blogs.msdn.com/photos/matthew_van_eerde/images/8346930/original.aspx" mce_src="http://blogs.msdn.com/photos/matthew_van_eerde/images/8346930/original.aspx" alt="Planetary orbit" height="188" width="387"&gt;
&lt;/p&gt;

&lt;p&gt;Conversely, our parametric point sweeps around the origin at &lt;i&gt;constant&lt;/i&gt; angular velocity.  So this is trivially
"equal areas in equal times".  Right?
&lt;/p&gt;

&lt;p&gt;Not quite.  It's true that our parametric point &lt;i&gt;appears&lt;/i&gt; to have constant angular velocity, &lt;i&gt;if you project
its path into the x-y plane&lt;/i&gt;... that is, if you view it from directly above, from a point on the z-axis.&lt;/p&gt;

&lt;p&gt;But that's a silly way to look at things.  To get an idea of the point's &lt;i&gt;actual&lt;/i&gt; motion, it's far more natural
to view it from an axis orthogonal to the plane of motion.  So let's put an observer (Aranea) directly above the point, and
another observer (Nellie) in the more natural location.&lt;br&gt;
&lt;img src="http://blogs.msdn.com/photos/matthew_van_eerde/images/8346936/original.aspx" mce_src="http://blogs.msdn.com/photos/matthew_van_eerde/images/8346936/original.aspx" alt="Aranea and Nellie" height="445" width="435"&gt;
&lt;/p&gt;

&lt;p&gt;From Aranea's point of view, the motion is a simple constant-speed trip around a circle.  But Nellie sees the parametric
point's true velocity.  The parametric point's velocity has two components... a constant "clockwise" component in the
x-y plane, and a variable orthogonal component going "up" or "down".  This is maximized when the curve crosses the x-y plane,
and minimized when the curve is topping or bottoming out.  So Nellie sees this:&lt;br&gt;
&lt;img src="http://blogs.msdn.com/photos/matthew_van_eerde/images/8346932/original.aspx" mce_src="http://blogs.msdn.com/photos/matthew_van_eerde/images/8346932/original.aspx" alt="Parametric point" height="188" width="387"&gt;
&lt;/p&gt;

&lt;p&gt;It seems hard to believe, given that the parametric point is speeding up and slowing down according to different rules...
and the notion of "center" is different... but the fact is, the parametric point &lt;i&gt;also&lt;/i&gt; sweeps out equal areas in equal times!
&lt;/p&gt;

&lt;p&gt;To see this most easily, look at Aranea's point of view again.  Have Aranea draw a grid on her picture of the system.
She can count the area swept out in any given time by counting the number of squares.&lt;/p&gt;

&lt;p&gt;Now consider what those squares look like to Nellie.  They're rectangles.  &lt;i&gt;But they're all of equal area.&lt;/i&gt;
Aranea and Nellie can label the squares according to some naming scheme, and will agree that the same set of squares/rectangles were
swept out... but this means, since the rectangles are of equal area, that even in Nellie's point of view, equal areas are
swept out in equal times! &lt;b&gt;QED #3.&lt;/b&gt;
&lt;/p&gt;
&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8346510" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/matthew_van_eerde/archive/tags/Math/default.aspx">Math</category><category domain="http://blogs.msdn.com/matthew_van_eerde/archive/tags/Audio/default.aspx">Audio</category><category domain="http://blogs.msdn.com/matthew_van_eerde/archive/tags/Win32/default.aspx">Win32</category></item><item><title>How it works: xkcd sucks at math</title><link>http://blogs.msdn.com/matthew_van_eerde/archive/2008/02/28/how-it-works-xkcd-sucks-at-math.aspx</link><pubDate>Thu, 28 Feb 2008 20:55:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7936729</guid><dc:creator>Maurits</dc:creator><slash:comments>6</slash:comments><comments>http://blogs.msdn.com/matthew_van_eerde/comments/7936729.aspx</comments><wfw:commentRss>http://blogs.msdn.com/matthew_van_eerde/commentrss.aspx?PostID=7936729</wfw:commentRss><description>&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;I'm a casual reader of Randall Munroe's &lt;a href="http://www.xkcd.com/" mce_href="http://www.xkcd.com/"&gt;xkcd&lt;/a&gt; web comic.&amp;nbsp; I'm partial especially to the artistic episodes.&lt;/p&gt;

&lt;p&gt;I was reading the "&lt;a href="http://www.xkcd.com/385/" mce_href="http://www.xkcd.com/385/"&gt;How it Works&lt;/a&gt;" episode, reproduced here:&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;img src="http://imgs.xkcd.com/comics/how_it_works.png" title="It's pi plus C, of course." alt="How it Works"&gt;&lt;/p&gt;

&lt;p&gt;Fine, a noble sentiment (women in the hard sciences are unfairly targeted) and well executed.&lt;/p&gt;

&lt;p&gt;As is my wont, I then went for the extra bit of juiciness to the episode by hovering over the image, and what do I find?&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;It's pi plus C, of course. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;... OK, fine.&amp;nbsp; A good reference to &lt;a href="http://www.princeton.edu/%7Emissouri/humor6.html" mce_href="http://www.princeton.edu/~missouri/humor6.html"&gt;an in-joke&lt;/a&gt; in the mathematical community.&amp;nbsp; Well done.&lt;/p&gt;

&lt;p&gt;... except I can't stomach the idea of "π plus a constant."&amp;nbsp; It just doesn't sit right.&amp;nbsp; And there's something wrong about the equation on the board.&amp;nbsp; It's missing something... else.&lt;/p&gt;

&lt;p&gt;The equation as it appears on the board:&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;img src="http://blogs.msdn.com/photos/matthew_van_eerde/images/7936591/original.aspx" alt="S(x^2) = pi"&gt;&lt;/p&gt;

&lt;p&gt;... and the suggested improvement:&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;img src="http://blogs.msdn.com/photos/matthew_van_eerde/images/7936601/original.aspx" alt="S(x^2) = pi + C"&gt;&lt;/p&gt;

&lt;p&gt;Yeah.&amp;nbsp; I still don't buy it. &lt;br&gt;&lt;/p&gt;

&lt;p&gt;Under the premise that the best way to spoil a good joke is to analyze it, let's see if we can figure out what our characters are trying to do with this integral, shall we?&lt;/p&gt;

&lt;p&gt;Well, there are integrals and integrals.&amp;nbsp; There are at least three common uses of integrals:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Find the antiderivative of a given function for later use.&lt;br&gt;&lt;/li&gt;

&lt;li&gt;Evaluate the antiderivative at upper and lower limits and subtract to find the area under the curve.&lt;/li&gt;

&lt;li&gt;Do a contour integration to evaluate a function over a known region.&lt;br&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The "plus a constant" comment applies exclusively to the first use.&amp;nbsp; The function &lt;i&gt;y&lt;/i&gt; = &lt;i&gt;x&lt;/i&gt;&lt;sup&gt;2&lt;/sup&gt; is analytic everywhere and so is not likely to be interesting for a contour integral.&amp;nbsp; But the second use seems to be very appropriate to this "integral equals a constant" equation:&lt;/p&gt;

&lt;p&gt;The first thing to do is find the antiderivative.&amp;nbsp; This can occasionally be very difficult, but in this case we have a textbook function which is the first recipe anyone memorizes:&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;img src="http://blogs.msdn.com/photos/matthew_van_eerde/images/7936604/original.aspx" alt="S(x^p)dx = x^(p + 1) / (p + 1) + C"&gt;&lt;/p&gt;

&lt;p&gt;The above formula holds for all &lt;i&gt;p&lt;/i&gt; except for &lt;i&gt;p&lt;/i&gt; = -1.&amp;nbsp;&amp;nbsp; That one's a little tricky because the denominator above would be 0.&amp;nbsp; For completeness, here's the case with &lt;i&gt;p&lt;/i&gt; = -1:&lt;br&gt;&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;img src="http://blogs.msdn.com/photos/matthew_van_eerde/images/7936586/original.aspx" alt="S(1/x)dx = ln x + C"&gt;&lt;/p&gt;

&lt;p&gt;A little faith is required if you haven't seen the derivation, but it works out.&amp;nbsp; (This is as good a definition as any of ln &lt;i&gt;x&lt;/i&gt;, FWIW.) &lt;/p&gt;

&lt;p&gt;Indeed, the antiderivative is a whole family of functions which differ only by a constant.&amp;nbsp; But if we're going to evaluate it at certain limits, we need to (eventually) know what the limits are.&amp;nbsp; Let's call them &lt;i&gt;a&lt;/i&gt; and &lt;i&gt;b&lt;/i&gt; for now:&lt;/p&gt;

&lt;p&gt;&lt;img src="http://blogs.msdn.com/photos/matthew_van_eerde/images/7936578/original.aspx" alt="S(x^p)dx:a-&amp;gt;b = (b^(p+1) - a^(p+1)) / (p+1)"&gt;&lt;/p&gt;

&lt;p&gt;(Note in particular that the evaluation doesn't depend on which of the family of functions we chose... &lt;i&gt;C&lt;/i&gt; cancels out.&amp;nbsp;&amp;nbsp; This is my problem with the comment.)&lt;br&gt;&lt;/p&gt;&lt;p&gt;Setting &lt;i&gt;p&lt;/i&gt; = 2 we still have a single equation in two unknowns (&lt;i&gt;a&lt;/i&gt; and &lt;i&gt;b&lt;/i&gt;).&amp;nbsp; Still need more information.&lt;/p&gt;

&lt;p&gt;Well, let's think about it for a minute.&amp;nbsp; Many functions have "natural" points of evaluation.&amp;nbsp; For &lt;i&gt;x&lt;/i&gt;&lt;sup&gt;2&lt;/sup&gt; this is 0.&amp;nbsp; For 1/&lt;i&gt;x&lt;/i&gt; it's 1.&amp;nbsp;&amp;nbsp; What if we set &lt;i&gt;a&lt;/i&gt; = 0?&amp;nbsp; The natural-ness of 0 as a base point of evaluation is made clear from a graph of &lt;i&gt;y&lt;/i&gt; = &lt;i&gt;x&lt;/i&gt;&lt;sup&gt;2&lt;/sup&gt;:&lt;br&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src="http://blogs.msdn.com/photos/matthew_van_eerde/images/7936589/original.aspx" alt="Graph" of="" y="x^2" shaded="" from="" 0="" to="" 2.1-ish,="" area="pi&amp;quot;"&gt;&lt;/p&gt;
&lt;p&gt;If we set &lt;i&gt;p&lt;/i&gt; = 2 &lt;b&gt;and&lt;/b&gt; &lt;i&gt;a&lt;/i&gt; = 0, then the equation above reduces to the solvable problem:&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;img src="http://blogs.msdn.com/photos/matthew_van_eerde/images/7936607/original.aspx" alt="(1/3)b^3 = pi"&gt;&lt;/p&gt;

&lt;p&gt;The solution is now trivial:&lt;/p&gt;

&lt;p&gt;&lt;img src="http://blogs.msdn.com/photos/matthew_van_eerde/images/7936541/original.aspx" alt="b = (3 pi)^(1/3) = 2.112307-ish"&gt;&lt;/p&gt;

&lt;p&gt;So we can complete the original blackboard equation as follows:&lt;/p&gt;

&lt;p&gt;&lt;img src="http://blogs.msdn.com/photos/matthew_van_eerde/images/7936611/original.aspx" alt="S(x^2)dx:0-&amp;gt;(3 pi)^(1/3) = pi"&gt;&lt;/p&gt;

&lt;p&gt;or in fuller generality as&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;img src="http://blogs.msdn.com/photos/matthew_van_eerde/images/7936576/original.aspx" alt="S(x^2)dx:a-&amp;gt;(3 pi + a^3)^(1/3) = pi"&gt;&lt;/p&gt;

&lt;p&gt;for any &lt;i&gt;a&lt;/i&gt;. &lt;/p&gt;&lt;p&gt;(I'm sticking with the &lt;i&gt;a&lt;/i&gt; = 0 solution, myself.) &lt;br&gt;&lt;/p&gt;
&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=7936729" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/matthew_van_eerde/archive/tags/Math/default.aspx">Math</category></item><item><title>Generating a sine wave with phase</title><link>http://blogs.msdn.com/matthew_van_eerde/archive/2008/01/18/generating-a-sine-wave-with-phase.aspx</link><pubDate>Fri, 18 Jan 2008 22:18:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7152444</guid><dc:creator>Maurits</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/matthew_van_eerde/comments/7152444.aspx</comments><wfw:commentRss>http://blogs.msdn.com/matthew_van_eerde/commentrss.aspx?PostID=7152444</wfw:commentRss><description>&lt;p&gt;I alluded to a bug in &lt;a href="http://blogs.msdn.com/matthew_van_eerde/archive/2008/01/08/generating-a-sine-wave.aspx" mce_href="http://blogs.msdn.com/matthew_van_eerde/archive/2008/01/08/generating-a-sine-wave.aspx"&gt;my last post&lt;/a&gt; but didn't explicitly say what it was.&amp;nbsp; Hint: it's all about phase.&lt;/p&gt;

&lt;p&gt;So let's talk about phase for a minute.&amp;nbsp; Sine waves are periodic... although they are frequently plotted over very large intervals, their natural domain is a single period.&lt;br&gt;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;
&lt;img src="http://blogs.msdn.com/photos/matthew_van_eerde/images/7152130/500x130.aspx" alt="2-D sine wave" mce_src="http://blogs.msdn.com/photos/matthew_van_eerde/images/7152130/500x130.aspx" height="130" width="500"&gt; &lt;/p&gt;

&lt;p&gt;(&lt;a href="http://blogs.msdn.com/photos/matthew_van_eerde/images/7152130/original.aspx" mce_href="http://blogs.msdn.com/photos/matthew_van_eerde/images/7152130/original.aspx"&gt;original&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;&lt;i&gt;Exercise&lt;/i&gt; - The original version, when printed, makes a nice armband.&amp;nbsp;&lt;/p&gt;

&lt;p&gt;&lt;i&gt;Advanced Exercise&lt;/i&gt; - generate a one-and-a-half period sine wave, print it on both sides of a piece of paper, and make a &lt;a href="http://en.wikipedia.org/wiki/M%C3%B6bius_strip" mce_href="http://en.wikipedia.org/wiki/M%C3%B6bius_strip"&gt;Möbius strip&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Well, that's a step, but it's not really getting at the nature of the sine wave.&amp;nbsp; Yes, we notice that the beginning value (0) is the same as the ending value... and the beginning slope (+1) is the same as the ending slope... and there is, perhaps, a certain smoothness beyond even slope (in fact, all derivatives agree at the crossover point...)&lt;br&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="http://blogs.msdn.com/matthew_van_eerde/archive/2007/12/18/if-you-see-a-fact-try-to-see-it-as-intuitively-as-possible.aspx" mce_href="http://blogs.msdn.com/matthew_van_eerde/archive/2007/12/18/if-you-see-a-fact-try-to-see-it-as-intuitively-as-possible.aspx"&gt;If you see a fact, try to see it as intuitively as possible.&lt;/a&gt;&lt;br&gt;&lt;br&gt;Do the first exercise above - print out the original image, cut it out along the lines, and make a cylinder by connecting the short ends together.&amp;nbsp; Rotate the cylinder through various orientations, paying particular attention to the location of the sine wave at all times.&lt;br&gt;&lt;/p&gt;

&lt;p&gt;What you get is hard to render in a two-dimensional blog post, but here's a stab at it (Matlab script below:)&lt;br&gt;&lt;/p&gt;
&lt;img src="http://blogs.msdn.com/photos/matthew_van_eerde/images/7152146/500x375.aspx" alt="3-D sine wave" mce_src="http://blogs.msdn.com/photos/matthew_van_eerde/images/7152146/500x375.aspx" height="375" width="500"&gt; 
&lt;p&gt;(&lt;a href="http://blogs.msdn.com/photos/matthew_van_eerde/images/7152146/original.aspx" mce_href="http://blogs.msdn.com/photos/matthew_van_eerde/images/7152146/original.aspx"&gt;original&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;Notice that this representation of the sine wave is an ellipse.&lt;/p&gt;

&lt;p&gt;&lt;span style="font-style: italic;"&gt;Exercise&lt;/span&gt; - Prove that the red line in the above graph is planar (Hint: you can type the equation of the plane in three keystrokes.)&lt;br&gt;&lt;/p&gt;

&lt;p&gt;&lt;span style="font-style: italic;"&gt;Advanced Exercise&lt;/span&gt; - Prove that it's an ellipse.&amp;nbsp;&lt;/p&gt;

&lt;p&gt;&lt;i&gt;Advanced Exercise&lt;/i&gt; - Make a &lt;a href="http://en.wikipedia.org/wiki/Johannes_Kepler" mce_href="http://en.wikipedia.org/wiki/Johannes_Kepler"&gt;Kepler&lt;/a&gt;ian observation. (Hint: note the angles at which the red line crosses the dotted black lines.)&lt;br&gt;&lt;/p&gt;

&lt;p&gt;So what does this have to do with phase?&lt;/p&gt;

&lt;p&gt;Everything.&lt;/p&gt;

&lt;p&gt;There's another hint in the documentation of &lt;a href="http://msdn2.microsoft.com/en-us/library/wkbss70y%28VS.80%29.aspx" mce_href="http://msdn2.microsoft.com/en-us/library/wkbss70y%28VS.80%29.aspx"&gt;double sin(double)&lt;/a&gt;, which I also linked in my last post:&lt;/p&gt;

&lt;blockquote&gt;&lt;b&gt;sin&lt;/b&gt; returns the sine of &lt;i&gt;&lt;span class="parameter"&gt;x&lt;/span&gt;&lt;/i&gt; [phase]. &lt;i&gt;If &lt;/i&gt;&lt;span class="parameter"&gt;x [phase]&lt;/span&gt;&lt;i&gt; is greater than or equal to 263, or less than or equal to –263, a loss of significance in the result occurs. &lt;/i&gt;(my emphasis)&lt;i&gt;&lt;br&gt;&lt;/i&gt;&lt;/blockquote&gt;

&lt;p&gt;A final hint: again from my last post, "as a side effect, we get a kinda-sorta implementation of &lt;span class="Unicode"&gt;&lt;i&gt;ϕ &lt;/i&gt;[phase]&lt;i&gt;."&lt;/i&gt;&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;When you find yourself with a kinda-sorta implementation of something, that is occasionally a hint from above that your life will be easier if you do some self-examination... perhaps a full-blown implementation of that thing will be (a) easier and (b) more what you actually want.&lt;/p&gt;

&lt;p&gt;No more hints.&amp;nbsp; If you want to figure it out for yourself, &lt;i&gt;stop reading now&lt;/i&gt;.&lt;/p&gt;

&lt;p&gt;OK, here's the scoop.&amp;nbsp; The thing you feed to sin(...) is a radian measurement.&amp;nbsp; This is naturally between 0 and 2&lt;i&gt;π&lt;/i&gt; (or, perhaps, -&lt;i&gt;π&lt;/i&gt; to &lt;i&gt;π&lt;/i&gt; if you prefer.)&amp;nbsp; You &lt;i&gt;can&lt;/i&gt; feed it things that are out of this range... say, sin(100.0)... but this is a little out of the natural and there are consequences.&lt;/p&gt;

&lt;p&gt;double-type numbers, and floating point numbers in general, can represent very small numbers to a very high degree of precision... and they can represent very large numbers with the same &lt;i&gt;relative&lt;/i&gt; degree of precision.&amp;nbsp; See &lt;a href="http://en.wikipedia.org/wiki/IEEE754-1985" mce_href="http://en.wikipedia.org/wiki/IEEE754-1985"&gt;IEEE754&lt;/a&gt; for gory details.&lt;/p&gt;

&lt;p&gt;An illustration is in order.&amp;nbsp; Richard Feynman, celebrity physicist, tells a story of his youth.&amp;nbsp; He claimed to be able to solve, in one minute, to within 10% accuracy, any problem that can be stated in ten seconds.&lt;br&gt;&lt;br&gt;He did pretty well until someone came up with "tan(10&lt;sup&gt;100&lt;/sup&gt;)".&lt;/p&gt;

&lt;p&gt;If you feed bigger and bigger numbers to sin(...), eventually it starts giving you sloppy answers.&amp;nbsp; It has to.&amp;nbsp; It's not getting enough information to give you a good answer.&lt;/p&gt;

&lt;p&gt;I was able to test the tone generation function and measure the signal-to-noise ratio of the tones it was producing after varying lengths of sample time.&amp;nbsp; It started off very well, but after very long times (days, weeks, years) the SNR would get slowly compromised until, after millions of years, the signal disappeared.&lt;/p&gt;

&lt;p&gt;&lt;i&gt;That is not the bug.&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;But it is a serious clue to the bug.&lt;/p&gt;

&lt;p&gt;If the worst-case scenario for an implementation is that the signal will depreciate over a million years, ship it.&lt;/p&gt;

&lt;p&gt;Take your cylinder that you made above.&amp;nbsp; Place it in front of you so that the taped-together part is directly in front of your nose, and the part to the right has the red line going up.&amp;nbsp; This is a sine wave.&lt;/p&gt;

&lt;p&gt;Rotate it clockwise 90 degrees, so the part in front of your nose has the red line brushing the top.&amp;nbsp; Now it is a cosine wave.&lt;/p&gt;

&lt;p&gt;Rotate it counter-clockwise 60 degrees, so the part in front of your nose has the red line crossing the dotted line.&amp;nbsp; Now it is a sine wave with a known phase (30 degrees.)&lt;br&gt;&lt;/p&gt;

&lt;p&gt;In fact, rotate it any angle (say, &lt;i&gt;&lt;span class="Unicode"&gt;ϕ&lt;/span&gt;&lt;/i&gt;&lt;span class="Unicode"&gt;) and it's a sine wave with known phase &lt;/span&gt;&lt;i&gt;&lt;span class="Unicode"&gt;ϕ&lt;/span&gt;&lt;/i&gt;&lt;span class="Unicode"&gt;.&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;We can't do that with our tone generator.&amp;nbsp;&lt;/p&gt;

&lt;p&gt;&lt;span style="font-style: italic;"&gt;That is the bug.&lt;/span&gt;&amp;nbsp; We're implementing a tweaked version of phase which has a forgivable side effect, but which is not a full implementation of phase.&lt;/p&gt;

&lt;p&gt;Why do we care about implementing phase?&lt;/p&gt;

&lt;p&gt;Well, for example, one problem I sometimes have when testing audio fidelity is I connect the stereo cables to the Audio Precision equipment the wrong way (left-to-right and right-to-left.)&amp;nbsp; If I could generate the test tones out of phase by 90 degrees, with the left channel in front of the right channel, I could take a phase measurement and log a "you have the cables backwards" error.&amp;nbsp; I send different frequencies over the cables, so the ifFrameOffset parameter is of little use here.&lt;/p&gt;

&lt;p&gt;So what's the improved implementation?&lt;/p&gt;

&lt;p&gt;Instead of&lt;/p&gt;
&lt;div style="font-family: monospace;"&gt;
&lt;p&gt;// given a buffer, fills it with sine wave data&lt;br&gt;HRESULT FillBufferWithSine(&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; LPCWAVEFORMATEX pWfx, // includes sample rate&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; double lfAmplitude,&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; double lfFrequency,&lt;br&gt;&lt;b&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; UINT64 ifFrameOffset,&lt;br&gt;&lt;/b&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; PBYTE pBuffer,&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; UINT32 cbBytes&lt;br&gt;);&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;do&lt;/p&gt;
&lt;div style="font-family: monospace;"&gt;

&lt;p&gt;// given a buffer, fills it with sine wave data&lt;br&gt;HRESULT 
FillBufferWithSine(&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; LPCWAVEFORMATEX pWfx, // includes sample rate&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; double lfAmplitude,&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; double lfFrequency,&lt;br&gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // in/out&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; //&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; On input, initial phase (0 to 2pi).&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; //&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; On output, phase of next sample.&lt;br&gt;
&lt;span style="font-weight: bold;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; double *plfPhase,&lt;/span&gt;&lt;br style="font-weight: bold;"&gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; PBYTE pBuffer, // out - byte count is cbBytes&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; UINT32 cbBytes&lt;br&gt;);&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;Now, if you want to generate a lot of contiguous sine tone buffers, you do something like this:&lt;/p&gt;
&lt;div style="font-family: monospace;"&gt;












&lt;p&gt;double lfPhase = 0.0; // start at 0... or somewhere else&lt;br&gt;while (...) {&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // ... &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // FillBufferWithSine takes care of updating lfPhase as necessary &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; hr = FillBufferWithSine(pWfx, lfAmp, lfFreq, &amp;amp;lfPhase, pBuffer, cbBytes);&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // ... &lt;br&gt;}&lt;br&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;As a side effect of this new phase feature, the quality degradation non-issue goes away.&amp;nbsp; So the generated tones will sound good &lt;i&gt;forever&lt;/i&gt;... which is kind of neat... but not the motivation for this fix.&lt;br&gt;&lt;/p&gt;

&lt;p&gt;Why did this happen?&lt;/p&gt;

&lt;p&gt;Because there are many ways to measure time.&amp;nbsp; You can count samples... you can count milliseconds... you can count hundred-nanosecond intervals... you can count CPU flops... you can do so many different things.&amp;nbsp; Some are more natural than others in different contexts.&lt;/p&gt;

&lt;p&gt;The reason this happened is because a time measure that was very natural in one context (counting frames: playing an audio stream) was somewhat artificially shoved into another context (generating a sine wave) where there was another, more natural context (changing phase.)&amp;nbsp; Alas, context switching is a frequent source of bugs.&lt;br&gt;&lt;br&gt;&lt;/p&gt;

&lt;hr style="width: 50%;"&gt;
&lt;p&gt;Matlab script to generate the three-dimensional rendering of a sine wave:&lt;/p&gt;
&lt;div style="font-family: monospace;"&gt;t = 0:pi/50:2*pi;&lt;br&gt;h = -1:0.5:1;&lt;br&gt;z = ones(size(t))' * h;&lt;br&gt;&lt;br&gt;sinx = cos(t);&lt;br&gt;siny = sin(t);&lt;br&gt;sinz = sin(t);&lt;br&gt;&lt;br&gt;plot3( ...&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; sinx, siny, z, 'k:', ...&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; sinx, siny, sinz, 'r' ...&lt;br&gt;);&lt;br&gt;&lt;br&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=7152444" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/matthew_van_eerde/archive/tags/Math/default.aspx">Math</category><category domain="http://blogs.msdn.com/matthew_van_eerde/archive/tags/Audio/default.aspx">Audio</category><category domain="http://blogs.msdn.com/matthew_van_eerde/archive/tags/Win32/default.aspx">Win32</category></item></channel></rss>