Quantcast
Channel: How would I find all sets of N single-digit, non-repeating numbers that add up to a given sum in PHP? - Stack Overflow
Viewing all articles
Browse latest Browse all 9

Answer by user187291 for How would I find all sets of N single-digit, non-repeating numbers that add up to a given sum in PHP?

$
0
0

Using Combinations code from here

foreach(new Combinations("123456789", 5) as $p)    $r[array_sum(str_split($p))] .= "$p ";print_r($r); 

result

[15] => 12345 [16] => 12346 [17] => 12347 12356 [18] => 12348 12357 12456 [19] => 12349 12358 12367 12457 13456 [20] => 12359 12368 12458 12467 13457 23456 [21] => 12369 12378 12459 12468 12567 13458 13467 23457 [22] => 12379 12469 12478 12568 13459 13468 13567 23458 23467 [23] => 12389 12479 12569 12578 13469 13478 13568 14567 23459 23468 23567 [24] => 12489 12579 12678 13479 13569 13578 14568 23469 23478 23568 24567 [25] => 12589 12679 13489 13579 13678 14569 14578 23479 23569 23578 24568 34567 [26] => 12689 13589 13679 14579 14678 23489 23579 23678 24569 24578 34568 [27] => 12789 13689 14589 14679 15678 23589 23679 24579 24678 34569 34578 [28] => 13789 14689 15679 23689 24589 24679 25678 34579 34678 [29] => 14789 15689 23789 24689 25679 34589 34679 35678 [30] => 15789 24789 25689 34689 35679 45678 [31] => 16789 25789 34789 35689 45679 [32] => 26789 35789 45689 [33] => 36789 45789 [34] => 46789 [35] => 56789 

isn't it cute?


Viewing all articles
Browse latest Browse all 9

Trending Articles