Solving Pi
ken at boring.UUCP
ken at boring.UUCP
Sat Jul 27 07:48:17 AEST 1985
I decided to rewrite the posted program in "bc". (If you think having
only 286 distinct names in BASIC is bad, try using "bc"!) To get more
decimal places I had to use scaling, see program for details.
Fortunately, we know 1 < pi < 10 :-).
It took 2 hours to compute pi to a 1000 places on a 780 but it is
limited only by the amount of memory "dc" can get. And at last I have
something to soak up all those spare CPU cycles.
Anybody know a faster converging series for pi?
Enjoy, Ken
#! /bin/sh
# This is a shell archive, meaning:
# 1. Remove everything above the #! /bin/sh line.
# 2. Save the resulting text in a file.
# 3. Execute the file with /bin/sh (not csh) to create the files:
# pi.b
# pi.sh
# This archive created: Fri Jul 26 15:37:57 1985
# By: Ken Yap ()
export PATH; PATH=/bin:$PATH
if test -f 'pi.b'
then
echo shar: will not over-write existing file "'pi.b'"
else
cat << \SHAR_EOF > 'pi.b'
/*
** NAME
** pi -- a program to calculate the value of pi
**
** SYNOPSIS
** bc pi.b
**
** DESCRIPTION
** Pi calculates the value of pi to the number of digits
** specified.
**
** This is an adaptation in BC of the BASIC program "Apple Pi"
** by Robert J. Bishop, published in Micro Apple (c) 1981
** Micro Ink, Inc. That program, when run on an Apple II,
** took 40 hours to solve pi to 1000 places. The
** program solves pi using the series expansion:
**
** infinity infinity
** ____ 16*(-1e(k+1)) ____ 4*(-1e(k+1))
** \ \
** pi = > ------------- - > ------------
** / /
** ---- (2k-1)*5e(2k-1) ---- (2k-1)*239e(2k-1)
** k = 1 k = 1
**
** AUTHORS
** Bryan Costales ( ...!dual!ptsfa!ski!eeg!bcx )
** pi.c (c) 1985 Bryan Costales
**
** Modified for bc by Ken Yap (..!seismo!{rochester,mcvax}!ken)
** pi.b
**
** BUGS
**
*/
scale = 99;
d/*en*/ = 1;
define i(p/*laces*/) {
auto x;
for (x = 0; x < p/*laces*/; x++) d/*en*/ *= 10;
"Result will be pi times 10 to the power of ";
return (p/*laces*/);
}
define p/*i*/(k) {
k *= 2;
s/*ign*/ = 1;
x = 0;
y = 0;
m = 5;
n = 239;
for(e/*xp*/ = 1; e/*xp*/ < k; e/*xp*/ += 2) {
f = d/*en*/ / (e/*xp*/ * m);
g = d/*en*/ / (e/*xp*/ * n);
if ( s/*ign*/ > 0 ) {
x += f;
y += g;
}
if ( s/*ign*/ < 0 ) {
x -= f;
y -= g;
}
m *= 25;
n *= 57121;
s/*ign*/ *= -1;
}
return(x * 16 - y * 4);
}
SHAR_EOF
if test 1529 -ne "`wc -c < 'pi.b'`"
then
echo shar: error transmitting "'pi.b'" '(should have been 1529 characters)'
fi
fi # end of overwriting check
if test -f 'pi.sh'
then
echo shar: will not over-write existing file "'pi.sh'"
else
cat << \SHAR_EOF > 'pi.sh'
#! /bin/sh
bc -c pi.b > pi.d << PROG
i(1000)
p(1000)
PROG
nice dc < pi.d > pi.1000 &
SHAR_EOF
if test 85 -ne "`wc -c < 'pi.sh'`"
then
echo shar: error transmitting "'pi.sh'" '(should have been 85 characters)'
fi
chmod +x 'pi.sh'
fi # end of overwriting check
# End of shell archive
exit 0
--
UUCP: ..!{seismo,okstate,garfield,decvax,philabs}!mcvax!ken Voice: Ken!
Mail: Centrum voor Wiskunde en Informatica, Kruislaan 413, 1098 SJ, Amsterdam.
More information about the Comp.sources.unix
mailing list