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
Browsing all 9 articles
Browse latest View live

Answer by Rahul Singh for How would I find all sets of N single-digit,...

Print sum of numbers form an array of n numbers having no duplicate digit in it. For exampleInput: [100, 213, 414, 555, 62, 321]Output: 596 ( i.e. 213+62+321 )

View Article



Answer by Josephine for How would I find all sets of N single-digit,...

Let's write f(30,5,1) for the answer to your problem. The 30 indicates the desired sum, the 5 indicates the number of digits which should add to the desired sum, and the 1 indicates the minimal...

View Article

Answer by dreeves for How would I find all sets of N single-digit,...

I believe this is known as the Subset Sum problem:http://mathworld.wolfram.com/SubsetSumProblem.html

View Article

Answer by user187291 for How would I find all sets of N single-digit,...

Using Combinations code from hereforeach(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]...

View Article

Answer by Matthew for How would I find all sets of N single-digit,...

This is probably sufficiently fast:<?php$digitCount = 5;$sum = 30;function getAnswer($b){ $a = ""; $i = 1; while ($b) { if ($b & 1) $a .= "$i "; $b >>= 1;++$i; } return $a;}for ($b = 0; $b...

View Article


Answer by artlung for How would I find all sets of N single-digit,...

function sumOfDigits($num) { $str = "{$num}"; $sum = 0; for ($i=0;$i<strlen($str);$i++) { $sum += (int)substr($str, $i, 1); } return $sum;}function hasDuplicateDigits($num) { $str = "{$num}";...

View Article

Answer by codaddict for How would I find all sets of N single-digit,...

A naive approach would be to increment a variable from 12345 till 98765 and to select it only if it has unique digits and sum of digits is 30:for($i=12345;$i<98765;$i++) { $arr =...

View Article

Answer by David Z for How would I find all sets of N single-digit,...

I know there are algorithms for this, and they will probably be provided by other people, but here's one quick simplification you can make: look for all sets of 4 single digits that add up to 21-29...

View Article


How would I find all sets of N single-digit, non-repeating numbers that add...

Let's say I want to find all sets of 5 single-digit, non-repeating numbers that add up to 30... I'd end up with [9,8,7,5,1], [9,8,7,4,2], [9,8,6,4,3], [9,8,6,5,2], [9,7,6,5,3], and [8,7,6,5,4]. Each of...

View Article

Browsing all 9 articles
Browse latest View live




Latest Images