-1

I want to solve this kind of problem:

I want to find the base of the system where this equation is true: 99 * 99 = 1210.

This equations seems to be written in the base, more than 10, so it's a bit confusing for me.

For bases, below 10 I am using the following methods in order to convert from Base P to base 10 or vise versa, but for bases above 10, seems that they don't work.

static int ConvertToPBase(int numberInTenBase, int P)
{
    List<int> list = new List<int>();
    while (numberInTenBase > 0)
    {
       list.Add((numberInTenBase % P));
       numberInTenBase /= P;
    }

    list.Reverse();
    string x = "";
    foreach (var item in list)
    {
        x += item;
    }

    return Convert.ToInt32(x);
}

static int ConvertTo10BaseFromP(int P, int number)
{
    int answer = 0;
    string num = number.ToString();

    for (int i = 0; i < num.Length; i++)
    {
        answer = answer * P + Convert.ToInt32(num[i].ToString());
    }

    return answer;
}
  • 1
    Possible duplicate of [Quickest way to convert a base 10 number to any base in .NET?](https://stackoverflow.com/questions/923771/quickest-way-to-convert-a-base-10-number-to-any-base-in-net) – Rivasa Jun 03 '18 at 18:18
  • I also want actual algorithm to solve this problem –  Jun 03 '18 at 18:19
  • 1
    start from base 1 (or 9 in your case) and increase the base until equal. – Slai Jun 03 '18 at 19:21

2 Answers2

1

You can solve for this directly. Remember, that the positional notation in some base b makes use of (integer) powers of b. e.g. 1210 in base 10 can be computed using (1*10)^3 + (2*10)^2 + (1*10)^1 + (0*10)^0

If you use the base as the variable, you get equations with a variable b to solve for:

(9b^1 + 9b^0) * (9b^1 + 9b^0) = 1b^3 + 2b^2 + 1b^1 + 0b^0
Hunter McMillen
  • 59,865
  • 24
  • 119
  • 170
0

Alternatively, and probably closer to what you were interested in you can try every base until you find one where the equation holds. Pseudo-code below:

start-base = 9; lhs = 0; rhs = 0;
repeat {
  # convert to base 10 for arithmetic
  lhs = to-base(from-base('99', start-base), 10) ** 2;
  rhs = to-base(from-base('1210', start-base), 10);

  start-base += 1;
} until lhs == rhs;

print start-base - 1;

Note: this does not account for the case where the equation doesn't hold in any base.

Hunter McMillen
  • 59,865
  • 24
  • 119
  • 170