Pi Spigot Algorythm

MM2forever

Active Member
Licensed User
Longtime User
Hello boys (& girls ^^)

I wanted to code a spigot algorythm in b4p (that means adapt the c code i found here: Pispigot.htm)

The parts i cant cope with are highlited:

B4X:
#include <stdio.h>
#include <stdlib.h>

#define N 4000       /* hier 4000 Stellen*/
#define LEN (N/4+1)*14 /* Feldlaenge def.*/

long a[COLOR="Red"][LEN][/COLOR];         /* 4 stell. Bereich */
long b;              /* Zaehler Basis alt*/
long c = LEN;        /* Feld-Index       */
long d;              /* Akkumulator      */
long e = 0;          /* Uebertr.4 stellig*/
long f = 10000;      /* Neue Basis       */
long g;              /* Nenner Basis     */
long h = 0;          /* erst 0, dann 1   */

int main(void)
{
[COLOR="Red"]for (;(b=c-=14) > 0;)[/COLOR]/* aeussere Schleife*/
 {
 [COLOR="Red"] for (; --b > 0;) [/COLOR]  /* Radix-Konvertier.*/
   {
     d *= b;         /* Feldindex  Basis */
     if (h == 0)     /* erster Durchlauf?*/
       d += 2000*f;  /* ja               */
     else
       d += a[b]*f;  /* nein             */
     g = b+b-1;      /* Nenner Basis     */
     a[b] = [COLOR="Red"]d%g[/COLOR];     /* Uebertrag        */
     d /= g;         /* Rest             */
   }
   h = printf("%04d",e+d/f); /*Korrektur */
   d = e = d%f;      /*nur ein Durchlauf */
                     /* Stelle bewahren  */
 }
}
getch();

Its that variable[some value] meant to be an array?
Arent these for loops actually while loops? And how should the conditions look like in b4p?
is d%f a nother way of doing a division?

THX in advance for your help.

MfG/Regards

Christian
 
Last edited:

agraham

Expert
Licensed User
Longtime User
long a[LEN];
is
Dim a(LEN)

I hate "clever" C code :mad:. C has a perfectly good "while" keyword that would make the intent clearer.
for ( ; (b=c-=14) > 0; )
is
Do While (c - 14) > 0
c = c - 14
b = c
..... ' rest of code
Loop
c = c - 14
b = c
' this makes the exit state of b and c the same as in the C code

for ( ; --b > 0; )
is
While (b -1) > 0
b = b - 1
..... ' rest of code
Loop
b = b - 1
' this makes the exit state of b the same as in the C code


a = d%g;
is
a(b) = d MOD g
 
Last edited:

MM2forever

Active Member
Licensed User
Longtime User
Well, I wrote that and h = e+d/f makes some trouble because d is infinity...^^ hence "Input string is not in a correct format".
Oh noes...What now?
btw is h actually the pi comma value that i have to display?

B4X:
Sub Globals
e = 0   
f = 10000
h = 0
Dim a(0)
End Sub

Sub App_Start
   Form1.Show
End Sub

Sub generate_pi(n)
LEN=(n/4+1)*14
Dim a(LEN)
c = LEN

   Do While (c - 14) > 0
      c = c - 14
      b = c
        Do While (b - 1) > 0
         b = b - 1
   
           d =d * b         
           If h = 0 Then     
                d = d+ 2000*f  
           Else
                d =d+ a(b)*f  
         End If
              g = b+b-1     
              a(b) = d Mod g    
              d =d / g     
         Loop
      b = b - 1 
Msgbox(f)
   h = e+d/f
   preview.Text=preview.Text&h&","
   e = d Mod f  
   d = e            
   Loop
   c = c - 14
   b = c 

End Sub
 

agraham

Expert
Licensed User
Longtime User
You are not giving it enough places to calculate, it needs a minimum of 16. Also as the algorithm is an integer algotithm you need a few tweaks.
B4X:
Sub generate_pi(n)
LEN=(n/4+1)*14
Dim a(LEN)
c = LEN
   Do While (c - 14) > 0
      c = c - 14
      b = c
        Do While (b - 1) > 0
         b = b - 1   
           d = d * b         
           If h = 0 Then     
                d = d + 2000*f  
           Else
                d = d + a(b)*f  
         End If
         g = b+b-1     
         a(b) = d Mod g    
         d = [COLOR="red"]Int(d / g)[/COLOR]    
         Loop
      b = b - 1 
      'Msgbox(g)
   h = e+d/f
   preview.Text=preview.Text & [COLOR="Red"]Format(h, "D4")[/COLOR] &","
   e = d Mod f  
   d = e            
   Loop
   c = c - 14
   b = c
End Sub
 

MM2forever

Active Member
Licensed User
Longtime User
of course i undid (is this the correct form?) it ^^

i dont care if its exactly pi by the way as long as it is an unlinear (non ran#) number sequence, need for what im finally gonna do with^^ (hope this wasnt too much hint, but guess if u want to^^)

i wont tell you what its gonna be until i got patent for it ^^ :sign0089:
 
Top