如何在 php 中定义自定义基数?
How to define a custom base of numbers in php?
我需要为自定义计算定义新的基数,
我有一个 11 个字符的顺序列表(比方说 a、h、k、d、e、f、g、z、i、j、m)
我希望能够对它们执行数学任务,就像它们是数字基础一样。
例如 a+h=k, a+k=d, j++=m 等等...
这可能吗?
我想到的最好的方法是采用 11 的常规基数,并在计算前后替换所有字符(因此 j++ 实际上是 9++=a,然后 a 将被翻译成 m)。
这种方式效率不高,但可以。
有更好的主意吗?
谢谢。
PHP 提供对十进制、八进制、十六进制和二进制数进行本地数学运算。如果您想使用其他类型的数字系统进行计算,您需要将它们转换为上述类型之一。
基本上您使用的是 base11 数字系统 - 具有自定义数字。 PHP 提供函数 base_convert()
以在具有不同基数的系统之间转换数字。有了这个,您只需要将自定义数字转换为 base11,然后将 base11 转换为十进制,然后进行计算,然后将其转换回来。
Hackish!,但可以这样做。
function add($a, $b) {
$custom_digits = 'ahkdefgzijm';
$base11_digits = '0123456789A';
// translate custom numbers to base11
$base11_a = strtr($a, $custom_digits, $base11_digits);
$base11_b = strtr($a, $custom_digits, $base11_digits);
// translate base11 numbers to decimal
$decimal_a = base_convert($base11_a, 11, 10);
$decimal_b = base_convert($base11_b, 11, 10);
// Do the calculation
$result = $decimal_a + $decimal_b;
// Convert result back to base11
$base11_result = base_convert($result, 10, 11);
// Translate base11 result into customer digits
return strtr($base11_result, $base11_digits, $custom_digits);
}
永远不会忘记!:
h + h == k
:)
更灵活的尝试可能是创建两个这样的函数:
function dec_to_custom($n) {
static $custom_digits = 'ahkdefgzijm';
static $base11_digits = '0123456789a';
return strtr(base_convert($n, 10, 11), $base11_digits, $custom_digits);
}
function custom_to_dec($n) {
static $custom_digits = 'ahkdefgzijm';
static $base11_digits = '0123456789a';
$base11 = strtr($n, $custom_digits, $base11_digits);
return base_convert($base11, 11, 10);
}
并在(整数!)数学运算中随心所欲地使用它们:
echo dec_to_custom(custom_to_dec(1) + custom_to_dec(1));
更新
看来我回答得太快了。您已经有了我建议的解决方案,并且您担心 strtr()
的用法。我只能说我做过一次类似的任务并做了很多分析并最终使用 strtr()
因为它表现出最好的性能。
我需要为自定义计算定义新的基数,
我有一个 11 个字符的顺序列表(比方说 a、h、k、d、e、f、g、z、i、j、m) 我希望能够对它们执行数学任务,就像它们是数字基础一样。 例如 a+h=k, a+k=d, j++=m 等等...
这可能吗?
我想到的最好的方法是采用 11 的常规基数,并在计算前后替换所有字符(因此 j++ 实际上是 9++=a,然后 a 将被翻译成 m)。 这种方式效率不高,但可以。
有更好的主意吗?
谢谢。
PHP 提供对十进制、八进制、十六进制和二进制数进行本地数学运算。如果您想使用其他类型的数字系统进行计算,您需要将它们转换为上述类型之一。
基本上您使用的是 base11 数字系统 - 具有自定义数字。 PHP 提供函数 base_convert()
以在具有不同基数的系统之间转换数字。有了这个,您只需要将自定义数字转换为 base11,然后将 base11 转换为十进制,然后进行计算,然后将其转换回来。
Hackish!,但可以这样做。
function add($a, $b) {
$custom_digits = 'ahkdefgzijm';
$base11_digits = '0123456789A';
// translate custom numbers to base11
$base11_a = strtr($a, $custom_digits, $base11_digits);
$base11_b = strtr($a, $custom_digits, $base11_digits);
// translate base11 numbers to decimal
$decimal_a = base_convert($base11_a, 11, 10);
$decimal_b = base_convert($base11_b, 11, 10);
// Do the calculation
$result = $decimal_a + $decimal_b;
// Convert result back to base11
$base11_result = base_convert($result, 10, 11);
// Translate base11 result into customer digits
return strtr($base11_result, $base11_digits, $custom_digits);
}
永远不会忘记!:
h + h == k
:)
更灵活的尝试可能是创建两个这样的函数:
function dec_to_custom($n) {
static $custom_digits = 'ahkdefgzijm';
static $base11_digits = '0123456789a';
return strtr(base_convert($n, 10, 11), $base11_digits, $custom_digits);
}
function custom_to_dec($n) {
static $custom_digits = 'ahkdefgzijm';
static $base11_digits = '0123456789a';
$base11 = strtr($n, $custom_digits, $base11_digits);
return base_convert($base11, 11, 10);
}
并在(整数!)数学运算中随心所欲地使用它们:
echo dec_to_custom(custom_to_dec(1) + custom_to_dec(1));
更新
看来我回答得太快了。您已经有了我建议的解决方案,并且您担心 strtr()
的用法。我只能说我做过一次类似的任务并做了很多分析并最终使用 strtr()
因为它表现出最好的性能。