r/CoderTrials • u/07734willy • Jul 08 '18
CodeGolf [Easy] Solving a Small Equation
This problem is a codegolf problem. That means the objective isn't to just solve the problem, but to do so with the smallest program size. Everything from comments, spaces, tabs, and newlines counts against you.
Background
While there certainly are some complex mathematical equations that are too difficult to solve, most of the ones using the basic mathematical operations (addition, multiplication, exponentiation...) are usually fairly easy. Especially when they they only have one variable, and one operator. However, one particularly difficult equation stands out:
x^x = kWhere ^ denotes exponentiation, and k is some constant we choose. This may look trivial to solve, and its worth taking a stab at it to convince yourself there isn't a simple way to approach this, apart from approximation.
Your task is to write a program to solve for x, to 5 decimal digits of precision, for a provided constant k.
Input
A single number- the k in x^x = k. Example:
743Output
A number, for which the x in x^x = k is accurate to 5 decimal places. For the above input, we would have:
4.43686Testcases
=========
743
4.43686
=========
97
3.58392
=========
256
4.0
=========
947
4.53387
=========
15
2.71316
=========
78974
6.18749
=========
11592.347
5.49334
=========Character Count
Use the following command to measure the size of the program, in bytes and characters:
wc -mc filename.txt
View all comments
Show parent comment
1
u/engiwengi Jul 08 '18
A little prettier.
Pretty sweet visualisation of how this method works from wikipedia. I start with an arbitrary guess of 1 and assume that it will converge fast enough in 10 iterations. I think for really huge numbers it might not get to 5 decimal places quick enough but not certain.