Advertisement
onexiv

Anti-derivative

Mar 2nd, 2025
339
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.55 KB | None | 0 0
  1. // Impossible is not for the faint of heart.
  2.  
  3. #include <fstream>
  4. #include <iostream>
  5. #include <vector>
  6. #include <string>
  7. #include <cmath>
  8.  
  9. int main() {
  10.     std::ifstream input_file("comp", std::ios::binary);
  11.     std::string input((std::istreambuf_iterator<char>(input_file)), std::istreambuf_iterator<char>());
  12.    
  13.     const int N = input.length();
  14.     std::vector<double> x(N + 1);
  15.     // input = "We the People We the People";  // First 5 characters of the US Constitution
  16.     // input.resize(N);
  17.     x[0] = 1;
  18.     std::cout << "Original string: " << input << std::endl;
  19.  
  20.     std::cout << "Forward x_n sequence: ";
  21.     for (int n = 0; n < N; ++n) {
  22.         unsigned char y = input[n];
  23.         x[n + 1] = x[n] + (static_cast<double>(y) / (n + 1));
  24.         std::cout << x[n] << " ";
  25.     }
  26.     std::cout << x[N] << std::endl;
  27.  
  28.     double compressed_value = x[N];
  29.     std::cout << "Compressed value (x_N): " << compressed_value << std::endl;
  30.  
  31.     std::string decompressed;
  32.     for (int n = N - 1; n >= 0; --n) {
  33.         double y_calc = (x[n + 1] - x[n]) * (n + 1);
  34.         int y = std::round(y_calc);
  35.         y = std::max(0, std::min(255, y));
  36.         decompressed = static_cast<char>(y) + decompressed;
  37.         x[n] = x[n + 1] - (static_cast<double>(y) / (n + 1));
  38.     }
  39.  
  40.     std::cout << "Decompressed string: " << decompressed << std::endl;
  41.  
  42.     if (input == decompressed) {
  43.         std::cout << "Compression is fully reversible." << std::endl;
  44.     } else {
  45.         std::cout << "Reversibility failed." << std::endl;
  46.     }
  47.  
  48.     return 0;
  49. }
  50.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement