problem calculating factorials

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Iidrisu
    New Member
    • Feb 2008
    • 1

    problem calculating factorials

    hi guys, i have a homework assigment that i have to complete but im a bit lost on how to do one part of it.

    the part of the question im having trouble with is this "

    Calculate sin^2(x) approximately by using n terms (n>0)
    the Taylor series

    sin(x)=Sum[(-1)^k x^(2k+1)/(2k+1)!] (k goes from 0 to n-1)

    "
    im not exactly sure how to evaluate the factorial in the equation. I know we should use a for loop but lets say i used
    for(k=0;k<n;k++ )
    j=(2k+1)*j

    that wont work because lets say n=3 it would say (1)*(3)*(5) ect. and not actually do (1)(2)(3)(4)(5) .
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Suppose you have the k-th term of that Taylor series; express the k+1st term
    in terms of the k-th term; note hat (2k+1)! == (2k-1)!*k*(k+1)

    kind regards,

    Jos

    Comment

    • Laharl
      Recognized Expert Contributor
      • Sep 2007
      • 849

      #3
      n!, mathematically, is the product n(n-1)...(2)(1). Thus, (2k-1)!=(2k-1)(2k-2)...

      To represent this in code, start by writing a loop to calculate n!, where n is any integer. A function is a good way to do this. Then, apply the function to your current problem.

      Comment

      Working...