16
\$\begingroup\$

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.

\$\endgroup\$
  • 5
    \$\begingroup\$ The 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. \$\endgroup\$ – DJMcMayhem Oct 27 '16 at 17:11
  • \$\begingroup\$ @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. \$\endgroup\$ – Magic Octopus Urn Oct 27 '16 at 17:14
  • 2
    \$\begingroup\$ With respect to @DJMcMayhem's comment, see the advice to avoid non-observable program requirements. \$\endgroup\$ – Peter Taylor Oct 27 '16 at 17:45
  • 2
    \$\begingroup\$ You 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? \$\endgroup\$ – Dennis Oct 27 '16 at 18:58
  • 1
    \$\begingroup\$ In the interest of clarity, I'd just remove the ordinals before that output that is actually required. \$\endgroup\$ – Dennis Oct 27 '16 at 20:08

10 Answers 10

8
\$\begingroup\$

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!

\$\endgroup\$
  • 1
    \$\begingroup\$ "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. \$\endgroup\$ – Magic Octopus Urn Oct 27 '16 at 18:31
  • 1
    \$\begingroup\$ @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. \$\endgroup\$ – Emigna Oct 27 '16 at 18:33
  • \$\begingroup\$ 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) \$\endgroup\$ – Emigna Oct 27 '16 at 18:34
  • 4
    \$\begingroup\$ Maybe just us, but definitely not just you. \$\endgroup\$ – Dennis Oct 27 '16 at 18:35
  • \$\begingroup\$ @carusocomputing: Iterations added. Need to find a cheaper way to do it as the "." was very expensive. \$\endgroup\$ – Emigna Oct 27 '16 at 18:41
5
\$\begingroup\$

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.

\$\endgroup\$
3
\$\begingroup\$

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!

\$\endgroup\$
3
\$\begingroup\$

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).

\$\endgroup\$
  • \$\begingroup\$ l<-[8*fromIntegral k] instead of the let ... saves a byte. \$\endgroup\$ – nimi Oct 27 '16 at 18:34
3
\$\begingroup\$

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
\$\endgroup\$
  • \$\begingroup\$ Nicee! Glad someone used the polynomial simplification. \$\endgroup\$ – Magic Octopus Urn Oct 27 '16 at 20:15
  • \$\begingroup\$ @carusocomputing Unfortunately I just got it shorter using the coefficients by building a table of values to sum column-wise \$\endgroup\$ – miles Oct 27 '16 at 21:02
  • \$\begingroup\$ Still, nicely done on both implementations. \$\endgroup\$ – Magic Octopus Urn Oct 27 '16 at 21:04
3
\$\begingroup\$

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
\$\endgroup\$
3
\$\begingroup\$

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
\$\endgroup\$
  • 1
    \$\begingroup\$ Welcome to the site! This is a nice first answer :) \$\endgroup\$ – DJMcMayhem Mar 14 '18 at 16:49
  • \$\begingroup\$ Parenthesis near the - would be not necessary \$\endgroup\$ – RosLuP Jun 7 at 15:40
  • \$\begingroup\$ Thank you @RosLuP :) \$\endgroup\$ – llpinokio Jun 10 at 12:56
  • \$\begingroup\$ 101 bytes \$\endgroup\$ – ceilingcat Jun 10 at 16:46
  • \$\begingroup\$ @ceilingcat ++t many times inside one statement would be for C (and C compiler) Undefined Behaviour \$\endgroup\$ – RosLuP Jun 11 at 16:32
2
\$\begingroup\$

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
\$\endgroup\$
  • \$\begingroup\$ but then Notes formula is never going going to be a golfing language. Thanks to @Shebang for the inspiration. \$\endgroup\$ – ElPedro Oct 28 '16 at 23:03
0
\$\begingroup\$

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);
    }
}
\$\endgroup\$
  • \$\begingroup\$ Doesn't this print 3.14159265358979 for any n >= 14 due to double precision? \$\endgroup\$ – Emigna Nov 4 '16 at 12:18
  • \$\begingroup\$ Yes, but I have no idea for workaround. \$\endgroup\$ – paldir Nov 4 '16 at 12:23
  • \$\begingroup\$ You can use the BigInteger library when calculating and then formatting the output as a string. \$\endgroup\$ – Emigna Nov 4 '16 at 12:39
0
\$\begingroup\$

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 
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.