52
3
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
or8.5
- Power functions, like
x^0.5
orx^-7
- Exponential functions, like
0.5^x
or7^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.
3Can we expect that
x
is always the variable to differentiate? – Kyle Kanos – 2014-06-17T13:39:13.0272Must the answer be simplified? Do we need to add like terms? – Rainbolt – 2014-06-17T13:40:46.110
Will any numbers be in scientific notation? – Martin Ender – 2014-06-17T14:02:21.097
Will there be any product rule? – scrblnrd3 – 2014-06-17T14:28:58.247
1
I guess it's time for my calculus project at http://scrblnrd3.github.io/Javascript-CAS/ to shine if I can actually golf it
– scrblnrd3 – 2014-06-17T14:33:02.6471Should we assume there are no parens? – Not that Charles – 2014-06-17T14:59:57.700
2
I've answered most of these questions in my edit. There is no scientific notation or product rule.
– Ypnypn – 2014-06-17T17:08:10.3201Oh. So no chain rule. – Cruncher – 2014-06-18T18:13:12.317
I'm sure a math language such as Maple would make short work of this problem. – MBraedley – 2014-06-18T19:20:46.347
@MBraedley However: http://meta.codegolf.stackexchange.com/a/1078/9498
– Justin – 2014-06-19T06:39:34.373Can
x^x
appear? – celtschk – 2014-06-19T12:56:28.257Also, is
-2^x
to be interpreted as-(2^x)
or as(-2)^x
? – celtschk – 2014-06-19T12:58:00.087@celtschk
x^x
will not appear (since it requires using techniques other than the ones listed). -2^x means -(2^x) (since, as mentioned, the base will never be negative). – Ypnypn – 2014-06-19T13:19:46.5771
--Views hot network questions: Help me with differential calculus! "I know differential calculus, maybe I can help this guy." --Reads question --Reviews answers "WTH, these answers are all terrible." On a serious note, I think it's an indication of a good question setup that two separate posters were convinced your situation was real.
– primo – 2014-06-19T17:46:12.707