Results 1 to 8 of 8

Thread: Pi to that whatever digit

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2008
    Posts
    4

    Pi to that whatever digit

    Is there a easy way to get Pi to whatever digit you want, like some way to calculate PI to the 100th place or 500th place

  2. #2
    Frenzied Member MaximilianMayrhofer's Avatar
    Join Date
    Aug 2007
    Location
    IM IN YR LOOP
    Posts
    2,001

    Re: Pi to that whatever digit

    Code taken from http://www.omegacoder.com/?p=91
    Code:
    public class CalculatePI
    
    {
    
        /*
    
         * Computation of the n'th decimal digit of \pi with very little memory.
    
         * Written by Fabrice Bellard on January 8, 1997.
    
         * 
    
         * We use a slightly modified version of the method described by Simon
    
         * Plouffe in "On the Computation of the n'th decimal digit of various
    
         * transcendental numbers" (November 1996). We have modified the algorithm
    
         * to get a running time of O(n^2) instead of O(n^3log(n)^3).
    
         * 
    
         * This program uses mostly integer arithmetic. It may be slow on some
    
         * hardwares where integer multiplications and divisons must be done
    
         * by software. We have supposed that 'int' has a size of 32 bits. If
    
         * your compiler supports 'long long' integers of 64 bits, you may use
    
         * the integer version of 'mul_mod' (see HAS_LONG_LONG).  
    
         */
    
     
    
        // Call this static to use.
    
        public static string Process(int digits)
    
        {
    
            StringBuilder result = new StringBuilder();
    
     
    
            result.Append("3.");
    
     
    
            DateTime StartTime = DateTime.Now;
    
     
    
            if (digits > 0)
    
            {
    
     
    
                for (int i = 0; i < digits; i += 9)
    
                {
    
                    String ds = CalculatePiDigits(i + 1);
    
                    int digitCount = Math.Min(digits - i, 9);
    
     
    
                    if (ds.Length < 9)
    
                        ds = string.Format("{0:D9}", int.Parse(ds));
    
     
    
                    result.Append(ds.Substring(0, digitCount));
    
                }
    
            }
    
     
    
            return result.ToString();
    
        }
    
     
    
     
    
        private static int mul_mod(int a, int b, int m)
    
        {
    
            return (int)(((long)a * (long)b) % m);
    
        }
    
     
    
        /* return the inverse of x mod y */
    
        private static int inv_mod(int x, int y)
    
        {
    
            int q, u, v, a, c, t;
    
     
    
            u = x;
    
            v = y;
    
            c = 1;
    
            a = 0;
    
     
    
            do
    
            {
    
                q = v / u;
    
     
    
                t = c;
    
                c = a - q * c;
    
                a = t;
    
     
    
                t = u;
    
                u = v - q * u;
    
                v = t;
    
            } while (u != 0);
    
     
    
            a = a % y;
    
     
    
            if (a < 0)
    
            {
    
                a = y + a;
    
            }
    
     
    
            return a;
    
        }
    
     
    
        /* return (a^b) mod m */
    
        private static int pow_mod(int a, int b, int m)
    
        {
    
            int r, aa;
    
     
    
            r = 1;
    
            aa = a;
    
     
    
            while (true)
    
            {
    
                if ((b & 1) != 0)
    
                {
    
                    r = mul_mod(r, aa, m);
    
                }
    
     
    
                b = b >> 1;
    
     
    
                if (b == 0)
    
                {
    
                    break;
    
                }
    
     
    
                aa = mul_mod(aa, aa, m);
    
            }
    
     
    
            return r;
    
        }
    
     
    
        /* return true if n is prime */
    
        private static bool is_prime(int n)
    
        {
    
            if ((n % 2) == 0)
    
            {
    
                return false;
    
            }
    
     
    
            int r = (int)Math.Sqrt(n);
    
     
    
            for (int i = 3; i <= r; i += 2)
    
            {
    
                if ((n % i) == 0)
    
                {
    
                    return false;
    
                }
    
            }
    
     
    
            return true;
    
        }
    
     
    
        /* return the prime number immediatly after n */
    
        private static int next_prime(int n)
    
        {
    
            do
    
            {
    
                n++;
    
            } while (!is_prime(n));
    
     
    
            return n;
    
        }
    
     
    
        private static String CalculatePiDigits(int n)
    
        {
    
            int av, vmax, num, den, s, t;
    
     
    
            int N = (int)((n + 20) * Math.Log(10) / Math.Log(2));
    
     
    
            double sum = 0;
    
     
    
            for (int a = 3; a <= (2 * N); a = next_prime(a))
    
            {
    
                vmax = (int)(Math.Log(2 * N) / Math.Log(a));
    
     
    
                av = 1;
    
     
    
                for (int i = 0; i < vmax; i++)
    
                {
    
                    av = av * a;
    
                }
    
     
    
                s = 0;
    
                num = 1;
    
                den = 1;
    
                int v = 0;
    
                int kq = 1;
    
                int kq2 = 1;
    
     
    
                for (int k = 1; k <= N; k++)
    
                {
    
     
    
                    t = k;
    
     
    
                    if (kq >= a)
    
                    {
    
                        do
    
                        {
    
                            t = t / a;
    
                            v--;
    
                        } while ((t % a) == 0);
    
     
    
                        kq = 0;
    
                    }
    
                    kq++;
    
                    num = mul_mod(num, t, av);
    
     
    
                    t = 2 * k - 1;
    
     
    
                    if (kq2 >= a)
    
                    {
    
                        if (kq2 == a)
    
                        {
    
                            do
    
                            {
    
                                t = t / a;
    
                                v++;
    
                            } while ((t % a) == 0);
    
                        }
    
                        kq2 -= a;
    
                    }
    
                    den = mul_mod(den, t, av);
    
                    kq2 += 2;
    
     
    
                    if (v > 0)
    
                    {
    
                        t = inv_mod(den, av);
    
                        t = mul_mod(t, num, av);
    
                        t = mul_mod(t, k, av);
    
     
    
                        for (int i = v; i < vmax; i++)
    
                        {
    
                            t = mul_mod(t, a, av);
    
                        }
    
     
    
                        s += t;
    
     
    
                        if (s >= av)
    
                        {
    
                            s -= av;
    
                        }
    
                    }
    
     
    
                }
    
     
    
                t = pow_mod(10, n - 1, av);
    
                s = mul_mod(s, t, av);
    
                sum = (sum + (double)s / (double)av) % 1.0;
    
            }
    
     
    
            int Result = (int)(sum * 1e9);
    
     
    
            String StringResult = String.Format("{0:D9}", Result);
    
     
    
            return StringResult;
    
        }
    
     
    
        // Put a space between every group of 10 digits.
    
     
    
        private static String breakDigitsIntoGroupsOf10(String digits)
    
        {
    
            String result = "";
    
     
    
            while (digits.Length > 10)
    
            {
    
                result += digits.Substring(0, 10) + " ";
    
                digits = digits.Substring(10, digits.Length - 10);
    
            }
    
     
    
            result += digits;
    
     
    
            return result;
    
        }
    
    }

  3. #3
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,628

    Re: Pi to that whatever digit

    it doesn't come out to a decimel ever, but isn't it equivalent to 22/7?
    My light show youtube page (it's made the news) www.youtube.com/@lightsofelberfeld
    Contact me on the socials www.facebook.com/lordorwell

  4. #4
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: Pi to that whatever digit

    Quote Originally Posted by Lord Orwell
    it doesn't come out to a decimel ever, but isn't it equivalent to 22/7?
    Are you trying to say Pi is equal to 22/7? It's definately not!!!

    22/7 is just an approximation, which is only equal to pi in the first three digits...:

    22/7 = 3.142857...
    Pi = 3.141593...

  5. #5
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    Re: Pi to that whatever digit

    Quote Originally Posted by Lord Orwell
    it doesn't come out to a decimel ever, but isn't it equivalent to 22/7?
    355/113 is a far more accurate approximation than 22/7.

    Also, you can let VB6 compute the following: PI = 4 * Atn(1) and get it accurate to 14 decimal places.
    Doctor Ed

  6. #6
    New Member
    Join Date
    Jul 2008
    Posts
    4

    Re: Pi to that whatever digit

    Why not memorize it?
    3.141592653589793238462643383279502884197169399375105820974944

    ^ Gave me 100 Points of extra credit in math 8th grade year...

  7. #7
    Fanatic Member
    Join Date
    May 2005
    Posts
    528

    Re: Pi to that whatever digit

    try the pi calculator in my sig:

  8. #8
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,628

    Re: Pi to that whatever digit

    i only memorized it to ten digits. That's all my commodore 64 showed.
    My light show youtube page (it's made the news) www.youtube.com/@lightsofelberfeld
    Contact me on the socials www.facebook.com/lordorwell

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width