Anyone help me to solve problems

Status
Not open for further replies.

manubatham20

Broken In
I am not getting the funda of the following problems-
1. Pascal triangle
2. Sin(x) series, where x is in radian sin x = x-(x^3/3!)+(x^5/5!)-(x^7/7!)+...


I tried the above programs many times but no result. Can anyone provide me its solutions??????????
 

QwertyManiac

Commander in Chief
Pascal's Triangle is easy if you know Combinations.

Each element is simply: P(n,k) = (n!)/(k!*(n-k)!)

Where n is the number of rows and k is 0 -> n (position value)

A simple python implementation would be like:
Code:
>>> for n in range(5): #0 to 4 = 5 rows
...     for k in range(n+1): # 0 to n = n elements
...             print fact(n)/(fact(k)*fact(n-k)),
...     print
... 
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
>>>

About the sine one, you need two counters. One odd incrementing and another for (-1)^n indication (Sign change).

In python again:
Code:
>>> def sin(x,n): # x is value, n is the iteration amount, for precision?
...     ans=0.0
...     odd=1.0
...     for p in range(n):
...             ans+=((-1)**p)*((x**odd)/fact(odd)) #(-1)^n * (x^num/num!) and on and on and on, as your series goes.
...             odd+=2.0 #increment odd number counter
...     return ans
... 
>>> sin(5,7)
-0.93758404902067816
 
K

khattam_

Guest
sin(x) series is not too difficult also...

Just sum with a loop... For alternate -ves try pow(-1,n+1)....

Thats very easy.

Do ur homework urself, or u'll not learn!
 
Status
Not open for further replies.
Top Bottom