Loading Now

solveEquation

Jack is a naughty boy and he does not obey any teacher except his Math teacher. The only reason is that Math is the only subject he’s interested in at school. Other teachers worry about him and one day, they decided to tell Jack’s Math teacher – Mr. Johnson to find a solution for it. Mr Johnson likes Jack very much because Jack’s talented in Math. When he heard the complaints about Jack, he’s very sad. And he tried to find a way to solve this. Finally, he thought that there was no way better than giving Jack a very difficult task so that he can not solve and make he promise that if he can not resolve it, he must obey other teachers, otherwise, other teachers must give him a gift and never complain about him anymore. But Mr. Johnson worried that the task was not difficult enough to defeat Jack. So before giving Jack the task, he asked you to test that task and then tell him if this task was ok. Please help him. Now this is that task:
Given the number n. Find the smallest number x that satisfies the following equation:
 x2 + s(x) * x - n = 0 
Where s(x) is the sum of all digits of x
If x does not exist, return -1.

Example: 

  • For input n = 2. The output should be: solveEquation(n) = 1
    Because 12 + 1 * 1 - 2 = 0 
  • For input n = 4. The output should be: solveEquation(n) = -1
    There is no number that satisfies the equation.

Input/Output:

  • [Execution time limit] 0.5s (C++), 3s (Java , C#), 4s (Python,JavaScript)

  • [Input] int64 n

     1 ≤ n ≤ 1018 

  • [Output] int64
    number x that satisfies the equation. If there is no answer, return -1.

Post Comment

Contact