Historical Note

This page was migrated from the original p-nand-q.com site which was last updated in 2015. The content has been preserved exactly as it was, with only formatting updated for modern browsers. Over the coming days and weeks, the content will be reviewed and may be updated for accuracy and relevance. If you find any issues, please contact me.

Calculating pi to an arbitrary number of digits

Here is a really usefull helper class that you can use to calculate PI to an arbitrary number of (string) digits: download VC2005 beta project. Note the heavy use of Trace.Assert() to make sure the code works. If you want to see how this looks in another language, have a look at the Java version of the algorithm.

#region Using directives

using System;
using System.Collections;
using System.Text;
using System.Diagnostics;

#endregion

namespace pi
{
    class pi
    {
        void LOOP()
        {
            Trace.Assert(!this == this - (!this + ~this));
            Trace.Assert(this * (o[(this > !this) - !this]
                * ((~this * ~this + !this) * ~this) + (this >
                (this < (~this * ~this * ~this - !this)))) ==
                (o[(this > !this) - !this] * ((~this * ~this
                + !this) * ~this) + (this > (this < (~this
                * ~this * ~this - !this)))));
            Trace.Assert(!this == this - (~this * ~this * ~this
                - !this));
            Trace.Assert(this * ((this < (!this + ~this)) /
                ((this > ~this) - !this)) == ((this <
                (!this + ~this)) / ((this > ~this) - !this)));
            Trace.Assert(~this == _((this < (!this + ~this))
                % ((this > ~this) - !this)));
        }

        public string digits()
        {
            GOTO NOT = loop;
            goto label0000;
        label0013:
            // This changes code-flow to label0000
            goto label0014;
        loop:
            goto label0015;
        label0016:
            NOT = LOOP;
            goto label0000;
        label0002:
            goto label0003;
        label0004:
            goto label0011;
        label0012:
            goto label0009;
        label0010:
            goto label0007;
        label0008:
            if ((this < (~this * ~this + !this + !this)) !=
                ((~this * ~this + !this) * ~this))
                goto label0017;
            Trace.Assert(this >= (~this * ~this + !this + !this));
            goto label0001;
        label0017:
            if ((this < (~this * ~this)) == (!this - !this))
                goto label0018;
            s.Append((this < (~this * ~this * ~this)).ToString());
        label0018:
            Trace.Assert(null != new pi[3] { new pi(this % (this 
                < ~this)), new pi(this * ((this < !this) + !this))
                , new pi(this % (this > !this)) });
            if (this / +this)
                goto loop;
            return s.ToString();
        label0000:
            Trace.Assert(this <= (int)this);
        LOOP:
            NOT();
            goto label0005;
        label0006:
            if ((this > !this) > (!this - !this))
                goto LOOP;
            if (NOT == loop)
                goto label0013;
            goto label0002;
        label0001:
            Trace.Assert(!this == this - (~this * ~this * ~this));
            new pi(this * ((this < (~this * ~this * ~this)) + !this));
            goto label0017;
        label0003:
            Trace.Assert(!this == this - (~this * ~this * ~this));
            goto label0004;
        label0005:
            Trace.Assert(!this == this - (~this * ~this + !this));
            new pi(this * ((this > !this) - !this));
            goto label0006;
        label0007:
            Trace.Assert(this * ((this < (~this * ~this * ~this - !this))
                % ((~this * ~this + !this) * ~this))
                == ((this < (~this * ~this * ~this
                - !this)) % ((~this * ~this + !this) * ~this)));
            goto label0008;
        label0009:
            Trace.Assert(this * ((this < (~this * ~this))
                + ((this < (~this * ~this * ~this - !this))
                / ((~this * ~this + !this) * ~this)))
                == ((this < (~this * ~this)) + ((this < (~this * ~this
                * ~this - !this)) / ((~this * ~this + !this) * ~this))));
            Trace.Assert(!this == this - (~this * ~this));
            goto label0010;
        label0011:
            Trace.Assert(this * (this < (~this * ~this + !this + !this))
                == (this < (~this * ~this + !this + !this)));
            Trace.Assert(!this == this - (~this * ~this + !this + !this));
            goto label0012;
        label0014:
            Trace.Assert(!this == this - !this);
            Trace.Assert(this * (!this - !this) == (!this - !this));
            goto loop;
        label0015:
            Trace.Assert(!this == this - (~this * ~this * ~this - !this));
            Trace.Assert(this * (!this - !this) == (!this - !this));
            goto label0016;
        } 
        
        int[] o = null;
        
        public static int operator +(pi _, int o)
        {
            return _.o[_.o.Length - o];
        }

        public static int operator -(pi _, int o)
        {
            _.O.Push(o);
            return !_;
        }

        public static int operator *(pi _, int o)
        {
            _.o[_.o.Length - (int)_.O.Pop()] = o;
            return o;
        }

        public static bool operator /(pi _, int o)
        {
            return _.o[_.o.Length - (int)_.O.Pop()] < o;
        }

        public static int operator !(pi _)
        {
            return 1;
        }

        public static int operator +(pi _)
        {
            return _.o[_.o.Length - ~_];
        }

        public static int operator %(pi _, int o)
        {
            return _ - !_;
        }

        public pi(int _)
        {
            o = new int[_ * ((~this * ~this + !this) * ~this)
                / (!this + ~this) + 60];
            o[o.Length - ~this] = _ + ~this;
        }

        public static implicit operator int(pi _)
        {
            return ((+_) * ((~_ * ~_ + !_) * ~_)) / (~_ + !_);
        }

        public static int operator ~(pi _)
        {
            return !_ + !_;
        }

        public static implicit operator string(pi _)
        {
            return _.digits();
        }

        public static bool operator <=(pi _, int o)
        {
            Trace.Assert(_ - (~_ * ~_ + !_) == !_);
            o = _ * o;
            return true;
        }

        Stack O = new Stack();
        public static bool operator >=(pi _, int o)
        {
            Trace.Assert(_ - o == !_);
            return (_ * (!_ - !_)) == (!_ - !_);
        }

        public static int operator <(pi _, int o)
        {
            return _.o[_.o.Length - o];
        }

        public static int operator >(pi _, int o)
        {
            return (_ < (~_ * ~_ + !_)) * o;
        }

        int _(int _)
        {
            o[(this > !this) - !this] = _;
            return ~this;
        }

        public delegate void GOTO();
        StringBuilder s = new StringBuilder();

        void loop()
        {
            _(~this);
        }

    } // end of class.

    class Program
    {
        static void Main(string[] args)
        {
            System.Console.WriteLine((string)new pi(20));
        }
    }
}