    ' http://faculty.cs.tamu.edu/klappi/csce411-s15/csce411-set1.pdf
    ' compute N digits of Euler's number e, using Spigot algorithm
    
    uses console 
     
    declare function e_spigot(int N) as string
     
    SetConsoleTitle "Euler's number e"
    string s = e_Spigot(150)
    print s cr
     
    putfile("e.txt",s)
    print cr
    print "Saved as e.txt" cr
     
    print "Enter ... " cr 
    waitkey
    ------------------------------

     
    function e_Spigot(int N) as string
    ==================================
       int q,x,i,j,k,idx
       redim int A[N]
       string s = nuls N 
       byte ss at strptr s
       '
       idx = 1 : ss[idx] = 2
       '
       for j = 1 to N
          A[j] = 1  'set all digits of nonintegral part to 1.
       next j                           
       '
       for i = 1 to N-2
          q = 0
          for j=N to 1 step -1
             A[j] = 10 * A[j] + q
             k = j + 1
             q = A[j] \ k         'amount that needs to be carried
             'A[j] = mod(A[j],k)  'keep only remainder
             A[j] = edx           'remainder register
          next
          idx++ : ss[idx] = q
       next i
       '
       'convert to ascii
       for x=1 to N
         ss[x]+=48
       next x
       '
       del A
       return s   
    end function
     