-4

Input:

  • Number of digits in the number
  • Sum of digits
  • Range of digits that can be used

Output:

  • All possible numbers where the sum of digits equals the one defined as an input.

Examples:


(num_digits = 4, sum = 12, range = 0-4)

  • 3 4 4 1
  • 4 4 4 0
  • 4 0 4 4
  • 1 4 4 3

(num_digits = 5, sum = 18, range = 0-7)

  • 5 5 5 2 1
  • 1 2 5 5 5
  • 5 5 2 1 5
  • 0 2 2 7 7
  • 7 7 0 2 2
  • 2 2 7 7 0

(num_digits = 3, sum = 20, range = 0-8)

  • 8 8 4
  • 4 8 8
  • 8 4 8
  • 7 7 6
  • 7 8 5

general method (algorithm) is preferred, however, C# can be a language to chose to resolve

regards

tomitheninja
  • 497
  • 2
  • 13
Junipar70
  • 3
  • 4
  • 1
    As a simple starting execise I suggest to think of helpful names for the variables you have. – TaW May 18 '19 at 10:56
  • Do a search for "c# combinations". You first have write code to get the combination and then add test in algorithm to perform a sum. – jdweng May 18 '19 at 13:14

1 Answers1

0

If I were you, I would use recursion, since in 1234 and 1235 sum of 123 is common, so it shouldn't be calculated multiple times.

Your recursive function has two main rules:

  1. The max depth of recursion = number of digits.
  2. The top-level calls the lower level with every possible digit

possible in your case = the range of digits.

Things you need to use in this exercise:

  • an exit condition for the recursion
  • loops in a given range
  • recursive functions
  • Some type of array/list (be aware that they are copied by memory address and not by value. https://stackoverflow.com/a/4347957/8549369)

This should be enough to write the program, but get used to one thing on StackOverflow: Copy an A-Z solution is not solving an algorithmic task.

tomitheninja
  • 497
  • 2
  • 13
  • This is not an answer. – nicomp May 18 '19 at 11:12
  • "Copying an A-Z solution is not solving an algorithmic task." – tomitheninja May 18 '19 at 11:16
  • issue can be resolve by brute force (via multiple loop) but this is not scaleable solution, suppose there is no. of digits are 23, then 23 times LOOP has to write. Awaiting some magic code independent of no. of digits, will work upon user input – Junipar70 May 18 '19 at 11:40