more specifically, $C x \log_2(x)- K = 0$, where $C$ and $K$ are constants ($\log_2$ means log base 2).
I was solving a topcoder problem, SortEstimate, which requires us to solve the aforementioned equation. (Side question: can it be called a polynomial? No? Then what is the name of such an equation?)
I used Binary search to solve this in C++ and was able to do so successfully to high accuracy. But then I came across a rather different solution which I think, having studied Numerical Analysis in college, may be using one of methods for root finding. I want to find the method, but haven't found anything useful on google or other numerical analysis resources available online.
Here's the code – also $K$ in the equation is the variable time
here
#include <iostream>
#include <cmath>
using namespace std;
class SortEstimate
{
public:
double howMany(int c, int time){
long double x = 7;
long double r = double(time)/double(c);
for (int i = 0; i<1000; i++) {
x = x - (x * log(x)/log(2.0) - r)/(1.0 + log(x)/log(2.0));
}
return x;
}
};
int main()
{
SortEstimate sort;
cout<<sort.howMany(1,8)<<endl;//4
cout<<sort.howMany(2,16)<<endl;//4
cout<<sort.howMany(37,12392342)<<endl;//23105
cout<<sort.howMany(1,2000000000)<<endl;//7.6375e+07
return 0;
}
In the for
loop, what does the statement mean? All I can think is it should be a numerical method. Any information would be useful.
If this is not the right community to ask this doubt, then please refer me to the correct one.