Guest User

Untitled

a guest
Jun 23rd, 2024
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.88 KB | None | 0 0
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <string>
  4. #include <iomanip>
  5.  
  6. class Hasher
  7. {
  8. public:
  9.     int count = 0;
  10.  
  11.     int hash(int a, int b)
  12.     {
  13.         count += 1;
  14.         return a ^ b; // Pretend this is a very cryptographically secure one way hash function
  15.     }
  16. };
  17.  
  18. // Calculates number of "rehashes" we need to make for this iteration
  19. int calculate_number_of_rehashes(int iteration, int n)
  20. {
  21.     if (iteration == 0)
  22.     {
  23.         // First iteration. Calculate hash for entire list
  24.         return n;
  25.     }
  26.  
  27.     int i = 2;
  28.     while (iteration % i == 0)
  29.     {
  30.         iteration /= i;
  31.         i += 1;
  32.     }
  33.     return i;
  34. }
  35.  
  36. int main()
  37. {
  38.     size_t n = 10;
  39.     std::vector<int> input(n);
  40.     std::vector<int> hashes(n);
  41.     std::vector<int> counts(n);
  42.     int iteration = 0;
  43.     Hasher hasher;
  44.  
  45.     for (int i = 0; i < n; i++)
  46.     {
  47.         input[i] = i;
  48.         counts[i] = 0;
  49.     }
  50.  
  51.     do
  52.     {
  53.         int number_of_rehashes = calculate_number_of_rehashes(iteration, n);
  54.         for (int i = n - number_of_rehashes; i < n; i++)
  55.         {
  56.             counts[i] += 1;
  57.             if (i == 0)
  58.             {
  59.                 hashes[i] = hasher.hash(42, input[i]);
  60.             }
  61.             else
  62.             {
  63.                 hashes[i] = hasher.hash(input[i - 1], input[i]);
  64.             }
  65.         }
  66.         iteration += 1;
  67.     }
  68.     while (std::next_permutation(input.begin(), input.end()));
  69.  
  70.     std::cout << "Hashes: " << hasher.count << std::endl;
  71.     std::cout << "Iterations: " << iteration << std::endl;
  72.    
  73.     // Note how this number converges closer and closer to e = 2.718281828
  74.     std::cout << "Factor: " << std::setprecision(15) << (double) hasher.count / iteration << std::endl;
  75.  
  76.     std::cout << "Counts: " << std::endl;
  77.     for (int i = 0; i < n; i++)
  78.     {
  79.         std::cout << i << ": " << counts[i] << std::endl;
  80.     }
  81. }
  82.  
  83. /*
  84. Results:
  85.  
  86. n = 10:
  87.     Hashes: 9864100
  88.     Iterations: 3628800
  89.     Factor: 2.71828152557319
  90.     Counts:
  91.     0: 10
  92.     1: 90
  93.     2: 720
  94.     3: 5040
  95.     4: 30240
  96.     5: 151200
  97.     6: 604800
  98.     7: 1814400
  99.     8: 3628800
  100.     9: 3628800
  101.  
  102.     real    0m0.362s
  103.     user    0m0.362s
  104.     sys     0m0.000s
  105.  
  106. n = 11:
  107.     Hashes: 108505111
  108.     Iterations: 39916800
  109.     Factor: 2.71828180114638
  110.     Counts:
  111.     0: 11
  112.     1: 110
  113.     2: 990
  114.     3: 7920
  115.     4: 55440
  116.     5: 332640
  117.     6: 1663200
  118.     7: 6652800
  119.     8: 19958400
  120.     9: 39916800
  121.     10: 39916800
  122.  
  123.     real    0m4.044s
  124.     user    0m4.044s
  125.     sys     0m0.000s
  126.  
  127. n = 12:
  128.     Hashes: 1302061344
  129.     Iterations: 479001600
  130.     Factor: 2.71828182619849
  131.     Counts:
  132.     0: 12
  133.     1: 132
  134.     2: 1320
  135.     3: 11880
  136.     4: 95040
  137.     5: 665280
  138.     6: 3991680
  139.     7: 19958400
  140.     8: 79833600
  141.     9: 239500800
  142.     10: 479001600
  143.     11: 479001600
  144.  
  145.     real    0m47.669s
  146.     user    0m47.669s
  147.     sys     0m0.000s
  148.  
  149. */
Add Comment
Please, Sign In to add comment