text:
Bailey–Borwein–Plouffe Iterations
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:

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.

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.

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

@Emigna it should correctly calculate pi to k digits on each iteration using the algorithm provided. So the precision is important.

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

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

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?

In the interest of clarity, I'd just remove the ordinals before that output that is actually required.

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!

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

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

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)

Maybe just us, but definitely not just you.

@carusocomputing: Iterations added. Need to find a cheaper way to do it as the "." was very expensive.

Could you suggest an edit for the wording? I'm not sure how else to word it. I meant literally outputting the n.

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

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.

so uh separate question: is there a reason to use every character in unicode set but english alphabets

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

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.

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

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!

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

Nicee! Glad someone used the polynomial simplification.

@carusocomputing Unfortunately I just got it shorter using the coefficients by building a table of values to sum column-wise

Still, nicely done on both implementations.

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

Welcome to the site! This is a nice first answer :)

Parenthesis near the - would be not necessary

Thank you @RosLuP :)

101 bytes

@ceilingcat ++t many times inside one statement would be for C (and C compiler) Undefined Behaviour

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

l<-[8*fromIntegral k] instead of the let ... saves a byte.

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 :-/

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

but then Notes formula is never going going to be a golfing language. Thanks to @Shebang for the inspiration.

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);
    }
}

Doesn't this print 3.14159265358979 for any n >= 14 due to double precision?

Yes, but I have no idea for workaround.

You can use the BigInteger library when calculating and then formatting the output as a string.

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


text:
Simplificate an array
Input
An array that can contain arrays or positive, consecutive, ascending integers. The arrays can have any number of arrays inside of them, and so on and so forth. No arrays will be empty.
Output
This array simplificated
How to simplificate an array
We will use the array, [1, [2, 3], [[4]], [[[5, 6], 7, [[[8]]]], 9]] as our example.
First, we check how deep the values are nested. Here are the depths and the numbers at those depths:
0  1
1  2 3 9
2  4 7
3  5 6
5  8

We construct the output array by taking the numbers in the original array, grouping them by how deep they are nested, and then nesting the groups at depths of the original depths of their  elements. Arrange the numbers in ascending order and ascending depth.
So, our output is [1, [2, 3, 9], [[4, 7]], [[[5, 6]]], [[[[[8]]]]]]
Examples
[1, [2, 3], [[4]], [[[5, 6], 7, [[[8]]]], 9]] -> [1, [2, 3, 9], [[4, 7]], [[[5, 6]]], [[[[[8]]]]]]
[[[1]], [2, [3]], 4, [5, [6, [7, [8], [9, [[10]]]]]]] -> [4, [2, 5], [[1, 3, 6]], [[[7]]], [[[[8, 9]]]], [[[[[[10]]]]]]]
[1] -> [1]
[1, [2], [[3]], [[[4]]], [[[[5]]]]] -> [1, [2], [[3]], [[[4]]], [[[[5]]]]]
[1, [[[[2], 3]]], [[4]]] -> [1, [[4]], [[[3]]], [[[[2]]]]]

Is output flexible? Like numbers in different lines, where each line is one level; or other array delimiters/separators

Related

@LuisMendo, yeah it's flexible

You are missing one bracket pair around 8 at line So, our output is...... However, you fixed it in the examples snippet.

@sbisit, why would it be surrounded by 6?—Unless you are counting the outermost ones that hold everything.

@Dopapp. Yes, I said "total" 6, so these one counts too. Now it is ok.

Can we take input the array as a string, because there are a lot of programming languages that doesn't support nested arrays?

@sbisit, sure that works

Test case: [1,[2,3],4] -> [1,4, [2,3]] ?

Can we assume the integers will always be single-digit as in your example? That would simplify string-based approaches

@LuisMendo, no. The second test case has 10 in it

Yeah I'll edit that @DLosc

Some answers output an empty line for nesting levels with not elements. Is it ok to return an empty array in such cases, e.g. you first example as [1, [2, 3, 9], [[4, 7]], [[[5, 6]]], [[[[]]]], [[[[[8]]]]]]?

Jelly, 8 bytes
fFṄḟ@;/ß

Output is one level per line, with empty lines for levels without elements. Try it online!
How it works
fFṄḟ@;/ß  Main link. Argument: A (array)

 F        Flat; yield all integers (at any level) in A.
f         Filter; intersect A with the integers, yielding those at level 0.
  Ṅ       Print the filtered array and a linefeed. Yields the filtered array.
     ;/   Reduce by concatenation.
          This decreases the levels of all integers at positive levels by 1.
   ḟ@     Swapped filter-false; remove the integers at level 0 in A from the array
          with decreased levels.
       ß  Recursively call the main link on the result.
          The program stops once A is empty, since ;/ will result in an error.

Perl, 52 bytes
Just a recursive subroutine. (unusual for a Perl answer, I know..)
sub f{say"@{[grep!ref,@_]}";@_&&f(map/A/?@$_:(),@_)}

Call it like that :
$ perl -E 'sub f{say"@{[grep!ref,@_]}";@_&&f(map/A/?@$_:(),@_)}f(1, [2, 3], [[4]], [[[5, 6], 7, [[[8]]]], 9])'
1
2 3 9
4 7
5 6

8

Each line of the output corresponds to a depth level of the array (hence the empty line in the example above).
It can be turned into a full program for just a few more bytes : add -n flag and an eval (inside @{ } to transform the input into an array and not an arrayref) to transform the input into a Perl array :
perl -nE 'sub f{say"@{[grep!ref,@_]}";@_&&f(map/A/?@$_:(),@_)}f(@{+eval})' <<< "[1, [2, 3], [[4]], [[[5, 6], 7, [[[8]]]], 9]]"

My previous approach was slightly longer (65 bytes), but still interesting, so I'll let it here :

perl -nE '/\d/?push@{$;[$d-1]},$_:/]/?$d--:$d++for/\[|]|\d+/g;say"@$_"for@' <<< "[1, [2, 3], [[4]], [[[5, 6], 7, [[[8]]]], 9]]"

JavaScript (ES6), 139 109 bytes
f=(a,v=b=>a.filter(a=>b^!a[0]))=>a[0]?v().concat((a=f([].concat(...v(1))),b=v())[0]?[b]:[],v(1).map(a=>[a])):[]

Explanation using the example input: v is a helper method which returns the arrays (with parameter 1) or values (with no parameter). We start with a = [1, [2, 3], [[4]], [[[5, 6], 7, [[[8]]]], 9]], which is nonempty. We filter out the arrays, giving [1]. We then recursively call ourselves on the arrays concatenated together, which is [2, 3, [4], [[5, 6], 7, [[[8]]]], 9], the result being [2, 3, 9, [4, 7], [[5, 6]], [[[[8]]]]]. We again filter out the arrays, which gives us the second term of our output, [2, 3, 9], however we have to be careful not to insert an empty array here. It them remains to wrap the arrays [4, 7], [[5, 6]], [[[[8]]]] inside arrays and append them to the output, resulting in [1, [2, 3, 9], [[4, 7]], [[[5, 6]]], [[[[[8]]]]]].

Might be able to save a few bytes by making an alias to filter. Maybe start with F=(x,y)=>x.filter(y)

@Cyoce Turned out to be 30 in the end!

You definitely have some golfing left to do on this. I think you can safely replace [].concat(...v(1)) with v(1) to save 14 bytes. There are probably a few other things as well but I'm having a hard time keeping track of the nested parentheses in my head.

@PatrickRoberts [].concat(...v(1)) is a very different beast to v(1), otherwise I wouldn't do it! For a simple example, consider a = [2, [3], [[4]]] then v(1) = [[3], [[4]]] but [].concat(...v(1)) = [3, [4]].

@Neil oh, wow I really should have tested my suggestion before opening my mouth. I feel like there should be a shorter way of doing this though..

JavaScript (ES6) 121 144 152
Edit Revised a lot, 1 byte saved thx Patrick Roberts, and 21 more just reviewing the code
Recursive function working on arrays in input and output. I don't like the request of having elements at depth 1 as single elements in output array (while greater levels are grouped as one element): [l1,l1, [l2...], [[l3...]] ]. While this would be more direct: [ [l1...], [[l2...]], [[[l3...]]] ]
f=(l,d=0,r=[])=>l.map(v=>v[0]?f(v,d+1,r):r[d]=[...r[d]||[],v])
r.reduce((r,v,d)=>d?[...r,(n=d=>d-->1?[n(d)]:v)(d)]:v,[])

Newline added for readability.
Some notes: the line 2 is evaluated again and again at each recursive call, but only the last iteration at the end of recursion is useful.
The special handling when d==0 in line 2 takes care of the anomaly for level 1 elements.
The n recursive function handles the array nesting in output
Test

f=(l,d=0,r=[])=>l.map(v=>v[0]?f(v,d+1,r):r[d]=[...r[d]||[],v])
&&r.reduce((r,v,d)=>d?[...r,(n=d=>d-->1?[n(d)]:v)(d)]:v,[])

console.log=x=>O.textContent+=x+'\n'

test=[
 [
   [1, [2,3], 4], /* -> */ [1, 4, [2,3]]
 ]
,[
   [1, [2, 3], [[4]], [[[5, 6], 7, [[[8]]]], 9]],
   // ->
   [1, [2, 3, 9], [[4, 7]], [[[5, 6]]], [[[[[8]]]]]]
 ]
,[
  [[[1]], [2, [3]], 4, [5, [6, [7, [8], [9, [[10]]]]]]],
  // ->
  [4, [2, 5], [[1, 3, 6]], [[[7]]], [[[[8, 9]]]], [[[[[[10]]]]]]]
 ]
,[
  [1], /* -> */ [1]
 ]
,[
  [1, [2], [[3]], [[[4]]], [[[[5]]]]],
  // ->
  [1, [2], [[3]], [[[4]]], [[[[5]]]]]
 ]
,[
  [1, [[[[2], 3]]], [[4]]],
  [1, [[4]], [[[3]]], [[[[2]]]]]
]]

test.forEach(t=>{
  var i=t[0], k=t[1], r=f(i),
      si=JSON.stringify(i),
      sr=JSON.stringify(r),
      sk=JSON.stringify(k)

  console.log((sr==sk?'OK ':'KO ')+si + " => " + sr)
})

Given that there are only nested arrays and positive integers, and it's specified that none of the arrays in the input are empty, an easier test for your ternary operator would be v[0] instead of v.map. Saves 1 byte.

@PatrickRoberts cool thanks

05AB1E, 27 26 25 21 bytes
D˜gFvyydi„ÿ ?}}¶?.gG«

Try it online! (slightly modified as .g isn't on TIO yet)
Explanation
D˜gF                    # flattened input length times do
    vy                  # for each y current level of list
      ydi„ÿ ?}          # if y is a digit, print with space
              }         # end v-loop
               ¶?       # print newline
                 .g     # calculate length of stack (this should be .g but I can't test)
                   G«   # length stack times, concatenate items on stack

The main strategy is to loop over each possible level of the nested array and print any digits on one row, while keeping the non-digits (lists) in a list one level less nested.

JavaScript (ES6) 168 bytes
f=a=>(s=[],b=-1,k=0,a.replace(/\d+|\[|\]/g,a=>a=='['?b++:a==']'?b--:(s[b]=s[b]||[]).push(a)),'['+s.map((a,b)=>k=a&&(k?',':'')+'['.repeat(b)+a+']'.repeat(b)).join``+']')

Demo

PHP, 145 Bytes
<?function c($r){$n=[];foreach($r as$k=>$v)if(is_array($v)){$n=array_merge($n,$v);unset($r[$k]);}if($n)$r[]=c($n);return$r;}print_r(c($_GET[a]));

Breakdown
function c($r){
  #usort($r,function($x,$y){return is_array($x)<=>is_array($y)?:$x<=>$y;});
#no need to sort and a simple sort($r); do it sort array after scalar
  $n=[];
  foreach($r as$k=>$v)if(is_array($v)){$n=array_merge($n,$v);unset($r[$k]);} # put arrays on the same depth together
  if($n)$r[]=c($n); # recursive if an array exists
  return$r; #return changes
}
print_r(c($_GET[a])); #Output and Input

Haskell, 124 123 bytes
data L=I Int|R[L]
d#R l=((d+1)#)=<<l
d#i=[(d::Int,i)]
[]!_=[]
l!1=l
l!d=[R$l!(d-1)]
h l=R$do d<-[1..];[i|(e,i)<-0#l,d==e]!d

As Haskell doesn't support mixed lists (integers and list of integers) by default, I define a custom list type L. Usage example:
*Main> h (R[I 1, R[I 2, I 3], R[ R[I 4]], R[ R[ R[I 5, I 6], I 7, R[R[R[I 8]]]], I 9]])
R [I 1,R [I 2,I 3,I 9],R [R [I 4,I 7]],R [R [R [I 5,I 6]]],R [R [R [R [R [I 8]]]]]]

Note: it takes some time to run, because it loops through all positive Ints (32 or 64bit) to look for a nest level that deep. Also: the custom list type cannot be printed by default, so if you want to see the result as in the example above, you need to add deriving Show to the data declaration (-> data L=I Int|R[L] deriving Show). Because it's not needed for returning a L-list from a function, I don't count the bytes.
How it works:
data L=I Int|R[L]               -- custom list type L, which is either an Int
                                -- (-> I Int) or a list of some L (-> R [L])

d#R l=((d+1)#)=<<l              -- # makes a list of (depth, I-number) pairs from
d#i=[(d::Int,i)]                -- a given L-list, e.g.
                                -- 0 # (R[I 1,R[I 2,I 3],I 4]) -> [(1,I 4),(2,I 2),(2,I 3),(1,I 4)]
                                -- the type annotation ::Int makes sure that all
                                -- depths are bounded. Without it, Haskell
                                -- would use arbitrary large numbers of type
                                -- ::Integer and the program won't finish

[]!_=[]                         -- ! wraps a list of Is with (d-1) additional
l!1=l                           --  R constructors
l!d=[R$l!(d-1)]

h l=                            -- main function, takes a L-list
      do d<-[1..]               -- for each nest level d make
        [i|(e,i)<-0#l,d==e]     -- a list of all I where the depth is d
                           !!d  -- and wrap it again with d-1 Rs
     R$                         -- wrap with a final R

Edit @BlackCap saved a byte by switching from >>= to do notation. Thanks!

Do notation saves a byte h l=R$do d<-[1..];[i|(e,i)<-0#l,d==e]!d

JavaScript (ES6), 127 137 134 bytes
Takes an array as input and returns a string.
f=(a,l=[],d=0,o='')=>`[${a.map(x=>x[0]?f(x,l,d+1,o+'['):l[d]=(l[d]?l[d]+',':o)+x),l.map((s,d)=>x+s+']'.repeat(d,x=','),x='').join``}]`

Test cases

f=(a,l=[],d=0,o='')=>`[${a.map(x=>x[0]?f(x,l,d+1,o+'['):l[d]=(l[d]?l[d]+',':o)+x),l.map((s,d)=>x+s+']'.repeat(d,x=','),x='').join``}]`

console.log(f([1, [2, 3], [[4]], [[[5, 6], 7, [[[8]]]], 9]]));
console.log(f([[[1]], [2, [3]], 4, [5, [6, [7, [8], [9, [[10]]]]]]]));
console.log(f([1]));
console.log(f([1, [2], [[3]], [[[4]]], [[[[5]]]]]));
console.log(f([1, [[[[2], 3]]], [[4]]]));

@Shebang Thanks for noticing. This should be fixed.

I believe that looks good now! :)

Pyth, 19 16 bytes
W=Qsf!&sITp+TdQk

Try it online. Test suite.
Note the leading space. Outputs levels on rows like the Perl answer.
Explanation

Implicit input in Q.
filter items T of Q on:

Check if sum is Identity on T.
If it was (it was a number), print T plus a space +…d.
If it wasn't (it was an array), keep it.

sum the items. This removes a layer of arrays from each item. If none are left, yields 0.
Assign = the result to Q.
While the result is nonempty, print the empty string k and a newline.

APL (Dyalog Unicode), 22 bytes
{⍬≢⍵:∇⊃,/⍵~⎕←⍵/⍨0=≡¨⍵}

Try it online!
Similar to Dennis' answer, a test case per line.

Red, 174 bytes
func[a][i: charset"0123456789"d: 0 m: copy #()parse mold a[any["["(d: d + 1)|"]"(d: d - 1)|" "|
copy t any i(either m/:d[append m/:d to 1 t][m/:d: to[]t])]]sort/skip to[]m 2]

Try it online!
A naive approach using the string representation of the array, keeping track of the depth by counting the [and ]. The integers are stored into a map which keys are the depths.

R, 96 bytes
f=function(x,`+`=list,`-`=unlist,i=sapply(x,is.list))c(+sort(-x[!i]),if(length(x[i]))+f(x[i]-F))

Try it online!
Outputs a nested list.
Ungolfed code
simplificate=
f=function(x){                      # recursive function f with argument x;
 islist=sapply(x,is.list)           # first determine which elements of x are lists
 c(                                 # now make a list of two groups of elements:
  list(sort(unlist(x[!islist]))),   # the first is the non-list elements of x,
  if(length(x[islist])){            # then (but only if there were some)
   list(                            # the second is a list of
    f(                              # the results of a recursive call to f
     unlist(x[islist],              # with the list elements of x as an argument,
      recursive=FALSE)))            # after unlisting by one level.
  }
 )
}

Or, 88 bytes
f=function(x,`-`=unlist,i=sapply(x,is.list))if(length(x)){print(sort(-x[!i]));f(x[i]-F)}

Try it online!
Outputs elements from each level on a different line.
This meets the flexible output allowed in the comments, but feels a bit sneaky when R is happily able to output a nested list as originally asked for in the challenge...

Python 3, 104 bytes
v=lambda l,m={},i=1:m if l==[]else v(l[1:],v(l[0],m,i+1)if str(l[0])>'A'else{**m,i:m.get(i,[])+l[:1]},i)

Try it online!

source: Stack Exchange

license: Creative Commons - Attribution Share-Alike - https://creativecommons.org/licenses/by-sa/4.0/

all_licenses: Creative Commons - Attribution Share-Alike - https://creativecommons.org/licenses/by-sa/3.0/

sort: votes


text:
FOIL Python's strong typing!
Your task is to write some code in Python 2 or 3 such that this expression:
(a+b)(c+d) == a*c + b*c + a*d + b*d

will evaluate to True without raising any exceptions.
To clarify, I will copy your code into a file, then from the file import *. Then I will type the expression into the console and verify that it is True.
This is code-golf, so the answer with the shortest length (in bytes) wins.

54 52 50 49 48 45 39 bytes
Removed 4 bytes thanks to Dennis.
The latest version is inspired by the "some reason" in xnor's answer.
class t(int):__add__=type
a=b=t()
c=d=0

Nice! There's 0 .__mul__ for lambda y:0 but it's the same length.

x.count saves a byte.

With a=t();b=c=d=0, you can use class t(int):__add__=lambda*x:t to save three bytes.

@Dennis Thanks. Good idea...

I don't get it... type(t(), t()) or t().type(t()) throws an exception, so what is happening when you do t() + t()?

@feersum just type(t()). The LHS becomes (type(b))(0) or t(0), which is equal to 0.

@JonathanAllan But + is a binary operator. Why would __add__ be called with 1 argument?

@feersum __add__ is called with two, but the first is interpreted as self, only other is passed to type. Weird, yes.

@feersum The difference is, I believe, that inbuilts use method-wrappers (py3) or unbound methods (py2) (rather than methods or bound methods respectively, like MySpam.__myEggs__ would usually be). I could well be wrong though, interested to know...

@JonathanAllan But try this: numargs = lambda *a: len(a);    class t(int): __add__ = numargs t() + t() results in 2!

@feersum: a + b first tries a.__add__(b). a.__add__ is type, so that becomes type(b). The key difference between this case and the usual case for methods is that usually, a.__add__ would be a different object from the thing you set __add__ to in the class definition, due to the descriptor protocol, which ordinary function objects implement. (There are also a few other tricky bits that aren't relevant here.)

54 bytes
class m(int):__call__=__add__=lambda*x:m()
a=b=c=d=m()

Make an object that inherits from int, except adding or calling just returns a copy of itself.
Same length:
class m(int):__call__=__add__=lambda a,b:a
a=b=c=d=m()

I thought min or {}.get would work in place of lambda a,b:a, but for some reason they act only on the second argument.

(it's [tag:code-golf])

Oops, I only saw [tag:programming-puzzle], will golf.

That was quite a reduction o-o

@xnor It doesn't work because min already has a __self__ attribute, so the class skips binding its own self. Why min has __self__ is another question...

@matsjoyce: No, it doesn't have anything to do with the fact that min has a __self__. min.__self__ is just an artifact of how built-in functions and built-in methods are implemented as the same type. min doesn't work here because unlike functions written in Python, built-in functions don't support the descriptor protocol, which is responsible for binding the first argument.

@user2357112 Ah, OK, I thought I got it working with sum yesterday, guess I was looking at wrong thing then.

81 66 bytes
class e:__mul__=lambda*o:0;__add__=lambda*o:lambda x:0
a=b=c=d=e()

68 bytes
While it cannot really compete with the existing answers, this one actually performs the calculation in question:
from sympy.abc import*
type(a+b).__call__=lambda x,y:(x*y).expand()

Explanation:

SymPy is a module for symbolic computations.
sympy.abc contains all single-letter symbols, in particular ones named a, b, c, and d.
a+b is an Add object, which represents a general sum.
type(a+b).__call__= […] monkey-patches the Add class to give it evaluation capabilities, in this case enabling it to work like a multiplication of caller and callee.
expand is necessary to make the expressions actually equal (since SymPy only performs thorough equality checks on demand).

all_licenses: Creative Commons - Attribution Share-Alike - https://creativecommons.org/licenses/by-sa/4.0/


text:
The Switching Sequence
Intro
The Switching sequence is defined like so:
Start with n people standing in a circle (6 for this example).
 1  2
6    3
 5  4

Starting from person 1, the person that is to the left of the "chosen" person is removed.
 1
6    3
 5  4

The removed person can "switch" the removal method up:

If the removed person is even (which it is in this case), the next removed person will be to the right of the next "chosen" person.
If the removed person is odd, the next removed person will be to the left of the next "chosen" person.

The next chosen person is also dependent on the previously removed person.

If the removed person is even, the next chosen person will be to the right of the previous chosen person.
If the removed person is odd, see above, but replace "right" with "left".

So the next chosen person is then 6.
Now we remove the person to the right of 6, which is 5:
 1
6    3
    4

Because 5 is odd, the removed person is now to the left. The new chosen person is 1.
We now remove 3:
 1
6
    4

We continue this process, until we're left with 1 number - in this example, the final number is 1. So therefore S(6) = 1.
The first few numbers are:
 n | S(n)
---------
 1 | 1
 2 | 1
 3 | 3
 4 | 1
 5 | 5
 6 | 1
 7 | 3
 8 | 6
 9 | 5
10 | 6
11 | 9

Task
Your task is to make a program (or a function) that returns S(n) (the nth number in the Switching sequence) when given n, using the least amount of bytes.
Example inputs and outputs:
1  -> 1
10 -> 6
13 -> 13

You are guaranteed to get a positive integer.
This is code-golf, so shortest code in bytes wins!
Note: There is no OEIS sequence (what?), to save you the trouble of searching.

No hits on oeis, to save people the search.

Obviously 2 never remains, but does 7?

@JonathanAllan I just checked for the first 1000 terms, and the result is currently "no". I'm not sure though - should I put that as a "side challenge" thing that people can attempt to prove or something? It's for brownie points, so it doesn't detract from the challenge.

Maybe it will be obvious once someone comes up with a method other than by following your instructions...

How do you expect people solving this without an OEIS? Someone push an OEIS, please?

I think 'left of the "chosen":' should say right? What happens if there is an odd number? Then, there will not be right or left elements.

@bacchusbeale What do you mean?

@Qwerp-Derp I think I might have stumbled upon a slight simplification to your rules. If you consider the next chosen person to be decided relative to the previously chosen person instead of the removed person you will never have to worry about a person being chosen twice in a row (unless they are they only one left but at that point you are done)

@1000000000 - yep if you see my code I could combine the removeIndex += direction up into the removeIndex -= line (but due to the need for setting the direction and the requirement of parentheses it would use two more bytes in total).

Python 2, 183 94 bytes
-4 bytes thanks to Artyer (use input() and print rather than def and return)
-1 byte thanks to FlipTack (use print-~p[0] rather than print p[0]+1)
p=range(input())
d=i=1
while p[1:]:m=p.pop(i)%2;i-=m+m-(d<0);d=-m|1;i+=d;i%=len(p)
print-~p[0]

repl.it
This just follows the instructions given, I've noticed some pattern, maybe it could be exploited?
The only changes are:

to use 0 based indexing (so even people are odd and vice-versa) - this is saves 5 bytes in the golfed logic and is corrected at the end with +1
to use 1 as left and -1 as right (to use a range - just like everyone is facing outwards instead)
to change the logic of the step where the next chosen individual is found to account for poping from the list making the "pointer" index already step one to the right in the list (or to the left in original terminology).

Ungolfed:
def circle(n):
    people = range(n) # p
    direction = 1 # d
    removeIndex = 1 # i
    while n > 1:
        removingMod2 = people.pop(removeIndex) % 2 # m
        removeIndex -= removingMod2 + removingMod2 - (direction == -1)
        direction = -removingMod2 or 1
        removeIndex += direction
        n -= 1
        removeIndex %= n
    return people[0] + 1

Can the last line be print-~p[0]?

Why yes it can!

license: Creative Commons - Attribution Share-Alike - https://creativecommons.org/licenses/by-sa/3.0/