【问题标题】:Implementing the Spigot algorithm for π (pi)为 π (pi) 实现 Spigot 算法
【发布时间】:2011-05-04 07:55:20
【问题描述】:

我很难理解页面底部here 的 π (pi) Spigot 算法

我在第 2 部分“将 A 放入正则形式”的底部迷路了,我不确定如何在 C(或任何语言)中实现这一点

【问题讨论】:

  • 你可能对这个question感兴趣。

标签: c algorithm math spigot-algorithm


【解决方案1】:
  #include <math.h>
  #include <stdio.h>
  #define N 100

  int len = floor(10 * N/3) + 1;
  int A[len];

  for(int i = 0; i < len; ++i) {
    A[i] = 2;
  }

  int nines    = 0;
  int predigit = 0;

  for(int j = 1; j < N + 1; ++j) {        
    int q = 0;

    for(int i = len; i > 0; --i) {
      int x  = 10 * A[i-1] + q*i;
      A[i-1] = x % (2*i - 1);
      q = x / (2*i - 1);
    }

    A[0] = q%10;
    q    = q/10;

    if (9 == q) {
      ++nines;
    }
    else if (10 == q) {
      printf("%d", predigit + 1);

      for (int k = 0; k < nines; ++k) {
        printf("%d", 0);
      }
      predigit, nines = 0;
    }
    else {
      printf("%d", predigit);
      predigit = q;

      if (0 != nines) {    
        for (int k = 0; k < nines; ++k) {
          printf("%d", 9);
        }

        nines = 0;
      }
    }
  }
  printf("%d", predigit);

【讨论】:

  • 为什么要在 for 循环中初始化?为什么你的 if 语句被颠倒了?
  • “初始化 for 循环”是什么意思? if 语句是相反的,因为这有助于我发现无意的分配(例如,if(q = 0) 而不是if(q == 0))。编译器会捕获我的版本 (if(0 = q)) 但不会捕获 if(q = 0)
  • 哦,你的意思是for (int i = 0 ...)?清理杂乱,我猜。在循环的实际范围之外没有临时初始化。从理论上讲,这应该确保索引变量在循环内的词法范围内,但我想我在某处读到它们实际上仍然绑定在循环外。
【解决方案2】:
// Spigot program for pi to NDIGITS decimals.
// 4 digits per loop.
// Thanks to Dik T. Winter and Achim Flammenkamp who originally made a compressed version of this. 
 
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
 
#define NDIGITS 15536          //max digits to compute
#define LEN (NDIGITS/4+1)*14   //nec. array length
 
long a[LEN];                   //array of 4-digit-decimals
long b;                        //nominator prev. base
long c = LEN;                  //index
long d;                        //accumulator and carry
long e = 0;                    //save previous 4 digits
long f = 10000;                //new base, 4 decimal digits
long g;                        //denom previous base
long h = 0;                    //init switch
 
int main(void) {
        for(; (b=c-=14) > 0 ;){    //outer loop: 4 digits per loop
                for(; --b > 0 ;){      //inner loop: radix conv
                        d *= b;            //acc *= nom prev base
                        if( h == 0 )
                                d += 2000*f;   //first outer loop
                        else
                                d += a[b]*f;   //non-first outer loop
                        g=b+b-1;           //denom prev base
                        a[b] = d % g;
                        d /= g;            //save carry
                }
                h = printf("%.4d",e+d/f);//print prev 4 digits
                                         // %.4d to add leading zeros
                d = e = d % f;         //save currently 4 digits
                                       //assure a small enough d
        }
        getchar();
        return 0;
}

【讨论】:

    【解决方案3】:

    http://www.numberworld.org/misc_runs/pi-10t/details.html 相比,我发现上述 spigot pi 程序的 o/p 数字有所不同

    50 位 Pi 的正确值: http://www.numberworld.org/misc_runs/pi-10t/details.html

    3.

    1415926535 8979323846 2643383279 5028841971 6939937510

    高于 pigot pi:

    3.

    1415926535 8979323846 2643383279 5**(0)**28841971 6939937510

                                         ^^^ zero missing
    

    通过修改将每个循环的 4 位更改为 8 位 长 f = 100000000;

    产生了正确的结果。

    【讨论】:

    • 似乎这是 Spigot 算法中的一个已知问题,需要纠正步骤 - 请参阅修复此问题并产生正确结果的 haenel 实现。在jjj.de/hfloat/spigot.haenel.txt开始时查看正确的C实现和描述
    • Haenel 为 32372 位实现的 Pi Spigot。使用 Chudnovsky 算法 o/p 验证了正确的数字序列。 2. 这是一个在这方面似乎是正确的版本,希望没有新的错误:(它也更快更短。)版本“快速”:(大约快 25%)/* Calculation of pi to 32372 decimal digits */ /* Size of program: 152 characters */ /* After Dik T. Winter, CWI Amsterdam */ unsigned a=1e4,b,c=113316,d,e,f[113316],g,h,i; main(){for(;b=c,c-=14;i=printf("%04d",e+d/a),e=d%a) while(g=--b*2)d=h*b+a*(i?f[b]:a/5),h=d/--g,f[b]=d-g*h;}
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多