Tuesday, January 5, 2016

Today we revisit the number systems in octal and hexadecimal. The base for these number systems is 8 and 16 respectively. Therefore the distinct digit representations are that many in number. As the numbering continues beyond the base it wraps around to more number of digits. For example, 8 is 10 in Octal and 16 is 10 in hexadecimal. Similarly 31 is 1F and 32 is 20 in hexadecimal. As you can see each unit increment rolls a digit to the next one and carries over to the next place order in that system and this occurs after every length(base) numbers.
Therefore if we divide the number repeatedly by the base length and collect the remainders, we get the representation in that number system. This is what we implemented earlier. Now we look at sequential incrementing.
void GetSequence(String pattern, integer number)
{ 
char[] array = pattern.ToCharArray();
int baselen = array.Length;
String num = "0";
for (int i = 0; i < number; i++)
{
int len = num.Length;
if (num[len-1].ToIndex() + 1 >= baselen){
 bool carryover = true;
 int k = 1;
 if (len-k < 0) num.padleft();
 num[len-k] = array[num[len-k].ToIndex() + 1 % baselen];
 k = k + 1;
 while (carryover)
 {
    if (len-k < 0) num.padleft();
    if (num[len-k].ToIndex() + 1 >= baselen){
        carryover = true;
    }else{
        carryover = false;
    }
    num[len-k] = array[num[len-k].ToIndex() + 1 % baselen];
    k = k+1;
 }
}else{
num[len-1] = array[num[len-1].ToIndex() + 1 % baselen];
}
}
Console.WriteLine(num);

}

No comments:

Post a Comment