<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
<META content="MSHTML 6.00.2800.1106" name=GENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY>
<DIV><FONT face=Arial size=2>For some reason the attachment doesn't seem to be
working, at least for me. Here's the code just in case. Please
excuse the spacing. For some reason the tabs are only represented by one
space.</FONT></DIV><FONT face=Arial size=2>
<DIV><BR><FONT face="Courier New">import std.c.stdio;<BR>import
std.c.stdlib;<BR>import std.c.time;</FONT></DIV>
<DIV><FONT face="Courier New"></FONT> </DIV>
<DIV><FONT face="Courier New">const int LONG_TIME=4000;</FONT></DIV>
<DIV><FONT face="Courier New"></FONT> </DIV>
<DIV><FONT face="Courier New">byte[] p;<BR>byte[] t;<BR>int q;</FONT></DIV>
<DIV><FONT face="Courier New"></FONT> </DIV>
<DIV><FONT face="Courier New">int main(char[][] args)<BR>{<BR> time_t
startime, endtime;</FONT></DIV>
<DIV><FONT face="Courier New"></FONT> </DIV>
<DIV><FONT face="Courier New"> if (args.length == 2)
{<BR> sscanf(&args[1][0],"%d",&q);<BR> } else
{<BR> printf("Usage: pi
[precision]\n");<BR> exit(55);<BR> }</FONT></DIV>
<DIV><FONT face="Courier New"></FONT> </DIV>
<DIV><FONT face="Courier New"> if (q <
0)<BR> {<BR> printf("Precision was too low, running with
precision of 0.\n");<BR> q = 0;<BR> }</FONT></DIV>
<DIV><FONT face="Courier New"></FONT> </DIV>
<DIV><FONT face="Courier New"> if (q >
LONG_TIME)<BR> {<BR> printf("Be prepared to wait a
while...\n");<BR> }</FONT></DIV>
<DIV><FONT face="Courier New"></FONT> </DIV>
<DIV><FONT face="Courier New"> // Compute one more digit than we display to
compensate for rounding<BR> q++;</FONT></DIV>
<DIV><FONT face="Courier New"></FONT> </DIV>
<DIV><FONT face="Courier New"> p.length = q + 1;<BR> t.length = q +
1;</FONT></DIV>
<DIV><FONT face="Courier New"></FONT> </DIV>
<DIV><FONT face="Courier New"> // Compute
pi<BR> std.c.time.time(&startime);<BR> arctan2();<BR> arctan3();<BR> mul4();<BR> std.c.time.time(&endtime);</FONT></DIV>
<DIV><FONT face="Courier New"></FONT> </DIV>
<DIV><FONT face="Courier New"> // Return to the number of digits we want to
display<BR> q--;</FONT></DIV>
<DIV><FONT face="Courier New"></FONT> </DIV>
<DIV><FONT face="Courier New"> // Print pi<BR> printf("pi =
%d.",cast(int)(p[0]));<BR> for (int i = 1; i <= q;
i++)<BR> printf("%d",cast(int)(p[i]));<BR> printf("\n");<BR> printf("%ld
seconds to compute pi with a precision of %d
digits.\n",endtime-startime,q);</FONT></DIV>
<DIV><FONT face="Courier New"></FONT> </DIV>
<DIV><FONT face="Courier New"> return 0;<BR>}</FONT></DIV>
<DIV><FONT face="Courier New"></FONT> </DIV>
<DIV><FONT face="Courier New">/* Template that determines if a number is a power
of <BR> * 2 at compile time<BR> */<BR>template isPow2(uint n) { const
bool isPow2 = (n % 2) != 0 ? false : isPow2!(n / 2); }<BR>template isPow2(uint n
: 0) { const bool isPow2 = false; }<BR>template isPow2(uint n : 1) { const bool
isPow2 = false; }<BR>template isPow2(uint n : 2) { const bool isPow2 = true;
}</FONT></DIV>
<DIV><FONT face="Courier New"></FONT> </DIV>
<DIV><FONT face="Courier New">/* Use a template to get good compile time
optimizations for<BR> * division and multiplication
<BR> */<BR>template fastdiv(int divisor)<BR>{<BR> void
fastdiv()<BR> {<BR> int r; // remainder</FONT></DIV>
<DIV><FONT face="Courier New"></FONT> </DIV>
<DIV><FONT face="Courier New"> <BR> for (int i = 0; i <= q;
i++) {<BR> int b = t[i] + r * 10;<BR> int q =
b / divisor;<BR> static if(isPow2!(divisor))
<BR> r = b % divisor; // compiler can optimize modulus
for powers of 2<BR> else <BR> r = b - q
* divisor;<BR> t[i] =
q;<BR> }<BR> }<BR>}</FONT></DIV>
<DIV><FONT face="Courier New"></FONT> </DIV>
<DIV><FONT face="Courier New">// Computes the arctangent of 2<BR>void
arctan2()<BR>{<BR> int n;</FONT></DIV>
<DIV><FONT face="Courier New"></FONT> </DIV>
<DIV><FONT face="Courier New"> t[0] =
1;<BR> fastdiv!(2);<BR> add();<BR> n = 1;<BR> do
{<BR> mul(n);<BR> fastdiv!(4);<BR> div(n +=
2);<BR> if ((((n-1) / 2) % 2) ==
0)<BR> add();<BR> else<BR> sub();<BR> }
while (!tiszero());<BR>}</FONT></DIV>
<DIV><FONT face="Courier New"></FONT> </DIV>
<DIV><BR><FONT face="Courier New">// Computes the arctangent of 3<BR>void
arctan3()<BR>{<BR> int n;</FONT></DIV>
<DIV><FONT face="Courier New"></FONT> </DIV>
<DIV><FONT face="Courier New"> t[0] =
1;<BR> fastdiv!(3);<BR> add();<BR> n = 1;<BR> do
{<BR> mul(n);<BR> fastdiv!(9);<BR> div(n +=
2);<BR> if ((((n-1) / 2) % 2) ==
0)<BR> add();<BR> else<BR> sub();<BR> }
while (!tiszero());<BR>}</FONT></DIV>
<DIV><FONT face="Courier New"></FONT> </DIV>
<DIV><FONT face="Courier New">void add()<BR>{</FONT></DIV>
<DIV><FONT face="Courier New"></FONT> </DIV>
<DIV><FONT face="Courier New"> for (int j = q; j >= 0;
j--)<BR> {<BR> if (t[j] + p[j] > 9)
{<BR> p[j] += t[j] -
10;<BR> p[j-1]++;<BR> }
else<BR> p[j] += t[j];<BR> }<BR>}</FONT></DIV>
<DIV><FONT face="Courier New"></FONT> </DIV>
<DIV><FONT face="Courier New">void sub()<BR>{<BR> for (int j = q; j >=
0; j--)<BR> if (p[j] < t[j]) {<BR> p[j] -= t[j] -
10;<BR> p[j-1]--;<BR> }
else<BR> p[j] -= t[j];<BR>}</FONT></DIV>
<DIV><FONT face="Courier New"></FONT> </DIV>
<DIV><BR><FONT face="Courier New">void mul(int multiplier)<BR>{<BR> int
c;</FONT></DIV>
<DIV><FONT face="Courier New"></FONT> </DIV>
<DIV><BR><FONT face="Courier New"> for (int i = q; i >= 0; i--)
{<BR> int b = t[i] * multiplier + c;<BR> c = b /
10;<BR> t[i] = b - c * 10; <BR> }<BR>}</FONT></DIV>
<DIV><FONT face="Courier New"></FONT> </DIV>
<DIV><BR><FONT face="Courier New">void mul4()<BR>{<BR> int c;</FONT></DIV>
<DIV><FONT face="Courier New"></FONT> </DIV>
<DIV><FONT face="Courier New"> for (int i = q; i >= 0; i--)
{<BR>
int b = p[i] * 4 +
c;<BR>
c = b / 10;<BR> p[i] = b - c * 10; <BR> }<BR>}</FONT></DIV>
<DIV><FONT face="Courier New"></FONT> </DIV>
<DIV><BR><FONT face="Courier New">void div(int divisor)<BR>{<BR> int r; //
remainder</FONT></DIV>
<DIV><FONT face="Courier New"></FONT> </DIV>
<DIV><FONT face="Courier New"> /*
Optimization: It's faster to do floatint point multiplication
than<BR> * integer division.
This is because there is no integer division
instruction.<BR> * Under the
hood, integer division is essentially floating-point
division.<BR>
*/<BR> double idiv = 1.0 /
divisor;</FONT></DIV>
<DIV><FONT face="Courier New"></FONT> </DIV>
<DIV><FONT face="Courier New"> for (int i = 0; i <= q; i++)
{<BR>
int b = t[i] + r * 10; <BR> int quotient = cast(int)(cast(double)b *
idiv);<BR> r = b - quotient * divisor;<BR> t[i] =
quotient;<BR> }<BR>}</FONT></DIV>
<DIV><FONT face="Courier New"></FONT> </DIV>
<DIV><FONT face="Courier New">int tiszero()<BR>{<BR> for (int k = 0; k
<= q; k++)<BR> if (t[k] != 0)<BR> return
false;<BR> return true;<BR>}</FONT></DIV>
<DIV> </DIV>
<DIV> </DIV>
<DIV> </DIV>
<DIV></FONT> </DIV></BODY></HTML>