Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <algorithm>
- #include <iostream>
- #include <vector>
- #include <cmath>
- #include <exception>
- int64_t fib(int64_t n)
- {
- if (n == 0 || n == 1)
- return 1;
- int64_t a = 1, b = 1, res = 0;
- for (int64_t i = 1; i < n; ++i)
- {
- res = a + b;
- a = b;
- b = res;
- }
- return res;
- }
- double fibonacci(double a, double b, double (*f)(double x), int64_t n)
- {
- if (a > b)
- std::swap(a, b);
- double lambda, mu;
- int64_t fibo;
- for (int64_t k = 0; k < n; ++k)
- {
- fibo = fib(n - k + 1);
- lambda = a + (double)(fib(n - k - 1)) / fibo * (b - a);
- mu = a + (double)(fib(n - k)) / fibo * (b - a);
- if (f(lambda) > f(mu))
- {
- a = lambda;
- }
- else
- {
- b = mu;
- }
- }
- return (a + b) * 0.5;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement