# Pi Spigot Algorythm

Discussion in 'Questions (Windows Mobile)' started by MM2forever, Oct 5, 2009.

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:

Code:
`#include &lt;stdio.h&gt;#include &lt;stdlib.h&gt;#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?

MfG/Regards

Christian

Last edited: Oct 5, 2009

long a[LEN];
is
Dim a(LEN)

I hate "clever" C code . 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: Oct 5, 2009

"Mod"? I was just looking for something like that but could not see it listed under "Math" keywords in Basic4PPC Help...

Mike.

many thanks for your quick help agraham ! :sign0060:
:sign0089:

Last edited: Oct 5, 2009

It's an operator, not a keyword.

What is d *= b; meaning?

and where do i have to set the loop statements? on the two } brackets?
and if, who is the if statement closing then?

d = d * b, ignore {, replace } by Loop
Code:
`If h = 0 Then     /* erster Durchlauf?*/  d = d + 2000*f;  /* ja               */Else  d =  d + a(b)*f;  /* nein             */End If`

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?

Code:
`Sub Globalse = 0   f = 10000h = 0Dim a(0)End SubSub App_Start   Form1.ShowEnd SubSub generate_pi(n)LEN=(n/4+1)*14Dim 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`

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.
Code:
`Sub generate_pi(n)LEN=(n/4+1)*14Dim 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 = cEnd Sub`

well before your message i made h = e+d/f to h = (e+d)/f and that at least gave me the right numbers.