第一个求圆周率的程序,目前发现,龙芯到目前为止无法算完999999位数的圆周率,因为我们测试的龙芯3a3000搞坏了,目前只测了99999圆周率,源码如下:
#include
#include
#include
int main(int argc, char * argv[])
{
clock_t start, finish;
double duration;
long * pi, * t, m, n, r, s;
int t0[][3] = {48, 32, 20, 24, 8, 4}, k0[][3] = {1, 1, 0, 1, 1, 1};
int n0[][3] = {18, 57, 239, 8, 57, 239}, d, i, j, k, p, q;
d = (argc > 1) ? (((i = atoi(argv[1])) < 0) ? 0 : i) : 1000;
q = (argc > 2) ? 1 : 0;
printf("%s\n\n", "Nature (R) Pi value compute Program (C) Tue 1999.11.30");
printf("pi= %s%d * arctg(1/%d) %s %d * arctg(1/%d) %s %d * arctg(1/%d) [%s]\n",
k0[q][0] ? "" : "-", t0[q][0], n0[q][0], k0[q][1] ? "+" : "-", t0[q][1],
n0[q][1], k0[q][2] ? "+" : "-", t0[q][2], n0[q][2], q ? "Stomer" : "Gauss");
if ((t = (long *)calloc((d += 5) + 1, sizeof(long))) == NULL) return 1;
if ((pi = (long *)calloc(d + 1, sizeof(long))) == NULL) return 2;
start = clock();
for (i = d; i >= 0; i--) pi[i] = 0;
for (p = 0; p < 3; p++) {
for (k=k0[q][p], n=n0[q][p], t[i=j=d]=t0[q][p], i--; i >= 0; i--) t[i] = 0;
for (r = 0, i = j; i >= 0; i--) {
r = (m = 10 * r + t[i]) % n;
t[i] = m / n;
k ? (pi[i] += t[i]) : (pi[i] -= t[i]);
}
while (j > 0 && t[j] == 0) j--;
for (k = !k, s = 3, n *= n; j > 0; k = !k, s += 2) {
for (r = 0, i = j; i >= 0; i--) {
r = (m = 10 * r + t[i]) % n;
t[i] = m / n;
}
while (j > 0 && t[j] == 0) j--;
for (r = 0, i = j; i >= 0; i--) {
r = (m = 10 * r + t[i]) % s;
m /= s;
k ? (pi[i] += m) : (pi[i] -= m);
}
}
}
for (n = i = 0; i <= d; pi[i++] = r) {
n = (m = pi[i] + n) / 10;
if ((r = m % 10) < 0) r += 10, n--;
}
finish = clock();
printf("pi= %ld.", pi[d]);
for (i = d - 1; i >= 5; i--)
printf("%ld%s", pi[i], ((m = d - i + 5) % 65) ? ((m % 5) ? "" : " ") : "\n");
printf("%sDIGITS: %d\n", (m % 65) ? "\n" : "", d - 5);
duration = (double)(finish - start) / CLOCKS_PER_SEC;
printf("time %f seconds \n", duration);
return 0;
}