About Polynomial Calculation

hello Guys!
I have a task from my lecturer to make output program like this


Polynomial (a+b)^n
===================

Input value n [1..9] : 2
Input value a [0..9] : 3
Input value b [0..9] : 4

Result of (a+b)^2 :

= 1(a^2*b^0) + 2(a^1*b^1) + 1(a^0*b^2)

= 1(9) + 2(12) + 1(16)

= 9 + 24 + 16

= 49


but I have a Problem about displaying this.


Result of (a+b)^2 :

= 1(a^2*b^0) + 2(a^1*b^1) + 1(a^0*b^2)

= 1(9) + 2(12) + 1(16)

= 9 + 24 + 16

= 49


can anyone tell me how to do it?
please tell in c example, if it is possible.thanks yeah!
Last edited on
please tell in c example
wouldn't that be a little too easy?
k'th member of (a+b)n is n!/(k!*(n-k)!) * a(n-k) * bn
don't know what other problems you could be having..
easy? don't know about that..
I am still beginner, and recently begin using C..
so please help me yeah?

the problem is.. I am being asked to make a recursive coding
to display this.. I really don't get it..

for example when you input..
n=2 ; a=1 ; b=1 ;
the Program Automatically Display this calculation..

Result of (a+b)^2 :

= 1(a^2*b^0) + 2(a^1*b^1) + 1(a^0*b^2)

= 1(1) + 2(1) + 1(1)

= 1 + 2 + 1

= 4


then if you make another input like this..
n=3 ; a=1 ; b=1 ;
then it display..

Result of (a+b)^3 :

= 1(a^3*b^0) + 3(a^2*b^1) + 3(a^1*b^2) + 1(a^0*b^3)

= 1(1) + 3(1) + 3(1) + 1(1)

= 1 + 3 + 3 + 1

= 8
I can't think of any (sensible) way to use recursion here..

Can you do the C(k) = n!/(k!*(n-k)!) part?
If you can,
print '= '
for k = 0 to n
   print C(k), '(a^', n-k, '*b^', k, ') + ' //think of a way to deal with the last '+'

print '= '
for k = 0 to n
   print C(k), '(', pow(a, n-k)*pow(b, k), ') + '

print '= ', pow(a+b, n)

Another way to get C is to use Pascal's triangle.
Last edited on
hmm, okay, I got it..

and how about pascal's triangle?
http://en.wikipedia.org/wiki/Pascal's_triangle
in Pascal's triangle, n'th row consists of all the coefficients of (a+b)n (n starts from 0)
if you only allow n from 1 to 9, you could hard code it. if you want it to be more dynamic, you could use recursion, but I assume then factorials would be faster.
Topic archived. No new replies allowed.