% Calculating pi in TeX, to fit on a business card
% See the blog post for more information.

% Setup variable names


% Calculate #1 as the remainder of #2/#3.
  \divide\remainder by#3%
  \multiply\remainder by-#3%
  \advance\remainder by#2%

% Functions for pushing and popping from the arrays of numbers.
% Each list is stored looking like
% ;<number>;<number>;...;<number>;:
% We can then pull out the numbers between the ; and detect the
% end using the :

% Helper function for \popcurr
% Pops the first element of \currlist into #1
% Pushing text #1 onto nextlist or currlist
    \the\currlist #1}}
    \the\nextlist #1}}

% Calculate #1 digits of pi using the "spigot" algorithm. See
% spigot.pdf for more information about the algorithm. See
% iwriteiam.nl/SigProgC.html#pi for the C program that this
% version is based on.
  % Given the number of digits we want, we figure out how many
  % terms in the expansion we need for the digit to be correct.
  % Since we're calculating in groups of 4, we need 14n/4
  % terms.
  \multiply\numterms by14
  \divide\numterms by4
  % Initialize each of the terms to 2000 by pushing that many
  % terms onto the list.
    \advance\i by1
  % Now the main loop. We repeat this until we've calculated
  % all of the digits, keeping track of the carry as we go 
  % along.
  \loop\ifnum\digit>0 {%
    % We keep track of the carry from one term to the next
    % here.
    % Setup the list to receive the updated term remainders as
    % we calculate them.
    % Iterate through each of the terms
      % Pull the term out of the list
      % Calculate the sum of term+carry
      \multiply\carry by\i
      \multiply\arrval by10000
      \advance\carry by\arrval
      % Calculate the divisor 2i-1
      \multiply\divisor by2
      \advance\divisor by-1
      % The remainder goes into the next list, and the divisor
      % gets carried over.
      \rem\arrval=\carry mod\divisor
      \divide\carry by\divisor
      \advance\i by-1
    % Finish the new list and replace currlist for the next
    % loop
    % Calculate the actual digits to show, which are the
    % carry/10000 + remainder from the last loop.
    \divide\digits by10000
    \advance\digits by\loopcarry
    % We add 10000 to the digits and then remove the leading
    % 1 in \maybeaddpoint so that we don't miss leading 0s.
    \advance\digits by10000
    % Calculate the remainder to carry to the next iteration
    \rem{\global\loopcarry}=\carry mod{10000}%
    \global\advance\digit by-14

% Add a . after the first digit if this is the first group of
% digits. This turns 314159 into 3.14159. Also, add an \hfil
% in between each number so that we can break after each number
% and so we justify the digits.
\def\maybeaddpoint 1#1#2#3#4{%

% Setup the page for a business card size

% Actually print the output
\noindent $\pi={}$\pid{100}...\hfill~