Advertisement
Guest User

Average number of operations per element vs. n (summation)

a guest
Mar 18th, 2015
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.50 KB | None | 0 0
  1. #include <iostream>
  2. #include <math.h>
  3.  
  4. using namespace std;
  5.  
  6. int lg_floor(int x) {
  7.     int c=-1;
  8.     for(; x!=0; c++, x>>=1);
  9.     return c;
  10. }
  11. int ops_for_number(int x) {
  12.     return lg_floor(x&(-x));
  13. }
  14.  
  15. int main(int argc, char *argv[]) {
  16.     for(int n=128; n<256; n++) {
  17.         double sum = 0;
  18.         double result;
  19.         for(int j=0; j<lg_floor(n); j++) {
  20.             sum+=j*(1<<(lg_floor(n)-j-1));
  21.         }
  22.         for(int j=1<<lg_floor(n); j<=n; j++) {
  23.             sum+=ops_for_number(j);
  24.         }
  25.         result = sum/n;
  26.         printf("%f\n", result);
  27.     }
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement