Pi Spigot Algorythm

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

  1. MM2forever

    MM2forever Active Member Licensed 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:

    Code:
    #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: Oct 5, 2009
  2. agraham

    agraham Expert Licensed 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: Oct 5, 2009
  3. mjcoon

    mjcoon Well-Known Member Licensed User



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

    Mike.
     
  4. MM2forever

    MM2forever Active Member Licensed User

    many thanks for your quick help agraham ! :sign0060:
    :sign0089:
     
    Last edited: Oct 5, 2009
  5. agraham

    agraham Expert Licensed User

    It's an operator, not a keyword.
     
  6. MM2forever

    MM2forever Active Member Licensed User

    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?
     
  7. agraham

    agraham Expert Licensed User

    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
     
  8. MM2forever

    MM2forever Active Member Licensed 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?

    Code:
    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
     
  9. agraham

    agraham Expert Licensed 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.
    Code:
    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
     
  10. MM2forever

    MM2forever Active Member Licensed User

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

    Now its working top notch with your additions.
    Thanks for your dedication!
     
  11. agraham

    agraham Expert Licensed User

    I trust that you have undone that change. The code I posted looks like its accurate at least to the Feynman point so presumably it's OK beyond.
     
  12. MM2forever

    MM2forever Active Member Licensed 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:
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice