Program that calculates the value of PI

This is a discussion on Program that calculates the value of PI within the C Programming forums, part of the General Programming Boards category; I found this program that calculates the value of pi somewhere on the net: Code: #include <stdio.h> #define SCALE 10000 ...

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    75

    Program that calculates the value of PI

    I found this program that calculates the value of pi somewhere on the net:

    Code:
    #include <stdio.h>
    #define SCALE 10000
    #define MAXARR 2800
    #define ARRINIT 2000
    
    main()
    {
            int i, j;
            int carry = 0;
            int arr[MAXARR+1];
    
            for (i = 0; i <= MAXARR; ++i)
                    arr[i] = ARRINIT;
            for (i = MAXARR; i; i -= 14) {
                    int sum = 0;
                    for (j = i; j > 0; --j) {
                            sum = sum*j + SCALE*arr[j];
                            arr[j] = sum % (j*2-1);
                            sum /= (j*2-1);
                    }
                    printf("%04d", carry + sum/SCALE);
                    carry = sum % SCALE;
            }
    
    }
    Output:
    Code:
    31415926535897932384626433832795028841971693993751058209749445923078164062862089
    98628034825342117067982148086513282306647093844609550582231725359408128481117450
    28410270193852110555964462294895493038196442881097566593344612847564823378678316
    52712019091456485669234603486104543266482133936072602491412737245870066063155881
    74881520920962829254091715364367892590360011330530548820466521384146951941511609
    43305727036575959195309218611738193261179310511854807446237996274956735188575272
    48912279381830119491298336733624406566430860213949463952247371907021798609437027
    70539217176293176752384674818467669405132000568127145263560827785771342757789609
    17363717872146844090122495343014654958537105079227968925892354201995611212902196
    08640344181598136297747713099605187072113499999983729780499510597317328160963185
    Can someone explain what's happening here and why it works?

  2. #2
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Doesn't the site you picked it up from say anything about it ?
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    224
    I assume that PI can be calculated using a series, so that program must iterate repeatedly using the series formula. Of course, that's not the exact value of PI, but the more iterations you do, the greater the accuracy.

  4. #4
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    if there is nothing wrong with the code then don't post here. This is a math question. You should aks somewhere else.

  5. #5
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    I'm sure Ken wouldn't mind moving it to the tech board

  6. #6
    Registered User
    Join Date
    Jun 2006
    Posts
    75
    Quote Originally Posted by Happy_Reaper
    Doesn't the site you picked it up from say anything about it ?
    No, it doesn't. Here's the link: http://www.codecodex.com/wiki/index....pi_calculation

    Quote Originally Posted by twomers
    I'm sure Ken wouldn't mind moving it to the tech board
    Yeah, please do it.

  7. #7
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    It appears to use a "spigot" algorithm; try Googling it. BTW there are tons more formulae from en.wikipedia.org/wiki/Pi
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  8. #8
    Registered User
    Join Date
    Jun 2006
    Posts
    75

    Thumbs up

    Quote Originally Posted by jafet
    It appears to use a "spigot" algorithm; try Googling it. BTW there are tons more formulae from en.wikipedia.org/wiki/Pi
    Thanks a lot, Jafet

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 10:17 PM
  2. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  3. pi spigot program
    By eehiram in forum C Programming
    Replies: 10
    Last Post: 07-15-2006, 10:20 AM
  4. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 06:49 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21