阅读本页:http://mathcentral.uregina.ca/QQ/database/QQ.02.06/joe1.html,我试图建立一些可能的工作,它可能不是最有效的解决方案,它也限于在32位系统上的count($primes) <= 32
。如果您需要更多,随意使用Bitset:
$primes = Array(2, 2, 2, 3, 3, 41, 53);
$num_primes = count($primes); // 7, if this is over 32, it won't work on 32bit systems
$divisors = Array();
// number of possible combinations
$limit = pow(2, $num_primes) - 1; // 127
// count a number up and use the binary
// representation to say which index is
// part of the current divisor
for($number = 0; $number <= $limit; $number++) {
$divisor = 1;
// only multiply activated bits in $number to the divisor
for($i = 0; $i < $num_primes; $i++) {
$divisor *= ($number >> $i) & 1 ? $primes[$i] : 1;
}
$divisors[] = $divisor;
}
echo implode(", ", array_unique($divisors));
这将导致到以下因数:
1, 2, 4, 8, 3, 6, 12, 24, 9, 18, 36, 72, 41, 82, 164, 328, 123, 246, 492,
984, 369, 738, 1476, 2952, 53, 106, 212, 424, 159, 318, 636, 1272, 477,
954, 1908, 3816, 2173, 4346, 8692, 17384, 6519, 13038, 26076, 52152, 19557,
39114, 78228, 156456
找到你需要在每一个可能的相互乘以每一个素因子所有分频器组合。为此,我计算可能的组合数($limit
)。如果你现在算数达此限制二进制表示看起来是这样的:
7 bit
<----->
0000000 0
0000001 1
0000010 2
0000011 3
0000100 4
0000101 5
0000110 6
0000111 7
0001000 8
0001001 9
...
1111110 126
1111111 127
的$number
目前的二进制表示法表示的$primes
指标来计算当前$divisor
。为了更好地展示这一点,我们假设$number = 5
,它是二进制的0000101
。并且$divisor
的计算将是2 * 1 * 2 * 1 * 1 * 1 * 1 = 4
。只有第一位和第三位被设置,因此只有数组中的第一个和第三个元素用于计算。
我希望这可以使它更清晰一点。
使用[array_unique](http://php.net/array_unique)删除重复项 – MarcDefiant 2013-02-08 12:19:03
没有这样的官方PHP函数。你将不得不编写你自己的算法。或者你可以尝试找到一个开源的实现。 – 2013-02-08 12:19:06
任何人都可以给我一个点在正确的方向,我的猜测是,我发现有多少元素在数组中,然后循环通过,所以对于上述我会有6个循环,我带来了二重奏,然后三胞胎等,但仍不知道如何实施 – user1967034 2013-02-08 12:23:10