Code Inefficiency?

View previous topic View next topic Go down

Code Inefficiency?

Post by Gecko on 9/10/2014, 3:04 pm

So, it occurs to me, my code may be a bit inefficient... Has been running for 17hours now... If I was less lazy, and not too far in I would quickly rewrite it, but I'll probably do it anyways. 'tis solving the Lattice Paths problem on Project Euler, I know a considerably easier way to solve it, but I wanted to do it a unique way.

Code:
//Lattice Paths
#include <iostream>
extern "C"
 {
    #include <pthread.h> //Not Used, was going to multithread, but could remember how at the time.
    #include <unistd.h>
 }
using namespace std;

bool checkBin(unsigned long long bin, int x)
{
   bool buffer[32] = {0};
   int i = 0;
   int count = 0;
   while(bin > 0)
   {
      if(bin % 2 != 0)
      {
         buffer[i] = 1;
      }
      else
      {
         buffer[i] = 0;
      }
      bin /= 2;
      i++;
   }
   for(int i = 0; i < 32; i++)
   {
      count += buffer[i];
   }
   if(count == x)
   {
      return true;
   }
   return false;
}

int main()
{
   int x;
   unsigned long long count = 0;
   cout << "Enter Size of Grid: ";
   cin >> x;
   unsigned long long bufBin = (pow(2, x) - 1);
   
   
   for(unsigned long long i = 0; i < bufBin; i++)
   {
      count += checkBin(i, x);
   }
   
   cout << (count * 2) + 2;
   
   return 0;
}
avatar
Gecko
Tier 4 (500 posts)
Tier 4 (500 posts)


Back to top Go down

View previous topic View next topic Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum