Skip to main content
Better explanation, I hope.
Source Link

Ruby - 160 152 140 Chars

Using recursion and the fact that for this recursive function, cosimplementation sin(x, n2n + 1) = 1 + cos(x, 2n - 1), being sin(x, n + 1) and cos(x, n) the series defined above for cos x and sin x.

p=->x,n{n<1?1:x*p[x,n-1]}
f=->n{n<2?1:n*f[n-1]}
c=->x,n{n<1?1:p[x,n]/f[n]-c[x,n-2]}
x,n=gets.split.map &:to_f
n*=2
puts c[x,n-1]+1,c[x,n-2]

Edit: Contributed by commenters (read below).

Ruby - 160 152 140 Chars

Using recursion and the fact that for this recursive function, cos(x, n) = 1 + sin(x, n + 1)

p=->x,n{n<1?1:x*p[x,n-1]}
f=->n{n<2?1:n*f[n-1]}
c=->x,n{n<1?1:p[x,n]/f[n]-c[x,n-2]}
x,n=gets.split.map &:to_f
n*=2
puts c[x,n-1]+1,c[x,n-2]

Edit: Contributed by commenters (read below).

Ruby - 160 152 140 Chars

Using recursion and the fact that for this recursive implementation sin(x, 2n + 1) = 1 + cos(x, 2n - 1), being sin(x, n) and cos(x, n) the series defined above for cos x and sin x.

p=->x,n{n<1?1:x*p[x,n-1]}
f=->n{n<2?1:n*f[n-1]}
c=->x,n{n<1?1:p[x,n]/f[n]-c[x,n-2]}
x,n=gets.split.map &:to_f
n*=2
puts c[x,n-1]+1,c[x,n-2]

Edit: Contributed by commenters (read below).

Corrected speelling.
Source Link

Ruby - 160 152152 140 Chars

Using recursion anand the fact that for this recursive function, cos(x, n) = 1 + sin(x, n + 1)

def p(xp=->x,n){n<1?1:x*p(xx*p[x,n-1)end1]}
def f(n)f=->n{n<2?1:n*f(nn*f[n-1)end1]}
def c(xc=->x,n){n<1?1:p(xp[x,n)n]/f(n)f[n]-c(xc[x,n-2)end2]}
x,n=gets.split.map &:to_f
n*=2
puts c(xc[x,n-1)+11]+1,c(xc[x,n-2)2]

Edit: Contributed by commenters (read below).

Ruby - 160 152 Chars

Using recursion an the fact that for this recursive function, cos(x, n) = 1 + sin(x, n + 1)

def p(x,n)n<1?1:x*p(x,n-1)end
def f(n)n<2?1:n*f(n-1)end
def c(x,n)n<1?1:p(x,n)/f(n)-c(x,n-2)end
x,n=gets.split.map &:to_f
n*=2
puts c(x,n-1)+1,c(x,n-2)

Ruby - 160 152 140 Chars

Using recursion and the fact that for this recursive function, cos(x, n) = 1 + sin(x, n + 1)

p=->x,n{n<1?1:x*p[x,n-1]}
f=->n{n<2?1:n*f[n-1]}
c=->x,n{n<1?1:p[x,n]/f[n]-c[x,n-2]}
x,n=gets.split.map &:to_f
n*=2
puts c[x,n-1]+1,c[x,n-2]

Edit: Contributed by commenters (read below).

Reduced to 152 chars
Source Link

Ruby - 160160 152 Chars

Using recursion an the fact that for this recursive function, cos(x, n) = 1 + sin(x, n + 1)

def p(x,n)n<1?1:x*p(x,n-1)end
def f(n)n<2?1:n*f(n-1)end
def c(x,n)n<1?1:p(x,n)/f(n)-c(x,n-2)end
x,n=gets.split.collect{|i|i.map &:to_f}
n*=2
puts c(x,n-1)+1,c(x,n-2)

Ruby - 160 Chars

Using recursion an the fact that for this recursive function, cos(x, n) = 1 + sin(x, n + 1)

def p(x,n)n<1?1:x*p(x,n-1)end
def f(n)n<2?1:n*f(n-1)end
def c(x,n)n<1?1:p(x,n)/f(n)-c(x,n-2)end
x,n=gets.split.collect{|i|i.to_f}
n*=2
puts c(x,n-1)+1,c(x,n-2)

Ruby - 160 152 Chars

Using recursion an the fact that for this recursive function, cos(x, n) = 1 + sin(x, n + 1)

def p(x,n)n<1?1:x*p(x,n-1)end
def f(n)n<2?1:n*f(n-1)end
def c(x,n)n<1?1:p(x,n)/f(n)-c(x,n-2)end
x,n=gets.split.map &:to_f
n*=2
puts c(x,n-1)+1,c(x,n-2)
Remove "--FIXED--" from header
Source Link
Loading
Improved explanation
Source Link
Loading
Removed a header typo
Source Link
Loading
Avoid using ** operator.
Source Link
Loading
added 26 characters in body
Source Link
Loading
deleted 12 characters in body
Source Link
Loading
Source Link
Loading