Php 从数组中获取所有数字组合返回内存不足错误
我想找到从包含几个示例数字的数组开始的所有组合:Php 从数组中获取所有数字组合返回内存不足错误,php,math,Php,Math,我想找到从包含几个示例数字的数组开始的所有组合: $array = [1,10,20,32,12,60,54,90,20,11,2,3,4,5,6,7,8,9,22,33,61,62,69,49, ...]; // example 59 numbers 我尝试了各种脚本(来自其他脚本),以找到大小为10的组合,例如: function sampling($chars, $size = 10, $combinations = array()) { // if it's the
$array = [1,10,20,32,12,60,54,90,20,11,2,3,4,5,6,7,8,9,22,33,61,62,69,49, ...]; // example 59 numbers
我尝试了各种脚本(来自其他脚本),以找到大小为10的组合,例如:
function sampling($chars, $size = 10, $combinations = array()) {
// if it's the first iteration, the first set
// of combinations is the same as the set of characters
if (empty($combinations)) {
$combinations = $chars;
}
// we're done if we're at size 1
if ($size == 1) {
return $combinations;
}
// initialise array to put new values in
$new_combinations = array();
// loop through existing combinations and character set to create strings
foreach ($combinations as $combination) {
foreach ($chars as $char) {
$tmp = $combination . $char;
if ($size == 2) {
$tmp .= '.com';
}
$new_combinations[] = $tmp;
// I don't get what you were going to do with this line,
// but this looks like a logical bug in your code
//$new_combinations[] = implode(".com", $new_combinations);
}
}
// call same function again for the next iteration
return $this->sampling($chars, $size - 1, $new_combinations);
}
或与:
错误:
致命错误:内存不足(已分配20061356032)(试图分配
20480字节)
我试图增加记忆:
- php.ini:
内存限制=-1
- 使用脚本标记
ini\u集(“内存限制”、“-1”)代码>
我使用
$this->sampling
,因为函数是类中的您只需使用单个数组即可实现所有排列,但将其发送到浏览器的负载将是巨大的。最好使用“下一步”按钮使浏览器中的UI智能化;)实际上,它只是一个个人工具,所以时间并不重要。你能解释一下我是怎么做到的吗?如果我正确地计算餐巾纸的背面,20061356032
大约是18GB,这是一个很大的内存,我猜可能是交换。你有足够的空间吗?另外,24项中的10项是63万亿的排列或7万亿的组合,将你带入了TB的领域。你如何计算这个数字(63万亿)@ChrisHaas?那么您认为这是不可能的吗?这是可能的,但是您应该专注于以这样一种方式编写代码,即您不需要存储任何超出绝对必要的内容。理想情况下,您一次只能处理一种排列和/或组合。非常适合这种情况,我之前编写了一个小库,它使用生成器生成给定集合的置换和组合:
require_once 'Math/Combinatorics.php';
$numbers = [1,10,20,32,12,60,54,90,20,11,2,3,4,5,6,7,8,9,22,33,61,62,69,49, ...];
$combinatorics = new Math_Combinatorics;
foreach($combinatorics->permutations($numbers, 10) as $p) {
echo join(' ', $p), "\n";
}