The goal is simple: Output a nonzero real solution x
to the equation sin(x) = -mx
, given input m
, in the fewest number of bytes.
Specifications:
- Your answer must be correct to 3 significant figures.
- You may output any real solution other than the trivial solution
x=0
. You can assumem
is such that at least one solution exists. You may also assumem!=0
.
An obviously suboptimal python solution using gradient descent:
from math import *
from random import *
a=x=0.001
m = 5.
def dE(x):return 2*(sin(x)+m*x+1)*(cos(x)+m)
for i in xrange(1000): x-=dE(x)*a
print x
Test cases
-0.25 -> ±2.4746
-0.1 -> ±2.8523 or ±7.0682 or ±8.4232
0.2 -> ±4.1046 or ±4.9063
a
to solvesin(x)=-ax
. Please don't say "you have to actually compute it", since requirements like that are too vague to work. \$\endgroup\$ – xnor Oct 27 '16 at 5:09x=0
is a trivial solution. You should specify which solution you want. \$\endgroup\$ – xnor Oct 27 '16 at 5:10m=0
has solutions (x=kπ
for integerk
). The values ofm
which don't have non-trivial real solutions are those which are too far from0
. \$\endgroup\$ – Peter Taylor Oct 27 '16 at 5:54