| Bytes | Lang | Time | Link |
|---|---|---|---|
| 069 | Python 2 | 170808T070000Z | tsh |
| 130 | Forth gforth | 201111T052059Z | Bubbler |
| 035 | CasioBasic | 170808T074901Z | numberma |
| 030 | Octave | 170809T081107Z | Cinaski |
| 040 | Julia 0.6.0 | 170808T184740Z | Goysa |
| 026 | 05AB1E | 170808T132807Z | Emigna |
| 080 | JavaScript ES6 | 170808T104950Z | Neil |
| 036 | Pari/GP | 170808T080330Z | alephalp |
| 052 | Haskell | 170808T120707Z | ბიმო |
| 014 | APL Dyalog | 170808T070021Z | Adá |
| 028 | Mathematica | 170808T073255Z | alephalp |
| 011 | M | 170808T080438Z | miles |
Python 2, 69 bytes
from fractions import*
f=lambda k:k and Fraction(k+f(k-1),1-k*f(k-1))
Use the identity:
tan(A + B) = (tan(A) + tan(B)) / (1 - tan(A) * tan(B))
We have:
f(k) = 0 if k = 0
= (k + f(k - 1)) / (1 - k * f(k - 1)) if k > 0
Thanks to Luis Mendo, save 4 bytes. Thanks to Lucenaposition, save 3 bytes.
Forth (gforth), 130 bytes
: g dup if tuck mod recurse then ;
: f 0 1 rot for 2dup i * + -rot swap i * - next
2dup g + abs over 0< 2* 1+ * >r i / swap r> / ;
Uses my own implementation of gcd, and the OEIS formula. Takes k from the stack and returns two integers denom num on the stack. It took a lot of effort (and bytes) to normalize the fraction.
: g ( x y -- gcd 0 )
dup if tuck mod recurse then
;
: f ( k -- denom num )
0 1 \ push initial fraction with num=0, denom=1
rot for \ ( num denom ) loop from k to 0 inclusive
2dup i * + -rot \ ( denom*k+num num denom )
swap i * - \ ( denom*k+num denom-num*k )
next
2dup g + abs \ ( num denom gcd ) evaluate positive gcd and remove 0
over 0< 2* 1+ * \ ( num denom gcd' ) negate gcd if denom < 0
>r i / swap r> / \ ( denom/gcd' num/gcd' )
;
Casio-Basic, 35 bytes
tExpand(tan(sum(seq(tan⁻¹(n),n,0,k
tan-1 should be entered as the one on the Trig keyboard; or the -1 can be entered separately from the abc>Math keyboard. According to the fx-CP400's manual, it's a single two-byte character (764).
Function, 34 bytes for the code, +1 byte to add k as an argument.
Explanation
seq(tan-1(n),n,0,k) generates all the values for tan-1(n) from 0 to k.
sum adds them all together, then tan does the tangent function on them.
tExpand will then turn them into a single fraction.
Julia 0.6.0 40 bytes
k->rationalize(tan(sum(x->atan(x),1:k)))
It's a straightforward implementation of the question. The precision of rationalize can sometimes be weird but works fine 99% of the time.
05AB1E, 33 26 bytes
0X)Iƒ©`N*+®`sN*-‚D¿D_i\¤}/
Explanation
0X) # initialize stack with [0,1]
Iƒ # for N in range [0 ... input] do:
© # store a copy of the current pair in the register
` # push the pair separately to the stack
N* # multiply the denominator with N
+ # add the numerator
®`s # push the denominator then the numerator to the stack
N* # multiply the numerator by N
- # subtract it from the denominator
D¿D # get 2 copies of the greatest common divisor
0Qi } # if the gcd equals 0
\¤ # replace it with the denominator
/ # divide the pair with this number
JavaScript (ES6), 80 bytes
f=n=>n?([a,b]=f(n-1),g=(a,b)=>a?g(b%a,a):b,c=g(d=b*n+a,e=b-n*a),[d/c,e/c]):[0,1]
Returns a [numerator, denominator] pair. Explanation: Let f(n-1) = a/b then f(n) = atan(tan(n)+tan(a/b)) = (n+a/b)/(1-n*a/b) = (b*n+a)/(b-n*a). It then remains to reduce the fraction to its lowest terms.
Pari/GP, 36 bytes
n->imag(x=prod(i=0,n,1+i*I))/real(x)
Or the same length:
n->fold((x,y)->(x+y)/(1-x*y),[0..n])
Haskell, 52 bytes
This uses the OEIS series expansion:
import Data.Ratio
f 0=0%1
f n|p<-f$n-1=(p+n)/(1-n*p)
Or a pointfree version:
(scanl1(\f n->(f+n)/(1-n*f))[0%1..]!!)
APL (Dyalog), 14 bytes
Requires ⎕FR←1287 (128 bit Floating-point Representation) for small input. Takes k as right argument.
1(∧÷,)3○¯3+.○⍳
⍳ integers one through k (zero is not needed as 0 = arctan 0)
¯3+.○ sum of arcus tangents
3○ tangent
1(…) apply the following tacit function with 1 as left argument and the above as right argument:
∧ the lowest common multiple (of 1 and the right argument); gives the numerator
÷ divided by
, the concatenation (of 1 and the right argument); gives the numerator and the denominator
Mathematica, 28 bytes
Fold[+##/(1-##)&,0,Range@#]&
A longer, but more interesting approach (32 bytes):
Im@#/Re@#&@Product[1+n I,{n,#}]&