下边内容段是关于C语言经典算法 - 超长PI计算的内容,应该能对大伙有一些用。
#include <stdio.h>
#define L 1000
#define N L/4+1
int main(void)
{
int s[N + 3] =
{
0
};
int w[N + 3] =
{
0
};
int v[N + 3] =
{
0
};
int q[N + 3] =
{
0
};
int n = (int)(L / 1.39793 + 1);
int k;
for (k = 1; k <= n; k++)
{
div(w, 25, w);
div(v, 239, v);
div(v, 239, v);
sub(w, v, q);
if (k % 2)
add(s, q, s);
else
sub(s, q, s);
}
printf("%d.", s[0]);
for (k = 1; k < N; k++)
printf("%04d", s[k]);
printf("n");
return 0;
}
{
int i, carry = 0;
for (i = N + 1; i >= 0; i--)
{
c[i] = a[i] + b[i] + carry;
if (c[i] < 10000)
carry = 0;
else
{
c[i] = c[i] - 10000;
carry = 1;
}
}
}
{
int i, borrow = 0;
for (i = N + 1; i >= 0; i--)
{
c[i] = a[i] - b[i] - borrow;
if (c[i] >= 0)
borrow = 0;
else
{
c[i] = c[i] + 10000;
borrow = 1;
}
}
}
{
int i, tmp, remain = 0;
for (i = 0; i <= N + 1; i++)
{
tmp = a[i] + remain;
c[i] = tmp / b;
}
}
网友评论