21
Symbolic Differentiation 1: Gone Coefishin'
Task
Write a program that takes in a polynomial in x from stdin (1 < deg(p) < 128) and differentiates it. The input polynomial will be a string of the following form:
"a + bx + cx^2 + dx^3 +" ...
where the coefficient of each term is an integer (-128 < a < 128). Each term is separated by one space, a +, and another space; linear and constant terms appear as above (i.e., no x^0
or x^1
). Terms will appear in order of increasing degree, and those powers with zero coefficient are omitted. All terms with coefficient 1 or -1 display that coefficient explicitly.
Your output must have precisely the same form. Note that coefficients in the output might be as big as 127*127 == 16129.
Examples
"3 + 1x + 2x^2" ==> "1 + 4x"
"1 + 2x + -3x^2 + 17x^17 + -1x^107" ==> "2 + -6x + 289x^16 + -107x^106"
"17x + 1x^2" ==> "17 + 2x"
Scoring
Your score is the length of your program in bytes, multiplied by three if you use a built-in or a library that does symbolic algebra.
I can't believe that we haven't already had this challenge here! – flawr – 2015-08-01T14:19:47.373
5
@flawr We sort of did. (Although that one required other functions as well and didn't have as strict rules on the output format.)
– Martin Ender – 2015-08-01T14:21:15.040@flawr I thought the same thing... and yet I didn't find Martin's linked post searching. Ah well. – hYPotenuser – 2015-08-01T15:51:32.370