C# School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a C# Expert!

Join 300,355 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,949 people online right now. Registration is fast and FREE... Join Now!




Formula to find digits of Pi?

 

Formula to find digits of Pi?, Just practicing a bit lol.

papuccino1

1 Jul, 2009 - 06:46 AM
Post #1

C# Programmer
Group Icon

Joined: 2 Mar, 2008
Posts: 943



Thanked: 33 times
Dream Kudos: 50
My Contributions
Hey guys,

I want to make a program that keeps adding a digit of Pi to a string each time a user presses a button.

What's the formula to find Pi? :S Google isn't helping at all!

User is offlineProfile CardPM
+Quote Post


ragingben

RE: Formula To Find Digits Of Pi?

1 Jul, 2009 - 07:18 AM
Post #2

D.I.C Head
Group Icon

Joined: 7 Oct, 2008
Posts: 153



Thanked: 9 times
Dream Kudos: 150
My Contributions
Hey, heres a VERY BASIC snippit that will display PI place by place for you to toy with, it isn't great though, it treats Math.PI as a string and just cuts out the appropriate digit. If you want a more exact PI than 15 decimal places you'll have too look it up anduse that instead. Hope that helps a bit!
CODE

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace pi
{
    class Program
    {
        static void Main(string[] args)
        {
            // itterate 15 times
            for (Int16 index = 0; index < 16; index++)
            {
                // display
                Console.Write(returnPIDecimalPlace(index));
            }
            // wait for input
            Console.Read();
        }

        /// <summary>
        /// Returns a decimal place of PI
        /// </summary>
        /// <param name="decimalPlace"></param>
        /// <returns></returns>
        private static String returnPIDecimalPlace(Int32 decimalPlace)
        {
            // if place element
            if(decimalPlace == 1)
            {
                // return place
                return ".";
            }  
            else
            {
                // return element
                return Math.PI.ToString().Substring(decimalPlace, 1);
            }
        }
    }
}


User is offlineProfile CardPM
+Quote Post

papuccino1

RE: Formula To Find Digits Of Pi?

1 Jul, 2009 - 07:30 AM
Post #3

C# Programmer
Group Icon

Joined: 2 Mar, 2008
Posts: 943



Thanked: 33 times
Dream Kudos: 50
My Contributions
Thanks for the help. But that's just using the Math.Pi class and substring to pull a char from an already set string. I'm looking for a way to actually calculate Pi so the computer calculates it indefinitely each time a user presses a button.
User is offlineProfile CardPM
+Quote Post

ragingben

RE: Formula To Find Digits Of Pi?

1 Jul, 2009 - 07:52 AM
Post #4

D.I.C Head
Group Icon

Joined: 7 Oct, 2008
Posts: 153



Thanked: 9 times
Dream Kudos: 150
My Contributions
Ah I see... sorry can't help!
User is offlineProfile CardPM
+Quote Post

Renagado

RE: Formula To Find Digits Of Pi?

1 Jul, 2009 - 07:55 AM
Post #5

D.I.C Head
Group Icon

Joined: 14 Jun, 2009
Posts: 180



Thanked: 16 times
Dream Kudos: 100
My Contributions
Well as you know the number Pi is based on the diameter and circumference of a circle. A way to approximate this is to create an imaginary polygon, and then calculate it's circumference. The more sides you give the polygon the better it represents Pi. You'll abviously need a lot of sides to get even a few decimals right.

That's the way it's done, am sure google can give you some concrete formulaes.
User is offlineProfile CardPM
+Quote Post

janne_panne

RE: Formula To Find Digits Of Pi?

1 Jul, 2009 - 09:57 AM
Post #6

D.I.C Addict
****

Joined: 9 Jun, 2009
Posts: 527



Thanked: 107 times
My Contributions
You should take a look at BBP formula:
http://en.wikipedia.org/wiki/Bailey-Borwein-Plouffe_formula

Here is the BBP formula in use, coded with Python:
http://en.literateprograms.org/Pi_with_the...ormula_(Python)

That might be what you are looking for as it is used to calculate single digit of pi instead of all of them.
Usually if you want to calculate the 100th digit, you have to calculate all digits from 0 -> 100
But that calculates only the 100th.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/7/09 07:29PM

Live C# Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month