fork download
  1. /* Bisection method to approximate solution to X^X=55 */
  2.  
  3. #include <iostream>
  4. #include <math.h>
  5. using namespace std;
  6.  
  7. int main() {
  8. void bisec(double,double);
  9. bisec(3,4); // trivial guess [3,4] of interval where the root lies in
  10. return 0;
  11. }
  12. void bisec(double lo,double hi)
  13. {
  14. double root,res;
  15. root=(lo+hi)/2; // bisecting the interval
  16. res=pow(root,root)-55; // polynomial evaluation
  17. if(hi-lo<pow(10,-6))
  18. {
  19. cout<<root;
  20. exit(0);
  21. }
  22. (res<0) ? bisec(root,hi) : bisec(lo,root); // recursive call depending on sign of res
  23. }
Success #stdin #stdout 0s 3460KB
stdin
Standard input is empty
stdout
3.33065