Answer the question
In order to leave comments, you need to log in
How to find all possible combinations of numbers from 1 to n-1 so that their sum is equal to n?
Good night) I just can’t figure out how to solve the problem ... there is a number, for example 8, you need to find all the numbers from 1 to 7, the sum of which is 8 without repetition ... i.e.
lots of
options ...
1, 7
2, 6
3, 5
1, 2, 5
1, 3, 4 all options, but it doesn't work very well....
Tell me who solved this, the algorithm for solving the problem, or poke a link))) in Google, as I have not tried to ask a question, it usually gives out a search in an array of a pair of numbers whose sum is equal to n, but I have no limit on how many numbers should be ... maybe 2, or maybe 8 or maybe some more ... you need to somehow go through the array and throw it in until you get the right amount or bust, then roll back and try further, and then I get confused, maybe I’m swarming in the wrong direction ...
Answer the question
In order to leave comments, you need to log in
Option 1 - complete recursive enumeration. There is one recursive function, which is passed the next number, the current sum and the current list of taken numbers. The function either takes the next number or skips it and is called recursively for the next number. If the amount is too large, it is simply returned immediately. If the sum is n - displays the current numbers and returns.
It is possible to store a list of taken numbers in a global array/list. But you have to carefully roll it back. Before the recursive call, if a new number has been added, then after calling it, remove it from the array / list. So the memory consumption will be O(n) instead of O(n^2).
Something like this:
void Generate(int n, int next, int sum, vector<int> taken) {
if (next > n || sum > n) return;
if (sum == n) {
Output(taken);
}
Generate(n, next+1, sum, taken);
taken.push_back(next);
Generate(n, next+1, sum+next, taken);
}
...
Generate(n, 1, 0, {});
If you have a pairing algorithm, just apply it recursively to the elements of the pairs.
works on small ones, there is no way to check on large ones
function* genz( n ){
for ( let i = n-1; i > 1; i--){
let a = n - i;
if (a < i) yield [i,a]
for (let d of [...genz(a)])
if (!d.some(value => value >= a || value >= i)) yield [i, ...d]
}
}
//pretty print
console.log([...genz(13)].map(v=>v.join(' + ')).join('\r\n'), '\r\n');
This option seems to work:
package main
import "fmt"
func sum(s []int) int {
result := 0
for _, v := range s {
result = result + v
}
return result
}
func main() {
n := 10000
var stack []int
p := n - 1
for p > 0 {
stack = append(stack, p)
for j := p - 1; j > 0; j-- {
stack = append(stack, j)
cur := sum(stack)
if cur < n {
continue
} else if cur == n {
fmt.Printf("%++v\n", stack)
}
stack = stack[:len(stack)-1]
}
stack = nil
p--
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question