| home | send comment | send link | add bookmark |

Digit generator for the base of the natural logarithms

by Stephen R. Schmitt


Definition

The constant irrational number e is one of the most important constants in mathematics. It is the base number for natural logarithms. It has the property that the area under the hyperbola y = 1/x from x = 1...e equals 1.

The sample program, below, uses a spigot algorithm to compute the first 10,000 digits. A spigot algorithm yields its outputs incrementally, and does not reuse them after producing them.

Zeno source code

Zeno 1.2 is an interpreter for the Zeno programming language. It is an easy to learn and is suitable for educational purposes.
var   A : array[3302] of int
const B : int := 3301

program

    var f, i, j, k, n : int

    % assign each element of array 1
    for i := 1...B do
        A[i] := 1
    end for
    % except this element
    A[3301] := 2    

    % initialize counters
    i := 0
    j := 2001
    f := 0
    put i:5, ":  "...           % total digits thus far

    while j > 0

        decr j
        k := 1
        n := B

        while n > 0
            f    := f + A[k]*100000
            A[k] := f mod n
            f    := f div n
            incr k
            decr n
        end while

        put_five( f )           % group of five digits

        if j mod 10 = 1 then    % start a new line
            i := i + 50
            put ""
            put i:5, ":   "...  % total digits thus far
        end if

    end while
    
end program

procedure put_five( n : int )

    var s : string := intstr( n, 0 )
    var z : int := 5 - strlen( s )

    while z > 0
        put "0"...
        decr z
    end while
    put s, " "...

end procedure

Sample output

    0:  271828 18284 59045 23536 02874 71352 66249 77572 47093 69995  
   50:   95749 66967 62772 40766 30353 54759 45713 82178 52516 64274  
  100:   27466 39193 20030 59921 81741 35966 29043 57290 03342 95260  
  150:   59563 07381 32328 62794 34907 63233 82988 07531 95251 01901  
  200:   15738 34187 93070 21540 89149 93488 41675 09244 76146 06680

Reference

  1. Mathworld: e
  2. Numbers, constants and computation


Copyright © 2005, Stephen R. Schmitt