Permuting names with C++

One day, talking with a friend of mine, we started for some reason not necessarily known permuting our names to see how many possible combinations we could get from them.
Well doing it with small names is quite easy and fast but with longer ones not that much.

We are programmers then we decided to create a piece of code to the job for us. At first I tought Python would be the best choice for such simple task but since I was with Code::Blocks open I decided to do it in C++ to train some of my skills on the language.

I'm now sharing the simple code of permuting strings;


#include 
#include 
 
using namespace std;
 
void Permute(string word, int times=0)
{
    if(word.length() <= 1 or times == word.length())
    {
        cout << word << endl;
    }
    else
    {
        for(int i = 0; i < word.length(); i++)
        {
            if(i == word.length()-1)
                break;
            cout << word << endl;
            char letter = word[i];
            word[i] = word[i+1];
            word[i+1] = letter;
        }
        Permute(word, times+1);
    }
}
 
int main()
{
    string word;
    cout << "Digite uma palavra: ";
    cin >> word;
    Permute(word, 0);
    return 0;
}

Here is a Pastebin to better visualize the syntax highlights: C++ Permutation

How to use with binary numbers?

How to use with binary numbers?


There's something I've been noticing in my university; a lot of people seems to have great trouble in understanding how binary numbers works.
The simplicity of the system end being a hassle for some people to learn how to count or to know the decimal a binary represents.

So I decide to give some insights about the matter. Is a very short topic to be fair, reading and writing binary numbers is as easy as do the same with decimals, the main difference of course, is instead of having 10 different symbols to work with you'll have only 2. So, you'll need to increase digits more often than you would to with a decimal number.

Let's count from 0 to 10, shall we?
  • Both 0 and 1 in binary are the same as their decimal counterparts.
  • If you want to count 2, you'll need to reset 1 to 0 and add a new digit 1. So 2 in binary is 10.
    • The pattern for that is quite simple, you basically reset the last digit and position 1 in front of it. Every time you increase a new digit it'll be 1 to be added as a new digit.
  • Following the same pattern, 3 will be 11. Since you changed from 0, there's no need in add a new digit.
  • 4 now: will change 1 to 0, so we'll need to update it adding a new digit. Reset all the ones to zeros and add a new digit 1. 4 = 100.
  • 5 will follow the same, change the last digit from zero to one; 101
  • Remember, everytime your final digit is one you'll need to reset to zero and add 1 in front of it, so 6 is 110.
  • Now we can do the rest, which basically follows the same pattern;
    • 7 - 111
    • 8 - 1000
    • 9 - 1001
    • 10 - 1010
Let's make this into an algorithm;
  • You'll start from the last digit
  • Each 1 will be changed to 0
  • If a 0 is found, you change it for a 1 and stop.
  • If no 0 is found, you'll change all ones to zeros and add a new one in front of them.
Four lines and you know how to increment a number! That's awesome, you can now literally count to infinity!
Maybe you want to decrease? Well, you just need to change the zeros to ones and the ones to zero in the algorithm. Not hard, eh?

Yes, yes, I know this will not help you to for example count directly to an specific number such as 48. Unless you count one by one until 48, which would be a boring and unnecessary task. Mainly because there's better and faster ways to do it.

For counting up to higher numbers without needing to increment one by one, you can use some patterns binaries have.
For that, you'll need to remember these;

1 - 1
10 - 2
100 - 4
1000 - 8
10000 - 16
100000 - 32
1000000 - 64
...

As you can see, everytime you increase one 0 into a binary number composed by a leading 1 and the rest 0, you multiply the value by 2 (curiously enough, if you delete the last digit of a binary number you divide it by 2. A lot easier than decimals, right?).

Using this we can get 48 in binary much faster:

  • First, let's choose the biggest number which is smaller or equal than 48: 100000;
  • Now let's take a number which added to 32 gets even closer to 48: 10000;
  • Let's add the first number to the last number, since 32 + 16 = 48;
  • For that, you'll ONLY change zeros to ones when they collide;
    • 100000 + 10000 = 110000 = 48 in binary
As you can see, 1 is the only number to not be even. The motive behind that, is because we will use 1 to get odd numbers since 3 is basically 2 + 1 and 7 is 6 + 1.
So, 49 would be 100000 + 10000 + 1 = 110001 = 49 in binary

If you still have doubts about these calculations, you can get here and test converting it to decimal.

What is colliding?

It can be a little difficult to see collisions without practice, but if you want to see them visually, the best way is to organize your binaries similar as you do with decimal addition;

            100000  =  32
              10000  =  16
                     1  =   1
+ ______ 
            110001  =  49


Well, it's basically it. There's no secret in reading and writing binaries. Although normally you'll use it alongside a programming language for example thus you'll probably not to write it or read it by hand, is good to know that, because with time, you'll be able to read out loud a binary number in decimal much easy and fast.