Bailey–Borwein–Plouffe Iterations

16

Bailey–Borwein–Plouffe Iterations

We've seen a few pi challenges on PPCG, but none that specifically dictate the algorithm you should be using. I'd like to see implementations of the Bailey–Borwein–Plouffe algorithm in any language up to iteration n. The formula is as follows:

Modified formula.

Your algorithm should output each iteration up to n, showing intermediate sums as well as the final result to form a "piangle". You may also use the reduced polynomial form of the algorithm shown on the wikipedia page. An example run for n=50 is shown below:

3
3.1
3.14
3.141
3.1415
3.14159
3.141592
3.1415926
3.14159265
3.141592653
3.1415926535
3.14159265358
3.141592653589
3.1415926535897
3.14159265358979
3.141592653589793
3.1415926535897932
3.14159265358979323
3.141592653589793238
3.1415926535897932384
3.14159265358979323846
3.141592653589793238462
3.1415926535897932384626
3.14159265358979323846264
3.141592653589793238462643
3.1415926535897932384626433
3.14159265358979323846264338
3.141592653589793238462643383
3.1415926535897932384626433832
3.14159265358979323846264338327
3.141592653589793238462643383279
3.1415926535897932384626433832795
3.14159265358979323846264338327950
3.141592653589793238462643383279502
3.1415926535897932384626433832795028
3.14159265358979323846264338327950288
3.141592653589793238462643383279502884
3.1415926535897932384626433832795028841
3.14159265358979323846264338327950288419
3.141592653589793238462643383279502884197
3.1415926535897932384626433832795028841971
3.14159265358979323846264338327950288419716
3.141592653589793238462643383279502884197169
3.1415926535897932384626433832795028841971693
3.14159265358979323846264338327950288419716939
3.141592653589793238462643383279502884197169399
3.1415926535897932384626433832795028841971693993
3.14159265358979323846264338327950288419716939937
3.141592653589793238462643383279502884197169399375
3.1415926535897932384626433832795028841971693993751
3.14159265358979323846264338327950288419716939937510

The precision of each iteration should equal the n that is passed to the algorithm, that is to say that each iteration should calculate pi up to the passed n for all k.

Rules:

  • Built-ins are not allowed, nor is pi, you must use the formula.
  • You must support n up to a maximum that your language allows in terms of the calculation of 16^n. If the input is causing an arithmetic overflow during calculation after x<n executions because your language only supports decimals up to 2^32-1, this is fine. Any other assumptions on n are not fine.
  • You MUST provide an explanation of how you got the output if it is not obvious. For instance, if you're posting in a Golfing language a break-down is 100% required. This is to ensure you're using the algorithm that is specified.
  • Standard loop-holes are disallowed.
  • This is code-golf, lowest byte count wins here.

Reference Code (Code used to Generate Example):

public static void main(String[] args) {
    (0..50).each {
        n->
        def x=(0..n).collect {
            j->
            def k=new BigDecimal(j)
            def s={it.setScale(n)}
            def a=s(1.0g).divide(s(16.0g)**s(k))
            def b=s(4.0g)/(s(8.0g)*s(k)+s(1.0g))
            def c=s(2.0g)/(s(8.0g)*s(k)+s(4.0g))
            def d=s(1.0g)/(s(8.0g)*s(k)+s(5.0g))
            def e=s(1.0g)/(s(8.0g)*s(k)+s(6.0g))
            def f=a*(b-c-d-e)
        }.sum()
        println(n + "\t" + x.setScale(n, BigDecimal.ROUND_DOWN))
    }
}

This implementation caps out at n=255, you may cap out at less or more.
This implementation was done in Groovy.

Magic Octopus Urn

Posted 2016-10-27T17:06:13.043

Reputation: 19 422

5The only downside I see is that it will be difficult to verify exactly what method someone is using soley based on the output, which is generally a problem with Calculate foo via x method challenges. – James – 2016-10-27T17:11:58.200

@DJMcMayhem Added an explanation of the code you post being required if it is not an obvious implementation, to ensure we can actually tell what they did. The algorithm is actually fairly straightforward though, so it shouldn't be too bad. – Magic Octopus Urn – 2016-10-27T17:14:34.913

@Emigna it should correctly calculate pi to k digits on each iteration using the algorithm provided. So the precision is important. – Magic Octopus Urn – 2016-10-27T17:27:41.223

2

With respect to @DJMcMayhem's comment, see the advice to avoid non-observable program requirements.

– Peter Taylor – 2016-10-27T17:45:28.083

@carusocomputing: What I meant was, is it okay to output more than n digits, where the digits after the nth may or may not be correct or if the result should be exactly n digits, but I removed the comment as it doesn't really matter. – Emigna – 2016-10-27T17:58:05.697

@Emigna the overall goal was to match the output provided using the algorithm specified for all n, probably would've been better to not use the rounding at all; as that would've made it more observable, but it's a bit late now. – Magic Octopus Urn – 2016-10-27T18:00:24.440

2You must support n up to a maximum that your language allows. Allows how? Can I use recursion? Can I use lists if generators would be more memory-friendly? Can I use 2n digits and chop the last n off? – Dennis – 2016-10-27T18:58:23.233

1In the interest of clarity, I'd just remove the ordinals before that output that is actually required. – Dennis – 2016-10-27T20:08:28.637

Answers

8

05AB1E, 63 52 50 bytes

Specialization formula

΃0NU62201122vy͹̰*8X*N>+÷+}16Xm÷+DX>£X__iÀ'.ìÁ},

Try it online!

BBP formula

ƒ4¹>°UX*8N*©>÷YX*®4+÷-1X*®5+÷-1X*®6+÷-1X*16Nm÷*ODN>£N__iÀ'.ìÁ},

Try it online!

Emigna

Posted 2016-10-27T17:06:13.043

Reputation: 50 798

1"Your algorithm should output each iteration up to n, showing intermediate sums as well as the final result to form a "piangle".", basically just perform this from 0 to n, pushing each to the stack and it'll be good. – Magic Octopus Urn – 2016-10-27T18:31:08.843

1@carusocomputing: Maybe change the wording on Outputting the current iteration's n is optional as I understood that as only the final result is necessary. – Emigna – 2016-10-27T18:33:17.493

Or maybe it's just me that's bad at reading (I know I tend to skip parts when I feel that I have the gist) – Emigna – 2016-10-27T18:34:24.453

4Maybe just us, but definitely not just you. – Dennis – 2016-10-27T18:35:47.067

@carusocomputing: Iterations added. Need to find a cheaper way to do it as the "." was very expensive. – Emigna – 2016-10-27T18:41:09.260

Could you suggest an edit for the wording? I'm not sure how else to word it. I meant literally outputting the n. – Magic Octopus Urn – 2016-10-27T18:52:51.543

@carusocomputing Something like in addition to printing the k digits calculated at each step of the algorithm, you may also output the current iteration number. Although I can't see why that bit is there at all as it's very unlikely that doing so will save bytes. – Emigna – 2016-10-27T18:57:20.337

I think the original wording is just fine. "Outputting the current iteration's n is optional." n is the iteration number, not the intermediate sum. – LarsH – 2016-10-27T20:11:16.670

so uh separate question: is there a reason to use every character in unicode set but english alphabets – Muhammad Umer – 2017-11-05T20:38:37.417

@MuhammadUmer: Not exactly sure what you're asking. There are only a few characters in this answer that aren't numbers or in the latin alphabet and I think I needed them. This answer could be made shorter today though. – Emigna – 2017-11-05T20:56:52.973

5

Python 2, 109 108 bytes

def f(n):k=1;s=0;t=100**n;exec-~n*'s+=4*t/k-2*t/(k+3)-t/(k+4)-t/(k+5)>>k/2;print"3."[:k]+`s`[1:k/8+1];k+=8;'

Test it on Ideone.

Dennis

Posted 2016-10-27T17:06:13.043

Reputation: 196 637

3

C GCC, 118 bytes

Golfed:

main(){double k,a,s=1,t;k=a=0;while(k<15){t=k++*8;a+=(4/(t+1)-2/(t+4)-1/(t+5)-1/(t+6))/s;s*=16;printf("%.15lf\n",a);}}

Ungolfed:

main(){
    double k,a,s=1,t;
    k=a=0;
    while(k<15){
        t=k++*8;
        a+=(4/(t+1)-2/(t+4)-1/(t+5)-1/(t+6))/s;
        s*=16;
        printf("%.15lf\n",a);
    }
}

To change n, just change while(k<15) to while(k < n)

output:

$ gcc pigolf.c -o pigolf
some gcc screaming warnings
$ ./pigolf 
3.133333333333333
3.141422466422466
3.141587390346582
3.141592457567436
3.141592645460336
3.141592653228088
3.141592653572881
3.141592653588973
3.141592653589752
3.141592653589791
3.141592653589793
3.141592653589793
3.141592653589793
3.141592653589793
3.141592653589793

max precision is 15 decimal places, i could increase to any value with gmp, but maybe next pi day :P

with pretty print, 143 bytes

Golfed:

main(){double k,a,s=1,t;char o[19];k=a=0;while(k<15){t=k++*8;a+=(4/(t+1)-2/(t+4)-1/(t+5)-1/(t+6))/s;s*=16;snprintf(o,k+3,"%.15lf",a);puts(o);}}

Ungolfed:

main(){
    double k,a,s=1,t;
    char o[19];
    k=a=0;
    while(k<15){
        t=k++*8;
        a+=(4/(t+1)-2/(t+4)-1/(t+5)-1/(t+6))/s;
        s*=16;
        snprintf(o,k+3,"%.15lf",a);
        puts(o);
    }
}

output:

$ gcc pigolf_pretty.c -o pigolf_pretty
more gcc screaming warnings
$ ./pigolf_pretty
3.1
3.14
3.141
3.1415
3.14159
3.141592
3.1415926
3.14159265
3.141592653
3.1415926535
3.14159265358
3.141592653589
3.1415926535897
3.14159265358979
3.141592653589793

llpinokio

Posted 2016-10-27T17:06:13.043

Reputation: 31

1Welcome to the site! This is a nice first answer :) – James – 2018-03-14T16:49:31.057

Parenthesis near the - would be not necessary – RosLuP – 2019-06-07T15:40:58.730

Thank you @RosLuP :) – llpinokio – 2019-06-10T12:56:46.670

101 bytes – ceilingcat – 2019-06-10T16:46:27.553

@ceilingcat ++t many times inside one statement would be for C (and C compiler) Undefined Behaviour – RosLuP – 2019-06-11T16:32:55.957

3

Haskell, 101 100 bytes

Thanks to @nimi for a byte.

f n=take(n+2).show$sum[1/16^k*(4/(l+1)-2/(l+4)-1/(l+5)-1/(l+6))|k<-[0..100+n],l<-[8*fromIntegral k]]

Straightforward implementation. Calculates n up to 15 digits (standard double precision).

ThreeFx

Posted 2016-10-27T17:06:13.043

Reputation: 1 435

l<-[8*fromIntegral k] instead of the let ... saves a byte. – nimi – 2016-10-27T18:34:41.747

3

Python 2, 174 Bytes

Man, this is a time when I wish that Python had some easier way of keeping infinite precision for decimals.. Possibly implementing your own infite accuracy type for this challenge is shorter but I can't imagine how. The formula is written verbatim.

from decimal import*
n=input();d=Decimal;getcontext().prec=n+2;p=d(0)
for i in range(n+1):f=8.*i;p+=d(16**(-i))*(4/d(f+1)-2/d(f+4)-1/d(f+5)-1/d(f+6));print str(p)[:-~i+(i>0)]

Example output for n=100 (with some added line numbers):

3
3.1
3.14
3.141
3.1415
3.14159
3.141592
3.1415926
3.14159265
3.141592653
3.1415926535
3.14159265358
3.141592653589
3.1415926535897
3.14159265358979
3.141592653589793
3.1415926535897932
3.14159265358979323
3.141592653589793238
3.1415926535897932384
3.14159265358979323846
3.141592653589793238462
3.1415926535897932384626
3.14159265358979323846264
3.141592653589793238462643
3.1415926535897932384626433
3.14159265358979323846264338
3.141592653589793238462643383
3.1415926535897932384626433832
3.14159265358979323846264338327
3.141592653589793238462643383279
3.1415926535897932384626433832795
3.14159265358979323846264338327950
3.141592653589793238462643383279502
3.1415926535897932384626433832795028
3.14159265358979323846264338327950288
3.141592653589793238462643383279502884
3.1415926535897932384626433832795028841
3.14159265358979323846264338327950288419
3.141592653589793238462643383279502884197
3.1415926535897932384626433832795028841971
3.14159265358979323846264338327950288419716
3.141592653589793238462643383279502884197169
3.1415926535897932384626433832795028841971693
3.14159265358979323846264338327950288419716939
3.141592653589793238462643383279502884197169399
3.1415926535897932384626433832795028841971693993
3.14159265358979323846264338327950288419716939937
3.141592653589793238462643383279502884197169399375
3.1415926535897932384626433832795028841971693993751
3.14159265358979323846264338327950288419716939937510
3.141592653589793238462643383279502884197169399375105
3.1415926535897932384626433832795028841971693993751058
3.14159265358979323846264338327950288419716939937510582
3.141592653589793238462643383279502884197169399375105820
3.1415926535897932384626433832795028841971693993751058209
3.14159265358979323846264338327950288419716939937510582097
3.141592653589793238462643383279502884197169399375105820974
3.1415926535897932384626433832795028841971693993751058209749
3.14159265358979323846264338327950288419716939937510582097494
3.141592653589793238462643383279502884197169399375105820974944
3.1415926535897932384626433832795028841971693993751058209749445
3.14159265358979323846264338327950288419716939937510582097494459
3.141592653589793238462643383279502884197169399375105820974944592
3.1415926535897932384626433832795028841971693993751058209749445923
3.14159265358979323846264338327950288419716939937510582097494459230
3.141592653589793238462643383279502884197169399375105820974944592307
3.1415926535897932384626433832795028841971693993751058209749445923078
3.14159265358979323846264338327950288419716939937510582097494459230781
3.141592653589793238462643383279502884197169399375105820974944592307816
3.1415926535897932384626433832795028841971693993751058209749445923078164
3.14159265358979323846264338327950288419716939937510582097494459230781640
3.141592653589793238462643383279502884197169399375105820974944592307816406
3.1415926535897932384626433832795028841971693993751058209749445923078164062
3.14159265358979323846264338327950288419716939937510582097494459230781640628
3.141592653589793238462643383279502884197169399375105820974944592307816406286
3.1415926535897932384626433832795028841971693993751058209749445923078164062862
3.14159265358979323846264338327950288419716939937510582097494459230781640628620
3.141592653589793238462643383279502884197169399375105820974944592307816406286208
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089
3.14159265358979323846264338327950288419716939937510582097494459230781640628620899
3.141592653589793238462643383279502884197169399375105820974944592307816406286208998
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986
3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862
3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280
3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803
3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348
3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482
3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253
3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534
3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421
3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211
3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170
3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706
3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679

This does seem to work for larger numbers, n=1000 runs in a couple seconds and n=10000 doesn't seem to have given me any errors yet!

Kade

Posted 2016-10-27T17:06:13.043

Reputation: 7 463

3

J, 73 64 62 bytes

(j.":"+10&^(<.@*%[)[:+/\16&^%~[:-/4 2 _1 1%1 4 5 6+/*&8)@i.@>:

This outputs each approximation to n digits as a formatted string. This uses the polynomial simplification of the formula and gets the first n digits by multiplying the sum by a power of 10, flooring it, and dividing by that same power of 10.

The input is taken as an extended integer, meaning that rationals are used when division occurs which keeps results exact.

Usage

This is the output for n = 100, showing the cumulative sums for k in [0, 100].

   f =: (j.":"+10&^(<.@*%[)[:+/\16&^%~[:-/4 2 _1 1%1 4 5 6+/*&8)@i.@>:
   f 100x
3                                                                                                     
3.1                                                                                                   
3.14                                                                                                  
3.141                                                                                                 
3.1415                                                                                                
3.14159                                                                                               
3.141592                                                                                              
3.1415926                                                                                             
3.14159265                                                                                            
3.141592653                                                                                           
3.1415926535                                                                                          
3.14159265358                                                                                         
3.141592653589                                                                                        
3.1415926535897                                                                                       
3.14159265358979                                                                                      
3.141592653589793                                                                                     
3.1415926535897932                                                                                    
3.14159265358979323                                                                                   
3.141592653589793238                                                                                  
3.1415926535897932384                                                                                 
3.14159265358979323846                                                                                
3.141592653589793238462                                                                               
3.1415926535897932384626                                                                              
3.14159265358979323846264                                                                             
3.141592653589793238462643                                                                            
3.1415926535897932384626433                                                                           
3.14159265358979323846264338                                                                          
3.141592653589793238462643383                                                                         
3.1415926535897932384626433832                                                                        
3.14159265358979323846264338327                                                                       
3.141592653589793238462643383279                                                                      
3.1415926535897932384626433832795                                                                     
3.14159265358979323846264338327950                                                                    
3.141592653589793238462643383279502                                                                   
3.1415926535897932384626433832795028                                                                  
3.14159265358979323846264338327950288                                                                 
3.141592653589793238462643383279502884                                                                
3.1415926535897932384626433832795028841                                                               
3.14159265358979323846264338327950288419                                                              
3.141592653589793238462643383279502884197                                                             
3.1415926535897932384626433832795028841971                                                            
3.14159265358979323846264338327950288419716                                                           
3.141592653589793238462643383279502884197169                                                          
3.1415926535897932384626433832795028841971693                                                         
3.14159265358979323846264338327950288419716939                                                        
3.141592653589793238462643383279502884197169399                                                       
3.1415926535897932384626433832795028841971693993                                                      
3.14159265358979323846264338327950288419716939937                                                     
3.141592653589793238462643383279502884197169399375                                                    
3.1415926535897932384626433832795028841971693993751                                                   
3.14159265358979323846264338327950288419716939937510                                                  
3.141592653589793238462643383279502884197169399375105                                                 
3.1415926535897932384626433832795028841971693993751058                                                
3.14159265358979323846264338327950288419716939937510582                                               
3.141592653589793238462643383279502884197169399375105820                                              
3.1415926535897932384626433832795028841971693993751058209                                             
3.14159265358979323846264338327950288419716939937510582097                                            
3.141592653589793238462643383279502884197169399375105820974                                           
3.1415926535897932384626433832795028841971693993751058209749                                          
3.14159265358979323846264338327950288419716939937510582097494                                         
3.141592653589793238462643383279502884197169399375105820974944                                        
3.1415926535897932384626433832795028841971693993751058209749445                                       
3.14159265358979323846264338327950288419716939937510582097494459                                      
3.141592653589793238462643383279502884197169399375105820974944592                                     
3.1415926535897932384626433832795028841971693993751058209749445923                                    
3.14159265358979323846264338327950288419716939937510582097494459230                                   
3.141592653589793238462643383279502884197169399375105820974944592307                                  
3.1415926535897932384626433832795028841971693993751058209749445923078                                 
3.14159265358979323846264338327950288419716939937510582097494459230781                                
3.141592653589793238462643383279502884197169399375105820974944592307816                               
3.1415926535897932384626433832795028841971693993751058209749445923078164                              
3.14159265358979323846264338327950288419716939937510582097494459230781640                             
3.141592653589793238462643383279502884197169399375105820974944592307816406                            
3.1415926535897932384626433832795028841971693993751058209749445923078164062                           
3.14159265358979323846264338327950288419716939937510582097494459230781640628                          
3.141592653589793238462643383279502884197169399375105820974944592307816406286                         
3.1415926535897932384626433832795028841971693993751058209749445923078164062862                        
3.14159265358979323846264338327950288419716939937510582097494459230781640628620                       
3.141592653589793238462643383279502884197169399375105820974944592307816406286208                      
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089                     
3.14159265358979323846264338327950288419716939937510582097494459230781640628620899                    
3.141592653589793238462643383279502884197169399375105820974944592307816406286208998                   
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986                  
3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862                 
3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628                
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280               
3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803              
3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034             
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348            
3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482           
3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825          
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253         
3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534        
3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342       
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421      
3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211     
3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117    
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170   
3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706  
3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067 
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679

Explanation

First make the range [0, n], shown for n = 5

   i. >: 5
0 1 2 3 4 5

Multiply each by 8

   (*&8) i. >: 5
0 8 16 24 32 40

Form the addition table between [1, 4, 5, 6] and the products with 8

   (1 4 5 6+/*&8) i. >: 5
1  9 17 25 33 41
4 12 20 28 36 44
5 13 21 29 37 45
6 14 22 30 38 46

Divide each row by [4, 2, -1, 1]

   (4 2 _1 1%1 4 5 6+/*&8) i. >: 5
       4   0.444444  0.235294       0.16  0.121212   0.097561
     0.5   0.166667       0.1  0.0714286 0.0555556  0.0454545
    _0.2 _0.0769231 _0.047619 _0.0344828 _0.027027 _0.0222222
0.166667  0.0714286 0.0454545  0.0333333 0.0263158  0.0217391

Then reduce the columns from bottom-to-top using subtraction

   ([:-/4 2 _1 1%1 4 5 6+/*&8) i. >: 5
3.13333 0.129426 0.0422205 0.0207553 0.0123137 0.00814508

Divide each 16k for k in [0, n] by each result

   (16&^%~[:-/4 2 _1 1%1 4 5 6+/*&8) i. >: 5
3.13333 0.00808913 0.000164924 5.06722e_6 1.87893e_7 7.76775e_9

Find the cumulative sums

   ([:+/\16&^%~[:-/4 2 _1 1%1 4 5 6+/*&8) i. >: 5
3.13333 3.14142 3.14159 3.14159 3.14159 3.14159

Compute 10k for k in [0, n] and multiply it with each

   (10&^(*)[:+/\16&^%~[:-/4 2 _1 1%1 4 5 6+/*&8) i. >: 5
3.13333 31.4142 314.159 3141.59 31415.9 314159

Then floor each of the products

   (10&^(<.@*)[:+/\16&^%~[:-/4 2 _1 1%1 4 5 6+/*&8) i. >: 5
3 31 314 3141 31415 314159

Divide it by the same power of 10 to get the results

   (10&^(<.@*%[)[:+/\16&^%~[:-/4 2 _1 1%1 4 5 6+/*&8) i. >: 5
3 3.1 3.14 3.141 3.1415 3.14159

miles

Posted 2016-10-27T17:06:13.043

Reputation: 15 654

Nicee! Glad someone used the polynomial simplification. – Magic Octopus Urn – 2016-10-27T20:15:09.960

@carusocomputing Unfortunately I just got it shorter using the coefficients by building a table of values to sum column-wise – miles – 2016-10-27T21:02:38.467

Still, nicely done on both implementations. – Magic Octopus Urn – 2016-10-27T21:04:02.430

3

PARI/GP, 86 bytes

n->for(k=p=0,n,printf("%."k"f\n",(p=16*p-4/(3-j=8*k+4)-2/j-1/j++-1/j++)\(8/5)^k/10^k))

Or without the decimal point in 69 bytes:

n->for(k=p=0,n,print((p=16*p-4/(3-j=8*k+4)-2/j-1/j++-1/j++)\(8/5)^k))

Rather than dividing through by 16k each iteration, the previous value of p is instead multiplied by 16. Floor of p ÷ (8/5)k is then the value of π truncated to the correct number of digits.

Sample Usage

$ gp
? n->for(k=p=0,n,printf("%."k"f\n",(p=16*p-4/(3-j=8*k+4)-2/j-1/j++-1/j++)\(8/5)^k/10^k))
? %(20)
3
3.1
3.14
3.141
3.1415
3.14159
3.141592
3.1415926
3.14159265
3.141592653
3.1415926535
3.14159265358
3.141592653589
3.1415926535897
3.14159265358979
3.141592653589793
3.1415926535897932
3.14159265358979323
3.141592653589793238
3.1415926535897932384
3.14159265358979323846

primo

Posted 2016-10-27T17:06:13.043

Reputation: 30 891

2

IBM/Lotus Notes Formula, 125 bytes

p:=0;@For(n:=0;n<=a;n:=n+1;b:=8*n;p:=p+@Power(16;-n)*(4/(b+1)-2/(b+4)-1/(b+5)-1/(b+6));o:=o:@Left(@Text(p);n+@If(n=0;1;2)));o

Formula in a computed field with another field called "a" for input.

Basically a port of the algorithm from the Python answer from @shebang. Calculates up to 15 digits after which it truncates due to a limitation of the language (see output). Had to waste 12 bytes with the @If statement at the end just to get rid of the . after the 3 at the start :-/

Sample Output

Ungolfed

p:=0;
@For(n:=0; n<=a; n:=n+1;
 b:=8*n;
 p:=p+@Power(16;-n)*(4/(b+1)-2/(b+4)-1/(b+5)-1/(b+6));
 o:=o:@Left(@Text(p);n+@If(n=0;1;2))
 );
o

ElPedro

Posted 2016-10-27T17:06:13.043

Reputation: 5 301

but then Notes formula is never going going to be a golfing language. Thanks to @Shebang for the inspiration. – ElPedro – 2016-10-28T23:03:58.007

0

APL(NARS), 206 chars, 412 bytes

fdn←{1∧÷⍵}⋄fnm←{1∧⍵}⋄r2fs←{q←⌈-/10x⍟¨(fdn ⍵),fnm ⍵⋄m←⎕ct⋄⎕ct←0⋄a←⌊⍵×10x*⍺⋄⎕ct←m⋄k←≢b←⍕a⋄0≥k-⍺:'0.',((⍺-k)⍴'0'),b⋄((k-⍺)↑b),'.',(k-⍺)↓b}⋄p←{+/¨{k←1+8×⍵⋄(+/4 2 1 1÷k,-k+3..5)÷16*⍵}¨¨{0..⍵}¨0..⍵}⋄q←{⍪⍵r2fs¨p⍵}

This find all approssimation in big rational, than use one function that convert big rational in numeric string... test:

 q 1x
3.1 
3.1 
  q 2x
3.13 
3.14 
3.14 
  q 3x
3.133 
3.141 
3.141 
3.141 
  q 10x
3.1333333333 
3.1414224664 
3.1415873903 
3.1415924575 
3.1415926454 
3.1415926532 
3.1415926535 
3.1415926535 
3.1415926535 
3.1415926535 
3.1415926535 
  q 20x
3.13333333333333333333 
3.14142246642246642246 
3.14158739034658152305 
3.14159245756743538183 
3.14159264546033631955 
3.14159265322808753473 
3.14159265357288082778 
3.14159265358897270494 
3.14159265358975227523 
3.14159265358979114638 
3.14159265358979312961 
3.14159265358979323271 
3.14159265358979323815 
3.14159265358979323844 
3.14159265358979323846 
3.14159265358979323846 
3.14159265358979323846 
3.14159265358979323846 
3.14159265358979323846 
3.14159265358979323846 
3.14159265358979323846 
  q 57x     
3.133333333333333333333333333333333333333333333333333333333 
3.141422466422466422466422466422466422466422466422466422466 
3.141587390346581523052111287405405052463875993287757993640 
3.141592457567435381837004555057293394007389950594818748976 
3.141592645460336319557021222442381831727406617979907186696 
3.141592653228087534734378035536204469558528012197801934814 
3.141592653572880827785240761895898484239065603786606461624 
3.141592653588972704940777767170189446971120489811822860633 
3.141592653589752275236177868398102225795024633409061087027 
3.141592653589791146388776965910347414779015888488996772587 
3.141592653589793129614170564041344858816452676296281615895 
3.141592653589793232711292261930077163422606275435901151635 
3.141592653589793238154766322501863827762609260414389714560 
3.141592653589793238445977501940281666096938425156252904675 
3.141592653589793238461732482037982486800056278143046732780 
3.141592653589793238462593174670682882792683045699610435502 
3.141592653589793238462640595138128445061235672871301070791 
3.141592653589793238462643227424822458237094279625505676929 
3.141592653589793238462643374515761485970237552267559842751 
3.141592653589793238462643382784091514246623611329334708720 
3.141592653589793238462643383251362615881909316518417908555 
3.141592653589793238462643383277897474896408560218644955706 
3.141592653589793238462643383279410929692483875831459799593 
3.141592653589793238462643383279497597978087353533999465917 
3.141592653589793238462643383279502579284902684600486947911 
3.141592653589793238462643383279502866555094658758532859204 
3.141592653589793238462643383279502883173477103651067488504 
3.141592653589793238462643383279502884137610730938143080855 
3.141592653589793238462643383279502884193695667358321264063 
3.141592653589793238462643383279502884196966326705909950134 
3.141592653589793238462643383279502884197157502154596455091 
3.141592653589793238462643383279502884197168700950456888403 
3.141592653589793238462643383279502884197169358296080453391 
3.141592653589793238462643383279502884197169396954642664355 
3.141592653589793238462643383279502884197169399232246022950 
3.141592653589793238462643383279502884197169399366660542801 
3.141592653589793238462643383279502884197169399374605817825 
3.141592653589793238462643383279502884197169399375076175949 
3.141592653589793238462643383279502884197169399375104060947 
3.141592653589793238462643383279502884197169399375105716347 
3.141592653589793238462643383279502884197169399375105814747 
3.141592653589793238462643383279502884197169399375105820603 
3.141592653589793238462643383279502884197169399375105820952 
3.141592653589793238462643383279502884197169399375105820973 
3.141592653589793238462643383279502884197169399375105820974 
3.141592653589793238462643383279502884197169399375105820974 
3.141592653589793238462643383279502884197169399375105820974 
3.141592653589793238462643383279502884197169399375105820974 
3.141592653589793238462643383279502884197169399375105820974 
3.141592653589793238462643383279502884197169399375105820974 
3.141592653589793238462643383279502884197169399375105820974 
3.141592653589793238462643383279502884197169399375105820974 
3.141592653589793238462643383279502884197169399375105820974 
3.141592653589793238462643383279502884197169399375105820974 
3.141592653589793238462643383279502884197169399375105820974 
3.141592653589793238462643383279502884197169399375105820974 
3.141592653589793238462643383279502884197169399375105820974 
3.141592653589793238462643383279502884197169399375105820974 

RosLuP

Posted 2016-10-27T17:06:13.043

Reputation: 3 036

0

C#, 183 bytes

Golfed:

void F(int n){double s=0;for(int k=0;k<=n;k++){s+=1/Math.Pow(16,k)*(4.0/(8*k+1)-2.0/(8*k+4)-1.0/(8*k+5)-1.0/(8*k+6));double p=Math.Pow(10,k);Console.WriteLine(Math.Truncate(s*p)/p);}}

Ungolfed:

void F(int n)
{
    double s = 0;

    for (int k = 0; k <= n; k++)
    {
        s += 1/Math.Pow(16, k)*(4.0/(8*k + 1) - 2.0/(8*k + 4) - 1.0/(8*k + 5) - 1.0/(8*k + 6));
        double p = Math.Pow(10, k);

        Console.WriteLine(Math.Truncate(s*p)/p);
    }
}

paldir

Posted 2016-10-27T17:06:13.043

Reputation: 109

Doesn't this print 3.14159265358979 for any n >= 14 due to double precision? – Emigna – 2016-11-04T12:18:04.010

Yes, but I have no idea for workaround. – paldir – 2016-11-04T12:23:25.757

You can use the BigInteger library when calculating and then formatting the output as a string. – Emigna – 2016-11-04T12:39:28.133