Web Analytics Made Easy -
StatCounter Simple calculation problem - CodingForum

Announcement

Collapse
No announcement yet.

Simple calculation problem

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Simple calculation problem

    I need a script that does: "(4/1)-(4/3)+(4/5)-(4/7)+(4/9)-(4/11)+(4/13)-(4/15)..." and thereby calculates pi, and I'm having a bit of a problem.

    I've got this so far, and I'm coming nowhere:
    PHP Code:
    <?php
    $number
    =1;
    $var_1=4/$number;
    $n1=$number+1;
    $n2=$n1/2
    if(number/2<1000)
        {
        echo 
    $n2 ". " $var_1;
        
    $number+2;
        }
    ?>
    Please help me! I'm trying to make it like this:

    "
    1. 4
    2. 2
    3. 3.333
    4. 2.333

    etc..


    Thanks in advance,

    "DLA"
    Always Use a DOCTYPE!
    Always Use * {margin: 0; border: none; padding: 0;} in the top of your CSS stylesheet.
    Always Use only CSS for layout; never (X)HTML.
    Always Have fun while coding!

  • #2
    PHP Code:
    echo '1: 4<br />';
    $num=4;
        for(
    $i=3;$i<2002;$i+=2)
        {
            if((
    $i/2)%2==0)
            {
                
    $num += (4/$i);
            }
            else
            {
                
    $num -= (4/$i);
            }
            echo ((
    $i+1)/2;).': '.$num.'<br />';
        } 
    Ofcourse you'd need to a lore more iterations to get anything resembling an accurate value for pi (100,000+)

    I have to ask - why?
    http://www.hazelryan.co.uk

    Comment


    • #3
      Code:
      (4/[ICODE]1[/ICODE])-(4/3)+(4/[ICODE]5[/ICODE])-(4/7)+(4/[ICODE]9[/ICODE])-(4/11)+(4/[ICODE]13[/ICODE])-(4/15)...
      4*i + 1 for i = 0 ....n
      Code:
      (4/1)-(4/[ICODE]3[/ICODE])+(4/5)-(4/[ICODE]7[/ICODE])+(4/9)-(4/[ICODE]11[/ICODE])+(4/13)-(4/[ICODE]15[/ICODE])...
      4*i + 3 for i = 0 ...n
      therefor:
      sum of 4/(4*i+1) - 4/(4*i+3) will give what you want.
      but this is the sum of 4*(4*i+3 - 4*i-1)/((4*i+1)*(4*i + 3) = 8/((4*i+1)*(4*i+3)
      PHP Code:
      $nmax 1000;
      $result 0;
      for(
      $i 0$i $nmax$i++){
         
      $result += 8/((4*$i 1) * (*$i 3);

      best regards

      Comment


      • #4
        These didn't really work out.. Help? Could you post the entire HTML file?
        Always Use a DOCTYPE!
        Always Use * {margin: 0; border: none; padding: 0;} in the top of your CSS stylesheet.
        Always Use only CSS for layout; never (X)HTML.
        Always Have fun while coding!

        Comment


        • #5
          PHP Code:
          $nmax 1000;
          $result 0;
          for(
          $i 0$i $nmax$i++){
             
          $result += 8/((4*$i 1) * (*$i 3);
             echo 
          $result "<br/>"// <- this?

          Comment


          • #6
            Why doesn't this work? Is it my webhost? I used a .php ending...

            PHP Code:
            <html>
            <head>
            <title>justtesting</title>
            </head>
            <body>
            <?php
            $nmax 
            1000
            $result 0
            for(
            $i 0$i $nmax$i++){ 
               
            $result += 8/((4*$i 1) * (*$i 3); 
               echo 
            $result "<br/>"// <- this? 
            }
            ?>
            </body>
            </html>
            Always Use a DOCTYPE!
            Always Use * {margin: 0; border: none; padding: 0;} in the top of your CSS stylesheet.
            Always Use only CSS for layout; never (X)HTML.
            Always Have fun while coding!

            Comment


            • #7
              Originally posted by DarkLightA View Post
              Why doesn't this work? Is it my webhost? I used a .php ending...

              PHP Code:
              <html>
              <head>
              <title>justtesting</title>
              </head>
              <body>
              <?php
              $nmax 
              1000
              $result 0
              for(
              $i 0$i $nmax$i++){ 
                 
              $result += 8/((4*$i 1) * (*$i 3); 
                 echo 
              $result "<br/>"// <- this? 
              }
              ?>
              </body>
              </html>
              can you post a link to the test page?

              best regards

              Comment


              • #8
                I don't know how but seems that was missing a ')':
                PHP Code:
                <html>
                <head>
                <title>justtesting</title>
                </head>
                <body>
                <?php
                $nmax 
                1000
                $result 0
                for(
                $i 0$i $nmax$i++){ 
                   
                $result += 8/((4*$i 1) * (*$i 3));  //<- here was missing a ')'
                   
                echo $result "<br/>"// <- this? 
                }
                ?>
                </body>
                </html>
                best regards

                Comment


                • #9
                  That was perfect! THANKS! You saved my life!
                  Always Use a DOCTYPE!
                  Always Use * {margin: 0; border: none; padding: 0;} in the top of your CSS stylesheet.
                  Always Use only CSS for layout; never (X)HTML.
                  Always Have fun while coding!

                  Comment


                  • #10
                    Originally posted by DarkLightA View Post
                    That was perfect! THANKS! You saved my life!
                    youa are welcome,
                    I apologize is my fault, I lose that brackets and I didn't see. I was tested before I post but I don't know what happend mean time.

                    best regards

                    Comment


                    • #11
                      I'm trying to make the script more accurate, and thereby tried averaging two calculations in a row (for example: (((4)-(4/3))+((4)-(4/3)+(4/5)))/2 ). This is the code I have for it now:

                      PHP Code:
                      <html> 
                      <head> 
                      <title>justtesting</title> 
                      </head> 
                      <body> 
                      <?php 
                      $nmax 
                      100000;  
                      $result 0;
                      $number 1;
                      for(
                      $i 0$i $nmax$i++){  
                         
                      $result += (2*(8/((4*$i 1) * (*$i 3))) + (1/((4*$number) + 1)))); 
                         echo 
                      $number ") " $result "<br/>";
                         
                      $number++;

                      ?> 
                      </body> 
                      </html>
                      Also, could someone help me compress it a little? I run it with $nmax as high as my webhost can handle it (around $nmax 400,000)

                      Here is the original code, before I started editing:

                      PHP Code:
                      <html> 
                      <head> 
                      <title>justtesting</title> 
                      </head> 
                      <body> 
                      <?php 
                      $nmax 
                      500000;  
                      $result 0;
                      for(
                      $i 0$i $nmax$i++){  
                         
                      $result += 8/((4*$i 1) * (*$i 3)); 
                         echo 
                      $number ") " $result "<br/>";


                      ?> 
                      </body> 
                      </html>
                      Thanks in advance.

                      Regards,
                      "DLA"
                      Always Use a DOCTYPE!
                      Always Use * {margin: 0; border: none; padding: 0;} in the top of your CSS stylesheet.
                      Always Use only CSS for layout; never (X)HTML.
                      Always Have fun while coding!

                      Comment


                      • #12
                        the problem is that you echo each partial result, and this take a lot of time.
                        Using javascript. for example I can go without problems to a nmax 10,000,000.
                        put all the code in a function and return the result only, then call that function once.

                        Edit: maybe I'm wrong but this don't seems to compute pi

                        best regards

                        Comment


                        • #13
                          I just ran this with 100,000,000 iterations and it gets pi correct to 8 decimal places (3.14159264859)

                          PHP Code:
                          $pi 0;

                          $iterations 40000;

                          for ( 
                          $i 0$i $iterations$i++ )
                          {
                              
                          $pi += (4/(4*$i+1));
                              
                          $pi -= (4/(4*$i+3));
                          }

                          echo 
                          $pi

                          Comment


                          • #14
                            Would it be possible for it to make an average though? I find that then it's much more accurate (around 9-10 decimal places with only 400000 iterations).

                            Edit: Also, are there any webhosts (free) that are good for this stuff?
                            Last edited by DarkLightA; Apr 25, 2009, 04:06 PM.
                            Always Use a DOCTYPE!
                            Always Use * {margin: 0; border: none; padding: 0;} in the top of your CSS stylesheet.
                            Always Use only CSS for layout; never (X)HTML.
                            Always Have fun while coding!

                            Comment


                            • #15
                              Anyone?
                              Always Use a DOCTYPE!
                              Always Use * {margin: 0; border: none; padding: 0;} in the top of your CSS stylesheet.
                              Always Use only CSS for layout; never (X)HTML.
                              Always Have fun while coding!

                              Comment

                              Working...
                              X