// \begin{verbatim} #include #include #include #include #include #include #include #include #include using namespace std; const int N = 30; // limits the depth of the recursion float guess(float x, float b, int M) { assert(x > 0.0 and b > 1.0); if(x == 1) return 0; else if(M <= 0) return 0; // Really? Shouldn't it be 1? Does it matter? else if(x < 1) return -guess(1/x,b,M); else if(x > b) return 1 + guess(x/b,b,M); else if(x < b) return guess(x*x,b,M-1)/2; else return 1.0; } float guess(float x, float b) { cout << "Computing guess(" << x << "," << b << ")" << endl; return guess(x,b,N); } int main() { float x; cout << "Enter a positive number x: "; cin >> x; cout << endl; float b; cout << "Enter a number larger than 1: "; cin >> b; cout << endl; cout << guess(x,b) << endl; return 1; } // \end{verbatim}