Search code examples
algorithmmathradix

Direct way of converting BASE-14 to BASE-7


Given (3AC) in base-14. Convert it into BASE-7.

A simple approach is to convert first 3AC into BASE-10 & then to BASE-7 which results in 2105.

I was just wondering that does there exist any direct way of conversion from BASE-14 to BASe-7?


Solution

  • I have found one approach.

    There is no need to calculate for base 10 and then base 7. It can be done using this formula! If a no X is represented in base 14 as X = an a(n-1) a(n-2) .... a(0)

    then in base 7 we can write it as

    X=.....rqp

    where
    p=(2^0)a(0)%7;
    q=((2^1)a(1) + p/7)%7
    r=((2^2)a(2) + q/7)%7

    ..........
    nth term=((2^n)a(n) + (n-1)th term/7)%7
    (will go further because a no. in base 14 will require more digits in base 7).

    The logic is simple, just based on properties of bases, and taking into account the fact that 7 is half of 14. Else it would have been a tedious task.

    Eg. here it is given 3AC.
    C =12;
    so last digit is (2^0 * 12)%7 = 5
    A=10
    next digit is (2^1 * 10 + 12/7)%7 = (20+1)%7=21%7=0
    next is 3;
    next digit is (2^2 * 3 + 21/7)%7 = (12+3)%7=15%7=1
    next is nothing(0);
    next digit is (2^3 * 0 + 15/7)%7 = (0+2)%7=2%7=2

    Hence, in base 7 number will be, 2105. This method may seem confusing and difficult, but with a little practice, it may come very handy in solving similar types of problems! Also, even if the number is very long, like 287AC23B362, we don't have to unnecessarily find base 10, which may consume atleast some time, and directly compute base 7!