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 codaddict 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

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 = preg_split('//',strval($i));    if(count(array_unique($arr)) == count($arr) && array_sum($arr) == 30)        echo $i."\n";}

Working example


Viewing all articles
Browse latest Browse all 9

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>