Re: Pi with bad calculator
- From: "Jim Black" <tramspap@xxxxxxxxx>
- Date: 18 Mar 2007 21:58:45 -0700
On Mar 18, 7:45 pm, BioFreak <BioFr...@xxxxxxxxxxxxxxx> wrote:
While vacationing in the Ozarks you're stuck with a
little piece of grocery store calculator whose
extravagance doesn't go beyond a mere square root
button. But you're doing a back of the envelope
calculation of something that begs for a value for Pi
way better than 3.1415 which you can remember for sure.
Other than cursing yourself for not remembering the
next few digits what else can you do to manage?
Well, there are nice algorithms for doing this, but the only one I
remember off the top of my head is
pi = 4/1 - 4/3 + 4/5 - 4/7 + ...
and that converges way to slow. And if I could look up an algorithm,
I could look up pi.
So it's back to first principles. We can use the old trick of
doubling the number of sides in a regular polygon repeatedly to
approximate a circle. So in my head I draw a little diagram
containing half of a side of an n-gon, and a side of a 2n-gon, and
lines connecting everything to the center. I set the radius of the
circle to one, and I'll call the length of the half-side of the n-gon
y. By two applications of the Pythagorean theorem, the length of the
2n-gon side is:
sqrt( y^2 + (1 - sqrt(1-y^2))^2 )
Simplifying, we get:
sqrt( 2 - 2 sqrt(1-y^2) )
So what we need to iterate is
s' = sqrt( 2 - 2 sqrt(1 - s^2/4) ),
starting from s=1 in the case of the hexagon. But notice that we
would be taking a square root and then immediately squaring it, which
is clearly wasteful. So let's simplify the operations we have to do
between two of the square roots that can't be eliminated:
1 - (sqrt(2-2x))^2 / 4
= (x + 1)/2
That isn't so painful, after all. First, we do the calculation up to
the square root from s=1, getting
sqrt(3/4).
Then we iterate
x' = sqrt((x + 1)/2)
N times, where N is some large number. Then we finish off the
calculation of the side length with
s = sqrt(2 - 2x).
Last, we multiply by 3 * 2^(N+1) to get pi.
I tried this for N = 20 on my PC's simulacrum of a stupid calculator.
I got: 3.1415926535897605995224090804006
pi = 3.1415926535897932384626433832795
On a real stupid calculator, though, I'd be a bit worried about loss
of precision. Notice that at the end of the calculation,
sqrt(2 - 2x) ~ pi / 3 / 2^(N+1)
so
x ~ 1 - pi^2 / 9 / 2^(2N+3).
That's going to limit how far you can take the calculation out, unless
you want to spend even more time thinking and come up with a clever
way to get around the problem.
--
Jim E. Black
.
- Follow-Ups:
- Re: Pi with bad calculator
- From: The Ghost In The Machine
- Re: Pi with bad calculator
- References:
- Pi with bad calculator
- From: BioFreak
- Pi with bad calculator
- Prev by Date: Re: Congregations of Lucifer are all agreed as One
- Next by Date: Re: 1.03 inches of non-brittle, ice-class steel.
- Previous by thread: Re: Pi with bad calculator
- Next by thread: Re: Pi with bad calculator
- Index(es):