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
공식적인 PHP 기능은 없습니다. 당신은 자신의 알고리즘을 작성해야합니다. 또는 오픈 소스 구현을 찾아보십시오. –
누구나 올바른 방향으로 한 지점을 줄 수 있습니까? 내 추측에 따르면 배열에 몇 개의 요소가 있는지 알아 내서 루프를 돌릴 수 있습니다. 위의 경우 6 개의 루프를 사용하여 어디에서 듀오를 가져 왔는지, 그래도 구현 방법을 모르겠다. – user1967034