3

I'm trying a problem but first I have to plot in r

(x+1)(x+2)...(x+n),

with n being a fixed integer.

Any idea how to create this routine?

drkthng
  • 6,651
  • 7
  • 33
  • 53
albert
  • 305
  • 4
  • 12

2 Answers2

4

Provided x is greater than -1, this might be most efficiently computed by exploiting the relationship

(x + 1)*(x + 2)* ... *(x + n) = Gamma(x+n+1) / Gamma(x+1).

Gammas are computed internally in terms of their logarithms, so use these logs in the form of lgamma:

f <- function(x, n) exp(lgamma(x+n+1) - lgamma(x+1))

A plot can then be obtained via curve, for instance, as in

curve(f(x,3), 0, pi)

enter image description here

whuber
  • 2,379
  • 14
  • 23
1

You want something like this?

f <- function(x, n) {
  return(prod(1/(x+(1:n))))
}
Martin Schmelzer
  • 23,283
  • 6
  • 73
  • 98