How would I find all sets of N single-digit, non-repeating numbers that add up to a given sum in PHP
Posted
by TerranRich
on Stack Overflow
See other posts from Stack Overflow
or by TerranRich
Published on 2010-05-04T01:46:25Z
Indexed on
2010/05/04
1:58 UTC
Read the original article
Hit count: 274
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 those sets contains 5 non-repeating digits that add up to 30, the given sum.
Any help would be greatly appreciated. Even just a starting point for me to use would be awesome.
I came up with one method, which seems like a long way of going about it: get all unique 5-digit numbers (12345, 12346, 12347, etc.), add up the digits, and see if it equals the given sum (e.g. 30). If it does, add it to the list of possible matching sets.
I'm doing this for a personal project, which will help me in solving Kakuro puzzles without actually solving the whole thing at once. Yeah, it may be cheating, but it's... it's not THAT bad... :P
© Stack Overflow or respective owner