March 14, 2010

Python pi.py Spigot

Here is pi.py (not to be confused with pypy), a short python implementation of Jeremy Gibbons's 2005 spigot generator for decimal digits of pi:

def pi_decimal_digits():
  q, r, t, j = 1, 180, 60, 2
  while True:
    u, y = 3*(3*j+1)*(3*j+2), (q*(27*j-12)+5*r)//(5*t)
    yield y
    q, r, t, j = 10*q*j*(2*j-1), 10*u*(q*(5*j-2)+r-y*t), t*u, j+1

The code to drive the algorithm is longer than the algorithm:

count, digits = 0, pi_decimal_digits()
while 1:
  print '%6d: %s' % (
    count, ''.join([str(digits.next()) for j in xrange(50)]))
  count += 50

This little script can be quite handy if, for example, you want to look up where in the digits of pi you can find the phone number for the Mass Audubon Society that is down the road from my house:

> ./pi.py | grep --color 2599661
  3050: 56638937787083039069792077346722182562599661501421

Conveniently, their phone number starts at the very visible 3087th digit of pi after the decimal point. My home phone number is a little bit further into the private section the pi phone book, at the 6,301,261th digit. And, if you must, you can reach me at my cell at the 22,000,091th digit, although I should warn you that my reception is not very good out here in the boonies.

Happy pi day.


Posted by David at 08:35 AM | Comments (1)

The Mystery of 355/113

Today's date is a good excuse to memorize a few more digits of 3.1415926535897932384626433832795....

And yet decimal approximations to pi are an artifact of our ten-fingered anatomy. Fractional approximations to pi are more satisfying, and they promise to teach us something more universal about pi.

We all know that 22/7 is a very good approximation to pi. But this well-known fraction is is actually 1/791 larger than a slightly less-well-known but much more mysterious rational approximation for pi:

.

Remembering 355/113

The fraction 355/113 is incredibly close to pi, within a third of a millionth of the exact value. This level of accuracy is far beyond its rights as a fraction with such a small denominator, and it causes various oddities elsewhere in math. For example, use any scientific calculator to compute cos(355) in radians. The oddball result is due to the freakish closeness of 355/113 to pi.

A cute mnemonic makes it easy for our base-10 species to remember this useful fraction. Write down the first three odd numbers twice: 1 1 3 3 5 5. Then divide the decimal number represented by the last 3 digits by the decimal number given by the first three digits.

The mystery is: why is this fraction so close to pi? The deeper you look, the more unique and unexplained 355/113 appears to be.

Continue reading "The Mystery of 355/113"
Posted by David at 04:03 AM | Comments (0)

March 11, 2010

It's Not About The Answer

Yesterday my son came across this beautiful problem set from a packet about combinatorics by Lexington teacher Vaag Mosca. Try it.

When working the set, you will realize very quickly that all the answers are the same. You could easily "finish" the problems by copying the same answer in each slot. But that would be totally unsatisfying.

When Anthony worked the problems and got some different answers, he was puzzled. "I know I got it wrong. What am I missing?" I have never seen Anthony work so hard to double-check his work.

Even better, after he got the right answer several times, he felt compelled to understand why it was the same. "I got it, and I see how it is related to seven, but where is the three?" He was really taking a new approach to math.

What you learn from this little problem set goes way beyond combinatorics:

The lesson is that math is not about the answer.


Posted by David at 07:23 AM | Comments (0)

March 08, 2010

Solid Geometry

Three dimensional solid geometry has nearly been written out of the American mathematical curriculum. Yet my middle-school math team is approaching the last and most difficult math meet of the year where, among other things, they are expected to know basic solid geometry.

So for the 3d-unaware student I have put together a super-compressed introduction to the basics of middle-school solid geometry, with a high level of challenge for intended for math team middle schoolers who are trying to learn everything they need to know in a week. The packet here has only 4 pages and 27 problems, with an extra 6 pages for solutions to check after you are done. The easy bits are skipped, and in exchange you get to see some common types of tricks as well as a hint of insight beyond basic prisms, pyramids, cones and spheres.

Here it is: Solid Geometry Facts and 33 Problems and solutions.


Posted by David at 01:10 AM | Comments (0)

March 04, 2010

Teaching is Hard

I coach a middle-school math team of 5th-8th graders, and I have learned that teaching is hard. My kids are all good kids, but after a long day at school, the girls want to chit chat and the boys want to wrestle. When it comes to learning hard math, the whizzes like to dominate the discussion, and the kids who are getting lost would prefer to literally hide under their desk.

Sometimes math team practices are disastrous, and I just want to let all my wild gazelles outside to play in the sun. And yet other times the classroom is exciting and magical and packed with learning, with the kids buzzing about primes or permutations by the end of the hour. It's not just the people in the room: we are the same every week.

But every week is different. What is happening?

Continue reading "Teaching is Hard"
Posted by David at 09:53 AM | Comments (0)

February 12, 2010

A Mathematical Notation Question

Quickly simplify the following expression: and show your work.

Solution:

Real solution:

The real question is, how the heck do you write all that notation in HTML? This problem is the bane of math teachers everywhere. But I did it here in about 5 minutes using a wonderful new tool that was released yesterday. Here is how.

The solution is to use mimeTeX notation via the brand-new version of the Google Chart Server. You can use the mathematical notation editor I have posted here - it will interpolate formulas within $dollar signs$ into HTML, generate the right <img> tags, and help insert css styles based on the image heights to get your vertical-alignment approximately right.

It helps if you know TeX syntax ahead of time, but even if you don't, it's not hard to learn what you need. When you go to davidbau.com/formula, it starts you off with a simple example.

Copy and paste your HTML.   QED.

Posted by David at 02:36 PM | Comments (2)

February 09, 2010

Second Coming of Wyden-Bennett?

Republicans, preparing for Obama's Healthcare Summit, clearly want to avoid looking as foolish as they did when hosting Obama at their House issues summit. "Start over," seems to be the new mantra.

GOP proposals for preconditions before talks just make them look like ridiculous Iranian nuclear negotiators; if Republicans continue on this path, they are playing into Democratic strategies and providing political cover for Democrats to pass healthcare by reconciliation next month.

Democrats look serious, and Republicans look like Ahmadinejad. Any good hawk knows that in that situation, stiff and swift sanctions are justified.

Continue reading "Second Coming of Wyden-Bennett?"
Posted by David at 06:56 AM | Comments (1)

February 01, 2010

Reading JQuery Sources

JQuery has become the stdlib of Javascript. After reading the jQuery docs, the next level of understanding comes from reading the code.

Here is an excellent new tool for reading the jQuery source, thanks to James Padolsey.

Food for Thought: International jQuery Use

On going back and looking at the top regions in which 'jquery' is a topic of Google searches, I find it interesting - and maybe worrying for U.S. internet innovators - that none of the top 6 jQuery cities of the world are in the U.S...

Continue reading "Reading JQuery Sources"
Posted by David at 07:23 AM | Comments (0)

January 30, 2010

Random Seeds, Coded Hints, and Quintillions

Here is a seedable random number generator in Javascript that you can set up to produce a determinstic sequence of pseudorandom numbers. Browsers do not provide a built-in way to seed Math.random(), so this solution is handy both when you need a repeatable pseudorandom sequence that is completely predictable, and when you need a robust seed that is much more unpredictable than your browser's built-in random number generator.

Many games that use weak random number generators have been cracked by exploiting their lack of randomness, and recently it has even been shown that it is possible to guess your 'random' Social Security Number given information about the time and location of your birth. To resist this type of attack, you want do better than a linear congruential PRNG seeded with the current time. Explanations below.

A Math.seedrandom Function

This script defines a function Math.seedrandom() that replaces Math.random with a seeded sequence of your choice. You use it by including seedrandom.js and then calling

Math.seedrandom('any string you like');

Next time you call Math.random(), you will get a deterministic sequence of results that can be reproduced at any time on any browser by calling seedrandom with the same string (with the example seed above, you will always get 0.4514661562021821, 0.06749172707294095, 0.8393296727715214, etc).

Seed:

The code uses RC4 as the pseudorandom number generator, so the randomness is a bit better than what you get from most browsers' built-in Math.random. But since it does all the computation in javascript, it is also is 3-10x slower.

A minified version of seedrandom.js (using the Google Closure Compiler) is about 1K.

The code also supports automatic seeding...

Continue reading "Random Seeds, Coded Hints, and Quintillions"
Posted by David at 08:06 PM | Comments (2)

January 24, 2010

Xinhua: We Report, You Decide

Does reading the Chinese press remind you of watching Fox News?

There is a whole world outside China and outside Fox News that is starkly different and a bit closer to reality.

But when you realize that Fox is actually the most trusted news brand in the U.S. today, you can get an appreciation for the success that a censored Chinese media can have inside the world's largest society. The effects of squashing dissent in the name of nationalism are not "very limited"...


Posted by David at 09:49 AM | Comments (1)

January 19, 2010

My God, What Have We Done?

Fellow Massachusetts voters! What was the point in electing Brown?

Continue reading "My God, What Have We Done?"
Posted by David at 09:57 PM | Comments (3)

January 18, 2010

Campaign Calls

We've gotten three calls from the Scott Brown campaign in the last 24 hours, two of them late at night. The GOP smells blood in the water.

But everybody around here is a Democrat!


Posted by David at 12:10 PM | Comments (1)

Calendar
March 2010
Sun Mon Tue Wed Thu Fri Sat
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
Search

Archives
Recent Entries
Python pi.py Spigot
The Mystery of 355/113
It's Not About The Answer
Solid Geometry
Teaching is Hard
A Mathematical Notation Question
Second Coming of Wyden-Bennett?
Reading JQuery Sources
Random Seeds, Coded Hints, and Quintillions
Xinhua: We Report, You Decide
My God, What Have We Done?
Campaign Calls
Vote
Ballmer Favors Torture for Lawyers
Anagram
Flawed Security
Google and China
Pointless Arbitrage
Mandelbrot
Bravo for Courier
What's in the Box
Coin Flips Are Biased
Javascript Reversi
Time To Build A Treehouse
Free Market Healthcare
Niagara Restaurants
Law vs Power
Weiner Goes to Washington
Where I Learned Programming
Disorderly Conduct
Dear Losers
McCain/Lieberman
Easy Math Typesetting
The Small Government Paradox
The 3.6% Accident
What is a Floppy Disk?
Two Steps Behind
Politics is Important
Your Compiler Vanishes in A Puff of Logic
Elastic Shortfalls
Poetry and Prose
Death and Taxes
Geithner's China Triumph
COP and the IMF Playbook
How To Nationalize
A Cultural Problem
Obama to the World: Join Us
What is a Stress Test?
How Big Is It?
Helicopters Arrive Under Cover Of AIG Testimony
Clever Outrage
Processing
Congressional Team Building
The Bossy R
Bulls Enter China Shop
American Majesty
No Deceit Here
Winning at Chess
Transition in Cyberspace
Elizabeth Warren
Continued Fractions
Rationals are Nonobvious
Yellowstone Swarm
RMB in the NYT
In Spending We Trust
Foreign Affairs on China
Junichiro Obama
Helicopters Take Off
Amazing Transparency
A Reflective Phobia
Trade Deficit Arithmetic
Perfect Numbers
Taxman Game
Mapping Out a Trade War
Currency Disaster
The Chinese Depression
Sudoku Help
Second Grade Spelling
Waiting For Doc
Twenty Eight Hours
What Do You Call It?
Rahm Emanuel
A Fourth Republic
Helicopters and Imports
An Historic Day
Chomp
The 144 Game
Klein on Obama
I Blame Markowitz
Time for Japan to Buy
Republican Rebels
Pictoral Subtraction
McCain and Obama at Al Smith
What After November 4?
Where Is The Excess Capital?
Quick, Get a Mortgage
World's Toughest Job
What's Scaring People
The U.S. Needs a King
Dear Nancy Pelosi
Links
Older Writing
About