Page 2 of 3 FirstFirst 123 LastLast
Results 26 to 50 of 61

Thread: Pi Script?

  1. #26
    Join Date
    Oct 2006
    Location
    Netherlands
    Posts
    3,285
    Mentioned
    105 Post(s)
    Quoted
    494 Post(s)

    Default

    But that is no longer the formula which was given to us . If that counts, so does the spigot algorithm I posted. Also you are comparing different stuff. I don't see how x iterations of your formula equals x iterations of mine. I won't let you win that easy ^^

    We need more clear rules!
    Working on: Tithe Farmer

  2. #27
    Join Date
    Oct 2006
    Location
    Netherlands
    Posts
    3,285
    Mentioned
    105 Post(s)
    Quoted
    494 Post(s)

    Default

    Quote Originally Posted by core View Post
    Au contraire, cher ami!

    That is indeed the same formula, it is just restructured. The steps to arrive at it are outlined in my post before. Just multiplied it out and factored out a 2.
    Ah, I am an idiot . Anyhow, results on my pc are different then yours, I get faster times with method. Reason is that processors aren't a fast with uints as they are with signed integers. But after optimizing your code by taking out * 16, yours was faster.

    Simba Code:
    function corePi (m : uint32) : Extended;
      var n : uint32;
    begin
      for n := 0 to m do
      begin
        result := result + 1.0 / (n * ((n shl 4) + 16) + 3)
      end;
      result := result * 8;
    end;
    Working on: Tithe Farmer

  3. #28
    Join Date
    Oct 2006
    Location
    Netherlands
    Posts
    3,285
    Mentioned
    105 Post(s)
    Quoted
    494 Post(s)

    Default

    Quote Originally Posted by core View Post
    I don't think its possible to go much faster with this formula
    You thought wrong:

    Simba Code:
    program new;

    function corePi(m : uint32) : Extended;
      var n : uint32;
    begin
      for n := 0 to m do
      begin
        result := result + 1.0 / (n * ((n * 16) + 16) + 3)
      end;
      result := result * 8;
    end;

    function corePi2(m : uint32) : Extended;
      var n : uint32;
    begin
      for n := 0 to m do
      begin
        result := result + 1.0 / (n * ((n shl 4) + 16) + 3)
      end;
      result := result * 8;
    end;

    function corePi3(m : uint32) : Extended;
      var n : Integer;
    begin
      for n := 0 to m do
        result := result + 0.5 / ((n * (n + 1.0)) + 0.1875);
    end

    var
      timez, i, trials, m:longint;
    begin
    trials := 10;
    m := 10000000;

    writeln('corePi: ' + FloatToStr(corePi(m)));
    writeln('corePi2: ' + FloatToStr(corePi2(m)));
    writeln('corePi3: ' + FloatToStr(corePi3(m)));

    timez := GetTickCount;
    for i:= 0 to trials do corePi(m);
    writeln('corePi: ' + FloatToStr((GetTickCount-timez)/trials) + 'ms');

    timez := GetTickCount;
    for i:= 0 to trials do corePi2(m);
    writeln('corePi2: ' + FloatToStr((GetTickCount-timez)/trials) + 'ms');

    timez := GetTickCount;
    for i:= 0 to trials do corePi3(m);
    writeln('corePi3: ' + FloatToStr((GetTickCount-timez)/trials) + 'ms');

    end.

    e:

    Results:
    corePi: 3.14159260358979
    corePi2: 3.14159260358979
    corePi3: 3.14159260358979
    corePi: 1748.8ms
    corePi2: 1483.5ms
    corePi3: 993.7ms
    Working on: Tithe Farmer

  4. #29
    Join Date
    Dec 2007
    Location
    192.168.1.73
    Posts
    2,439
    Mentioned
    6 Post(s)
    Quoted
    119 Post(s)

    Default

    Please tell me I'm not the only one who panicked when they saw the bitwise shifting?

    I'm starting to get concerned about my competence for starting this degree in September...

  5. #30
    Join Date
    Oct 2006
    Location
    Netherlands
    Posts
    3,285
    Mentioned
    105 Post(s)
    Quoted
    494 Post(s)

    Default

    Quote Originally Posted by Richard View Post
    Please tell me I'm not the only one who panicked when they saw the bitwise shifting?

    I'm starting to get concerned about my competence for starting this degree in September...
    Bitwise shifting is just a very fast multiply/divide operator. The only con is that you can only divide/multiply with powers of 2 (2, 4, 8, etc etc). There is also a restriction that it can only be used on integers, not on an extended for example

    example:

    10 shl 4 == 10 * 2^4 == 10 * 16

    10 shr 3 == 10 / 2^3 == 10 / 8
    Working on: Tithe Farmer

  6. #31
    Join Date
    Dec 2007
    Location
    192.168.1.73
    Posts
    2,439
    Mentioned
    6 Post(s)
    Quoted
    119 Post(s)

    Default

    Quote Originally Posted by masterBB View Post
    Bitwise shifting is just a very fast multiply/divide operator. The only con is that you can only divide/multiply with powers of 2 (2, 4, 8, etc etc). There is also a restriction that it can only be used on integers, not on an extended for example

    example:

    10 shl 4 == 10 * 2^4 == 10 * 16

    10 shr 3 == 10 / 2^3 == 10 / 8
    Ah, I remember now, thank you It has been far too long since I've programmed. It's all very stressful

    EDIT: Finally fixed my function doing it recursively. I don't know how to edit the stack size in Simba, so I did it in java. (you might want to edit the VM stack size to get some reasonable degree of accuracy with this)

    Code:
    public class Pi2 {
    
    	static int depth = 1000000;
    
    	public static void main(String[] args) {
    		double pi = 4.0 * pi2(-1);
    		System.out.print(pi);
    	}
    
    	private static double pi2(int i) {
    		i++;
    		if (i == depth)
    			return 1.0 / ((2.0 * i) + 1.0);
    		else
    			return Math.pow(-1, i) * (1.0 / ((2.0 * i) + 1.0)) + pi2(i);
    	}
    }
    Win.
    Last edited by Richard; 03-18-2013 at 12:05 AM.

  7. #32
    Join Date
    Oct 2006
    Location
    Netherlands
    Posts
    3,285
    Mentioned
    105 Post(s)
    Quoted
    494 Post(s)

    Default

    Quote Originally Posted by Richard View Post
    Finally fixed my function doing it recursively.
    Simba Code:
    function corePi4(m : uint32; n:Integer = 0) : Extended;
    begin
    * if n > m then
    * * result := 0
    * else
    * * result := 0.5 / ((n * (n + 1.0)) + 0.1875) + corePi4(m, n+1);
    end;
    *

    Note however that lape got a maximum amount of recursive calls it can do. Therefor this one will never be exact-ish.*

    Quote Originally Posted by core View Post
    Always a pleasure to get schooled by masterBB!
    It was also a pleasure to win. I enjoyed our script fight ^^ I like these kind of challenges. Next time I will beat you with a bigger distance for sure (nah, I can't pull this of twice)
    Working on: Tithe Farmer

  8. #33
    Join Date
    Oct 2006
    Location
    Netherlands
    Posts
    3,285
    Mentioned
    105 Post(s)
    Quoted
    494 Post(s)

    Default

    Quote Originally Posted by core View Post
    Simba Code:
    function corePi5(m : uint32) : Extended;
    begin
      result := 0.5 / ((m * (m + 1.0)) + 0.1875);
      if m > 0 then
        result := result + corePi4(m - 1);
    end;

    *Dusts off shoulders*
    Ignoring that the code you posted is actually using my code (+ corePi4) I improved it slightly, not really significant though.

    Simba Code:
    function corePi6(m : Integer) : Extended;
    begin
      result := 0.5 / ((m * (m + 1)) + 0.1875);
      if not(m = 0) then
        result := result + corePi6(m - 1);
    end;

    edit:
    note that I improved it for m numbers not much higher then 10000.

    edit2:
    my results were:
    7.208 ms
    6.396 ms

    with 10000 m and 1000 t
    Working on: Tithe Farmer

  9. #34
    Join Date
    Oct 2006
    Location
    Netherlands
    Posts
    3,285
    Mentioned
    105 Post(s)
    Quoted
    494 Post(s)

    Default

    Quote Originally Posted by core View Post
    This code does not produce results identical to all of the others, and is therefore invalid
    Hmm, how strange. Your results don't match any of mine:
    3.14154265858932
    3.14154265858932
    3.14154265858932
    Working on: Tithe Farmer

  10. #35
    Join Date
    Oct 2006
    Location
    Netherlands
    Posts
    3,285
    Mentioned
    105 Post(s)
    Quoted
    494 Post(s)

    Default

    Quote Originally Posted by core View Post
    See my edit. I used m := 275052, which I believe is the highest m possible to run.
    But what precision can we live with? The highest m mine was able to run with was 46340, because you tested it with 10000, I wanted to nail that test. My first quick write up was able to go much higher btw.

    e:
    after some testing I realized I am not going to make this much faster
    so the winner is:
    Core for a 46340 < m < 275052
    Me for 0 < m < 46340
    Last edited by masterBB; 03-18-2013 at 12:23 PM.
    Working on: Tithe Farmer

  11. #36
    Join Date
    May 2012
    Location
    Moscow, Russia
    Posts
    661
    Mentioned
    35 Post(s)
    Quoted
    102 Post(s)

    Default

    Simba Code:
    under construction

    Result:
    Simba Code:
    Compiled successfully in 219 ms.
     An       = 3.6356761630528E-6
     S(n-1)   = 0.78539907231979
     Pi/4     = 0.78539816339744
     4*S(n-1) = 3.14159628927917
     Pi       = 3.14159265358979
    Time:4.7ms
    Successfully executed.

    In LaPe.
    Last edited by CynicRus; 03-18-2013 at 12:31 PM.
    Per aspera ad Astra!
    ----------------------------------------
    Slow and steady wins the race.

  12. #37
    Join Date
    Oct 2006
    Location
    Netherlands
    Posts
    3,285
    Mentioned
    105 Post(s)
    Quoted
    494 Post(s)

    Default

    Quote Originally Posted by core View Post
    (or someone significantly beats us here)
    I am afraid another Russian joined this thread :S
    Working on: Tithe Farmer

  13. #38
    Join Date
    Oct 2006
    Location
    Netherlands
    Posts
    3,285
    Mentioned
    105 Post(s)
    Quoted
    494 Post(s)

    Default

    Quote Originally Posted by core View Post
    is against all the rules!
    The rules which have not been written, yet are so clear to us :P
    Working on: Tithe Farmer

  14. #39
    Join Date
    May 2012
    Location
    Moscow, Russia
    Posts
    661
    Mentioned
    35 Post(s)
    Quoted
    102 Post(s)

    Default

    Simba Code:
    function GetPi(e: Extended; var Cnt : Integer): extended;
    var
      i,sng : Integer;
      dx,res : Extended;
    begin
      I := 1;
      Sng := 1;
      dx := 1;

      while Abs(dx) > Abs(e) Do
        begin
          Res := Res + dx;
          Cnt:=Cnt+1;
          i:=i+2;
          Sng := Sng * (-1);
          dx := Sng/I;
        end;
      Res := Res * 4;
      Result:=Res;
    end;

    Output:
    Simba Code:
    Compiled successfully in 234 ms.
     Pi       = 3.14159065358979
    500000
    Time:7.8ms
    Successfully executed.
    Per aspera ad Astra!
    ----------------------------------------
    Slow and steady wins the race.

  15. #40
    Join Date
    May 2012
    Location
    Moscow, Russia
    Posts
    661
    Mentioned
    35 Post(s)
    Quoted
    102 Post(s)

    Default

    Quote Originally Posted by core View Post
    We mingle with the unseen and dance with the ephemeral

    I'm just curious as to why the super script is still "under construction"...
    Because there is not considered to Leibniz. I've used a monte-carlo method

    And you said that it is against the rules-)
    Per aspera ad Astra!
    ----------------------------------------
    Slow and steady wins the race.

  16. #41
    Join Date
    Oct 2006
    Location
    Netherlands
    Posts
    3,285
    Mentioned
    105 Post(s)
    Quoted
    494 Post(s)

    Default

    Quote Originally Posted by CynicRus View Post
    Simba Code:
    function GetPi(e: Extended; var Cnt : Integer): extended;
    var
      i,sng : Integer;
      dx,res : Extended;
    begin
      I := 1;
      Sng := 1;
      dx := 1;

      while Abs(dx) > Abs(e) Do
        begin
          Res := Res + dx;
          Cnt:=Cnt+1;
          i:=i+2;
          Sng := Sng * (-1);
          dx := Sng/I;
        end;
      Res := Res * 4;
      Result:=Res;
    end;

    Output:
    Simba Code:
    Compiled successfully in 234 ms.
     Pi       = 3.14159065358979
    500000
    Time:7.8ms
    Successfully executed.
    In my bench:

    corePi: 3.14159260358979
    corePi2: 3.14159260358979
    corePi3: 3.14159260358979
    GetPi: 3.14159260358979
    corePi: 1641.1ms
    corePi2: 1435.2ms
    corePi3: 996.9ms
    GetPi: 1778.5ms
    Working on: Tithe Farmer

  17. #42
    Join Date
    Dec 2007
    Location
    192.168.1.73
    Posts
    2,439
    Mentioned
    6 Post(s)
    Quoted
    119 Post(s)

    Default

    Quote Originally Posted by masterBB View Post
    Simba Code:
    function corePi4(m : uint32; n:Integer = 0) : Extended;
    begin
    * if n > m then
    * * result := 0
    * else
    * * result := 0.5 / ((n * (n + 1.0)) + 0.1875) + corePi4(m, n+1);
    end;
    *

    Note however that lape got a maximum amount of recursive calls it can do. Therefor this one will never be exact-ish.*



    It was also a pleasure to win. I enjoyed our script fight ^^ I like these kind of challenges. Next time I will beat you with a bigger distance for sure (nah, I can't pull this of twice)
    Yeah, that's the general issue with using this formula. This is why I did it in java in the end, because I don't know how to change the stack size in Simba.

    However, even when editing the stack size, you still need to go to a huge depth of recursion to gain a reasonable degree of accuracy.

  18. #43
    Join Date
    May 2012
    Location
    Moscow, Russia
    Posts
    661
    Mentioned
    35 Post(s)
    Quoted
    102 Post(s)

    Default

    Quote Originally Posted by core View Post
    We are the champions, my friend, even with Cynic's different algorithm!
    Simba Code:
    function GetPi(m : integer) : extended;
    var s : extended;
        i : integer;
    begin
         s:=0;
         for i:=0 to m do
              s:=s+(pow((-1),i)*factorial(6*i)*(13591409 + 545140134*i))/(factorial(3*i)*pow(factorial(i),3)*pow(640320,3*i+3/2));
         s := 1/(s * 12);
         result:=s;
    end;

    var
    timez: integer;
    begin
      timez:=GetTickCount;
      WriteLn(' Pi       = '+ToStr(GetPi(10)));
      WriteLn('Time:'+FloatToStr((GetTickCount-timez)/10) + 'ms');
    end.

    Successfully executed.
    Compiled successfully in 250 ms.
    Pi = 3.14159265358979
    Time:0ms
    Successfully executed.
    Per aspera ad Astra!
    ----------------------------------------
    Slow and steady wins the race.

  19. #44
    Join Date
    Mar 2012
    Location
    127.0.0.1
    Posts
    3,383
    Mentioned
    95 Post(s)
    Quoted
    717 Post(s)

    Default

    Quote Originally Posted by CynicRus View Post
    Simba Code:
    function GetPi(m : integer) : extended;
    var s : extended;
        i : integer;
    begin
         s:=0;
         for i:=0 to m do
              s:=s+(pow((-1),i)*factorial(6*i)*(13591409 + 545140134*i))/(factorial(3*i)*pow(factorial(i),3)*pow(640320,3*i+3/2));
         s := 1/(s * 12);
         result:=s;
    end;

    var
    timez: integer;
    begin
      timez:=GetTickCount;
      WriteLn(' Pi       = '+ToStr(GetPi(10)));
      WriteLn('Time:'+FloatToStr((GetTickCount-timez)/10) + 'ms');
    end.

    Successfully executed.
    Compiled successfully in 250 ms.
    Pi = 3.14159265358979
    Time:0ms
    Successfully executed.
    0 ms...? CAN YOU BEAT IT?!

Page 2 of 3 FirstFirst 123 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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