Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static List<string> FindCombination(int range, int searchValue)
- {
- List<string> output = new List<string>();
- loop(1,1,"1");
- return output;
- void loop(long value, int iteration, string takenPath)
- {
- if (iteration == range && value == searchValue) //Escape from recursion when we reach the final number (itteration)
- {
- output.Add($"{takenPath} = {value}");
- }
- else if (iteration < range)
- {
- iteration += 1; //the next number
- loop(value + iteration, iteration, takenPath + " + " + iteration); //add the number
- loop(value - iteration, iteration, takenPath + " - " + iteration); //subtract the number
- //adds the number to the end of the current number, example: 1 and 2 => 12, 4 and 5 => 45
- if (value > 0)
- loop(value * 10 + iteration, iteration, takenPath + iteration.ToString());
- else
- loop(value * 10 - iteration, iteration, takenPath + iteration.ToString());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement