LB Booster
« Finding specified digits of PI »

Welcome Guest. Please Login or Register.
Apr 1st, 2018, 03:55am



ATTENTION MEMBERS: Conforums will be closing it doors and discontinuing its service on April 15, 2018.
We apologize Conforums does not have any export functions to migrate data.
Ad-Free has been deactivated. Outstanding Ad-Free credits will be reimbursed to respective payment methods.

Thank you Conforums members.
Speed up Liberty BASIC programs by up to ten times!
Compile Liberty BASIC programs to compact, standalone executables!
Overcome many of Liberty BASIC's bugs and limitations!
LB Booster Resources
LB Booster documentation
LB Booster Home Page
LB Booster technical Wiki
Just BASIC forum
BBC BASIC Home Page
Liberty BASIC forum (the original)

« Previous Topic | Next Topic »
Pages: 1  Notify Send Topic Print
 thread  Author  Topic: Finding specified digits of PI  (Read 467 times)
Richard Russell
Administrator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM


Posts: 1348
xx Finding specified digits of PI
« Thread started on: Sep 8th, 2015, 1:29pm »

There are several algorithms for finding the value of PI to large numbers of digits (more than a million is practical in LBB) but they mostly work by starting at the beginning! A post on the Just BASIC forum queried whether it is possible to calculate just a few digits starting at a specific position, without finding the preceding digits.

This is a particularly interesting problem, especially when you are counting decimal digits - after all there's nothing 'natural' about base-10! Somewhat surprisingly it is possible, although the method was devised as recently as about twenty years ago. The program below, copied from my post to the JB forum, does it, although it's quite slow for large digit positions.

Because LBB calculates with greater floating-point precision than JB, the program should be good for more than 10 consecutive digits (probably up to about 14 or 15 should be OK, but I wouldn't want to guarantee it):

Code:
    INPUT "Enter the required starting digit of PI: "; D
    INPUT "Enter the number of digits wanted (max 10): "; C

    N = INT((D+20) * LOG(10) / LOG(2))
    acc = 0

    A = 3
    WHILE A <= 2*N

      M = INT(LOG(2*N) / LOG(A))
      P = A ^ M

      S = 0
      U = 1
      L = 1
      V = 0
      Q = 1
      R = 1

      FOR K = 1 TO N

        T = K
        IF Q >= A THEN
          DO
            T = INT(T/A)
            V = V-1
          LOOP UNTIL T MOD A
          Q = 0
        END IF

        Q = Q+1
        U = (U * T) MOD P

        T = 2*K - 1
        IF R >= A THEN
          IF R = A THEN
            DO
              T = INT(T/A)
              V = V+1
            LOOP UNTIL T MOD A
          END IF
          R = R-A
        END IF

        L = (L * T) MOD P
        R = R+2

        IF V > 0 THEN
          T = inv.mod(L, P)
          T = (T * U) MOD P
          T = (T * K) MOD P

          FOR I = V TO M-1
            T = (T * A) MOD P
          NEXT

          S = S+T
          IF S >= P THEN S = S-P
        END IF

      NEXT K

      T = pow.mod(10, D-1, P)
      S = (S * T) MOD P
      acc = acc + S / P
      acc = acc - INT(acc)

      A = next.prime(A)
      SCAN
    WEND

    PRINT "Decimal digits of PI at position "; D; ": "; STR$(INT(acc * 10^C))
    END

FUNCTION next.prime(N)
    DO
      N = N+1
    LOOP UNTIL is.prime(N)
    next.prime = N
END FUNCTION

FUNCTION is.prime(N)
    IF N MOD 2 = 0 THEN is.prime = 0 : EXIT FUNCTION
    FOR I = 3 TO SQR(N) STEP 2
      IF N MOD I = 0 THEN is.prime = 0 : EXIT FUNCTION
    NEXT
    is.prime = 1
END FUNCTION

FUNCTION pow.mod(A, B, M)
    R = 1
    DO
      IF B AND 1 THEN R = (R * A) MOD M
      B = INT(B/2)
      IF B = 0 THEN EXIT DO
      A = (A * A) MOD M
    LOOP UNTIL 0
    pow.mod = R
END FUNCTION

FUNCTION inv.mod(X, Y)
    V = Y
    C = 1
    DO
      Q = INT(V / X)

      T = C
      C = A-Q*C
      A = T

      T = X
      X = V-Q*X
      V = T
    LOOP UNTIL X = 0
    A = A MOD Y
    IF A<0 THEN A = A+Y
    inv.mod = A
END FUNCTION  

Richard.
« Last Edit: Sep 13th, 2015, 10:22pm by Richard Russell » User IP Logged

lancegary
New Member
Image


member is offline

Avatar




PM


Posts: 9
xx Re: Finding specified digits of PI
« Reply #1 on: Sep 9th, 2015, 12:02pm »

I haven't studied your algorithm in detail (too much other work) but I think you are using the Spigot algorithm? This algorithm is discussed here:

http://dept.cs.williams.edu/~heeringa/classes/cs135/s15/readings/spigot.pdf
User IP Logged

Richard Russell
Administrator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM


Posts: 1348
xx Re: Finding specified digits of PI
« Reply #2 on: Sep 9th, 2015, 12:48pm »

on Sep 9th, 2015, 12:02pm, lancegary wrote:
I haven't studied your algorithm in detail (too much other work) but I think you are using the Spigot algorithm?

If I understand correctly, the Spigot algorithm is the one I use in the earlier program I published, which finds digits of PI starting from the beginning. Crucially it uses an array, so the number of digits you can find depends (in principle) on the amount of memory available.

The most recent program, which finds digits starting at a specified position, has very small memory requirements. There is no array, and therefore the digits which you can find do not depend, even in principle, on the memory available. The general method is called the BBP Formula after the discoverers Bailey-Borwein-Plouffe.

Richard.
User IP Logged

lancegary
New Member
Image


member is offline

Avatar




PM


Posts: 9
xx Re: Finding specified digits of PI
« Reply #3 on: Sep 9th, 2015, 3:59pm »

Thanks.

Lance
User IP Logged

Pages: 1  Notify Send Topic Print
« Previous Topic | Next Topic »

| |

This forum powered for FREE by Conforums ©
Terms of Service | Privacy Policy | Conforums Support | Parental Controls