14
2
Definition
The maxima and minima of a given function are the largest and smallest values of the function either within a given range or otherwise within the entire domain of the function.
Challenge
The challenge is to find the local maxima and minima of a given polynomial function using any method you may like. Don't worry, I will try my best to explain the challenge and keep it as simple as possible.
The input will contain all the coefficients of the single variable polynomial in either decreasing or increasing order of power (up to you). For example,
[3,-7,1]
will represent3x2 - 7x + 1 = 0
[4,0,0,-3]
will represent4x3-3=0.
How To Solve (Using Derivatives)?
Now, let's say our input is [1,-12,45,8]
, which is nothing but the function x3 - 12x2 + 45x + 8
.
The first task is to find the derivative of that function. Since it is a polynomial function, so it is indeed a simple task to do.
The derivative of
xn
isn*xn-1
. Any constant terms present withxn
are simply multiplied. Also, if there are terms added/subtracted, then their derivatives are also added or subtracted respectively. Remember, the derivative of any constant numerical value is zero. Here are a few examples:x3 -> 3x2
9x4 -> 9*4*x3 = 36x3
-5x2 -> -5*2*x = - 10x
2x3 - 3x2 + 7x -> 6x2 - 6x + 7
4x2 - 3 -> 8x - 0 = 8x
Now solve the equation by equating the new polynomial to zero and get only the integral values of x.
Put those values of x in the original function and return the results. That should be the output.
Example
Let us take the example we mentioned earlier, i.e, [1,-12,45,8]
.
- Input:
[1,-12,45,8]
- Function:
x3 - 12x2 + 45x + 8
- Derivative ->
3x2 - 24x + 45 + 0 -> [3,-24,45]
- Solving equation
3x2 - 24x + 45 = 0
, we getx = 3
orx = 5
. - Now putting
x = 3
andx = 5
in the function, we get the values(62,58)
. - Output ->
[62,58]
Assumptions
Assume that all the input coefficients are integers. They can be in increasing or decreasing order of power.
Assume the input to be at least a 2-degree polynomial. If the polynomial has no integer solutions, you can return anything.
Assume that the final result will be integers only.
You can print the results in any order. The degree of the input polynomial would not be more than 5, so that your code can handle it.
The input will be valid so that the solutions of x are not saddle points.
Also, you are not forced to do it by the derivative method. You can use any method you feel like.
Sample Input And Output
[2,-8,0] -> (-8)
[2,3,-36,10] -> (91,-34)
[1,-8,22,-24,8] -> (-1,0,-1)
[1,0,0] -> (0)
Scoring
This is code-golf so the shortest code wins.
1
If I understand correctly: in the example the step "Solving equation" would be partially this previous challenge of you? Also, step "Now putting x=3 and x=5 in the function" means the original function at "Function" and not the function at "Derivative", right?
– Kevin Cruijssen – 2018-01-31T16:11:22.2431For sample I/O 3, I'm getting
(-1, 0, 1)
, which I believe is the actual correct answer... not sure though. If you disagree with me ping me in chat. – HyperNeutrino – 2018-01-31T16:14:05.737Is it okay to take the actual polynomial as input, like
3x^2-7x+1
? – JungHwan Min – 2018-01-31T16:19:14.700@HyperNeutrino very sorry. That was a typo. Fixed now. – Manish Kundu – 2018-01-31T16:37:05.833
@JungHwan yes it's okay – Manish Kundu – 2018-01-31T16:37:47.597
@KevinCruijssen yes, absolutely. – Manish Kundu – 2018-01-31T16:59:40.503
1
The input will be valid so that the solutions of x are not saddle points
, the case[1,0,0,3]
seems to give a saddle point. – JungHwan Min – 2018-01-31T17:03:17.8471@JungHwanMin ah that example was added before the rule was made. Removed now. – Manish Kundu – 2018-01-31T17:19:24.983
Suggested test case:
[1,0,0] -> (0)
(credit to @betseg). My answer was correct for all your test cases, but failed for this one. And Arnauld's JavaScript answer also failed for this test cases. If multiple answers fail a certain test case, it's a good test case to add. :) – Kevin Cruijssen – 2018-01-31T20:04:46.2501
x^3 - 12x^2 + 45x
+8 = 0
, although personally I prefer you write it asf(x)=x^3-12x^2+45x+8
without the=0
because=0
doesn't make sense since we are dealing with a function, not solving an equation. – Weijun Zhou – 2018-01-31T20:40:06.460Under “how to solve“ there is a sign error with
+8
in the input list but-8
in the description. – Felix Dombek – 2018-01-31T22:04:19.723@FelixDombek yeah right ty. – Manish Kundu – 2018-02-01T00:40:30.863