Help me with differential calculus! I love programming and know every language, but I suck at math. Unfortunately, my school requires that computers students must take a year of calculus. There's a test next week, and I don't know any of the formulas for derivatives!

Please help me find the formulas. I need a cheat sheet - a program (as short as possible so my teacher won't notice it) that takes an expression (like 4*x^3-2) as input and outputs the derivative. (I don't care if the input and output uses command line arguments, STDIN, STDOUT, or whatever, since I'm doing all of the calculation in my head anyway.)

The test covers the following types of functions:


Constants, like -3 or 8.5
Power functions, like x^0.5 or x^-7
Exponential functions, like 0.5^x or 7^x (the base is always positive)
A constant multiplied by a  function, like 3*x^5 or -0.1*0.3^x
The sum and difference of multiple functions, like -5*x^2+10-3^x


My teacher always formats his questions in the exact same way, as shown above. He also doesn't use any fractions, numbers like pi or e, or really big numbers (bigger than 1,000). He never uses parentheses, and always shows multiplication using an asterisk (*). The only variable used is always x.

On the other hand, my teacher is pretty lenient about answers. They don't need to be simplified at all or formatted exactly as shown above, as long as it's clear what the answer is saying.

While I can use any language, remember that I can't figure out derivatives by myself. So if the program uses built-in functions to deal with equations or calculate derivatives, I won't be able to use it.

During the test, I won't have access to the Internet or any files other than the program on the cheat sheet.

Note: This scenario is entirely fictional. In real life, cheating and helping others cheat is wrong and should never be done.
Perl - 121 122

(+2 for -p)

s/(?&lt;![-\d.*^])-?[\d.]+(?![*^\d.])/0/g;s/(?&lt;!\^)x(?!\^)/1/g;s/x\^(-?[\d.]+)/"$1*x^".($1-1)/ge;s/([\d.]+)\^x/ln($1)*$&amp;/g


Test:

$ perl -p diff.pl &lt;&lt; EOF
&gt; -3
&gt; 8.5
&gt; x^0.5
&gt; x^-7
&gt; 0.5^x
&gt; 7^x
&gt; 3*x^5
&gt; -0.1*0.3^x
&gt; -5*x^2+10-3^x
&gt; EOF
0
0
0.5*x^-0.5
-7*x^-8
ln(0.5)*0.5^x
ln(7)*7^x
3*5*x^4
-0.1*ln(0.3)*0.3^x
-5*2*x^1+0-ln(3)*3^x