Thread: New Program

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    101

    New Program

    So i just got assigned two assignments, but i will just start on one for now. Anyways, the assignment is "Bruckners Formula for pi". We need to make a program that solves this equation for pi:
    pi= 4/( 1 + (1^2/(2+3^2)/(2+5^2)/(2+n^2)) Sorry it was hard to write this o the computer. But we can assume the n input is odd and is non-negative. So, like the 1^2 is divided by 2+(3^2) and that part(3^2ONLY) is divided by 2+5^2 and 5^2 is divided by 2+n^2.... and so on.
    Anyways, I am so confused as to where to start... i try to start on it but i just get stuck. Can someone help me conceptualize this program?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So pick a reasonable n, like 13. Write down your formula all the way out, and use your knowledge of the algebraic order of operations to figure out the order in which things must happen. Once you have that, look for a pattern that you can exploit in a loop.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    101
    still clueless... can anyone help me get on the right path? i tried working it out, but when i go back to the computer i get nowhere.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If you can't do it on paper, there's no way you're going to get it in the computer. Let's check what you should have gotten for 13:
    13^2 = 169
    2 + this = 171
    11^2 = 121
    last line/two lines ago = 0.7076 (and so on)
    2 + this = 2.7076
    9^2 = 81
    last line/two lines ago = 29.9158 (and so on)
    2 + this = 31.9158
    7^2 = 49
    last line/two lines ago = 1.5353
    2 + this = 3.5353
    5^2 = 25
    last line/two lines ago = 7.072
    2 + this = 9.072
    3^2 = 9
    last line/two lines ago = 0.9921
    2 + this = 2.9921
    1^2 = 1
    last line/two lines ago = 0.3342

    1 + this = 1.3342
    4/this = 2.998

    I've never come across Bruckner's formula nor can I find it in any reference, so I don't know how fast it actually approaches pi, but that seems to be what we get.

    Hopefully you can see a pattern.

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    101
    YES! i can! thanks so much for the help... im thinkin it should be easy to do program the math inside the loop, but what should the condition to enter the loop be? i know the number they enter determines howm nay times but is it exactly 13 times? idk im confused there

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    You should have the number of decimal places determine when the loop ends, imo. Though this formula is VERY slow, so I wouldn't go all crazy and try to make the world record for digits of pi calculated.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Lucid15 View Post
    YES! i can! thanks so much for the help... im thinkin it should be easy to do program the math inside the loop, but what should the condition to enter the loop be? i know the number they enter determines howm nay times but is it exactly 13 times? idk im confused there
    Since the variable starts at <whatever they enter> and goes down to 1, I'm thinking it stops when you get to 1.

  8. #8
    Registered User
    Join Date
    Sep 2008
    Posts
    101
    ok so they enter 13 and i minus two from that number until it reaches 1 and then the loop should be over correct?

  9. #9
    Registered User
    Join Date
    Sep 2008
    Posts
    101
    so i tried to get the pow function to work, am i doing this wrong?
    Code:
    #include <FPT.h>
    int main()
    {
      double n,a,b;
      n=inD();
      a=1;
      b=1;
      while(n>=1){
       a= pow(n,2);
    
        outD(a);
      }
      
      }

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    pow is in math.h. I have no idea what you think that code is supposed to do, so all I can say is that you have an all-or-nothing loop; n doesn't change inside it, so if you get in you can never get out.

  11. #11
    Registered User
    Join Date
    Sep 2008
    Posts
    101
    I think it is in my header file now, cause it works, i got rid of the loop.

  12. #12
    Registered User
    Join Date
    Sep 2008
    Posts
    101
    Well i tried it out, but im getting wayyyyyyyy to big of a answer..... with this code
    Code:
    #include <FPT.h>
    int main()
    {
      double n,a,b,c;
      n=inD();
      a=1;
      b=1;
      while(n>1){
       a= pow(n,2);
       a=a+2;
       n=n-1;
       b=pow(n,2);
       c=(b/a) +2;
       n=n-1;
      }
      if(n==1){
        a=pow(n,2);
        c=(c/a)+1;
      }
    
    
    
        outD(4/c);
      
      
      }

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If n is supposed to go down by 2, why do you do n=n-1? Edit: Also, the body of your loop has way more than two lines in it, which is a problem.

  14. #14
    Registered User
    Join Date
    Sep 2008
    Posts
    101
    Ok problem fixed with the -1 i have no idea what i was thinking, and does it really matter how many lines are within the loop? i guess hes never told us that.

  15. #15
    Registered User
    Join Date
    Sep 2008
    Posts
    101
    Ok now my answer seems to get a little small.. for 13 i get: 1.2 and for any other higher number i get 1.8 and thats as high as it gets

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM