Sharing code logic between php and javascript

I have a website that does some complex math on the server side (php) and on the client side (javascript). The math is identical on both sides, and the code was manually translated between the languages. The problem is in code maintenance: one always needs to update and validate both sides when doing a modification. Is there a way to have to maintain only one side, and have the other side reflect the changes?

Any other options? Are there automatic code translaters that could auto-generate some core files?

One solution not considered si to do ajax requests to the server instead of doing the math client-side. This has been rejected due to the response time being too long.