Advertisement
misiekii123

Horner

May 31st, 2024 (edited)
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.41 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <chrono>
  5.  
  6. using namespace std;
  7. using namespace chrono;
  8.  
  9. double F(double x, const vector<double>& factors, const vector<int>& powers) {
  10.     double result = 0;
  11.     for (int i = 0; i < factors.size(); i++) {
  12.         double act = 1;
  13.         for (int j = 0; j < powers[i]; j++) {
  14.             act *= x;
  15.         }
  16.         result += factors[i] * act;
  17.     }
  18.     return result + 1;
  19. }
  20.  
  21. double horner(double x, const vector<double>& factors, const vector<int>& powers) {
  22.     int max_power = *max_element(powers.begin(), powers.end());
  23.  
  24.     vector<double> complete_factors(max_power + 1, 0);
  25.     for (int i = 0; i < factors.size(); ++i) {
  26.         complete_factors[max_power - powers[i]] = factors[i];
  27.     }
  28.  
  29.     double result = complete_factors[0];
  30.     for (int i = 1; i < complete_factors.size(); ++i) {
  31.         result = result * x + complete_factors[i];
  32.     }
  33.     return result;
  34. }
  35.  
  36. int main() {
  37.     double x;
  38.     cin >> x;
  39.  
  40.     vector<double> factors;
  41.     vector<int> powers;
  42.  
  43.     while (true) {
  44.         double f;
  45.         int p;
  46.         cin >> f >> p;
  47.         if (f != 0 && p != 0) {
  48.             factors.push_back(f);
  49.             powers.push_back(p);
  50.         } else {
  51.             break;
  52.         }
  53.     }
  54.  
  55.     vector<int> indices(factors.size());
  56.     for (int i = 0; i < indices.size(); ++i) {
  57.         indices[i] = i;
  58.     }
  59.  
  60.     sort(indices.begin(), indices.end(), [&](int a, int b) {
  61.         return powers[a] > powers[b];
  62.     });
  63.  
  64.     vector<double> sorted_factors(factors.size());
  65.     vector<int> sorted_powers(powers.size());
  66.     for (int i = 0; i < indices.size(); ++i) {
  67.         sorted_factors[i] = factors[indices[i]];
  68.         sorted_powers[i] = powers[indices[i]];
  69.     }
  70.  
  71.     auto start = high_resolution_clock::now();
  72.     double result_F = F(x, sorted_factors, sorted_powers);
  73.     auto stop = high_resolution_clock::now();
  74.     auto duration_F = duration_cast<duration<double>>(stop - start);
  75.     cout << "F: " << result_F << " (" << duration_F.count() << " seconds)" << endl;
  76.  
  77.     start = high_resolution_clock::now();
  78.     double result_horner = horner(x, sorted_factors, sorted_powers);
  79.     stop = high_resolution_clock::now();
  80.     auto duration_horner = duration_cast<duration<double>>(stop - start);
  81.     cout << "Horner: " << result_horner << " (" << duration_horner.count() << " seconds)" << endl;
  82.  
  83.     return 0;
  84. }
  85.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement