SHARE
TWEET

Average number of operations per element vs. n (brute force)

a guest Mar 18th, 2015 31 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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))+1;
  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=1; j<=n; j++) {
  20.                         sum+=ops_for_number(j);
  21.                 }
  22.                 result = sum/n;
  23.                 printf("\n%f\n", result);
  24.         }
  25. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top