我想解决这类问题:
我想找到这个等式为真的系统的基数:99 * 99 = 1210。
这个等式似乎写在基础上,超过10个,所以对我来说有点混乱。
对于10以下的碱,我使用以下方法从Base P转换为10,反之亦然,但对于10以上的碱,似乎它们不起作用。
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;
}
答案 0 :(得分:1)
你可以直接解决这个问题。请记住,某些基数b
中的位置表示法使用b
的(整数)幂。例如可以使用(1*10)^3 + (2*10)^2 + (1*10)^1 + (0*10)^0
如果使用base作为变量,则可以得到带有变量b的等式来求解:
(9b^1 + 9b^0) * (9b^1 + 9b^0) = 1b^3 + 2b^2 + 1b^1 + 0b^0
答案 1 :(得分: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.