    ' 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)
    printl s
     
    putfile("e.txt",s)
    print cr
    printl "Saved as e.txt"
     
    printl "Enter ... " 
    waitkey
    ------------------------------

     
    function e_Spigot(int N) as string
    ==================================
       int q,x,idx
     
       dyn int A[N]
     
       string s = stringbytes(N,0)      'nuls   
       byte ss at strptr s
       idx++ : ss[idx]=2
       int j
       for j = 1 to N
          A[j] = 1                      'set all digits of nonintegral part to 1.
       next j                           
       int i
       for i = 1 to N-2
          if mod(i,1000) = 0 then printl i
          q = 0
          j=N
          while j > 0
             A[j] = 10 * A[j] + q
             q = A[j] \ (j + 1)         'amount that needs to be carried
    '         A[j] = mod(A[j],(j + 1))   'keep only remainder
             A[j] = edx
             j--
          wend
          idx++ : ss[idx] = q
       next i
       
       'convert to ascii
       for x=1 to n : ss[x]+=48 : next x
     
       'release dynamic array
       del A
     
       return s   
    end function
     