fork download
  1. #include <iostream>
  2. using namespace std;
  3. #include <cmath>
  4. double f(double x){
  5. return x*x-(4*x)+3;
  6. }
  7. int main() {
  8. double a,b,eps,gold,k=0;
  9. gold=(1+sqrt(5))/2;
  10. a=0;
  11. b=3;
  12. eps=0.00001;
  13. while(abs(b-a)>eps){
  14.  
  15. double x1,x2;
  16. x1=b-((b-a)/gold);
  17. x2=a+((b-a)/gold);
  18. if(f(x1)>f(x2)){
  19. a=x1;
  20. }
  21. else{
  22. if(f(x1)<=f(x2)){
  23. b=x2;
  24.  
  25. }
  26. }
  27. k++;
  28. }
  29. cout<<(b+a)/2<<" "<<k;
  30. return 0;
  31. }
  32.  
Success #stdin #stdout 0.01s 5264KB
stdin
Standard input is empty
stdout
2 27