How many ways can you collect the number 40 with three numbers 10 25 15

x = 10

y = 25

z = 15

I understand what you can write

x + z + z is 40

y + z is 40

x + x + x + x is 40

It turns out: in three ways

Tell me how to express it "learn" mathematically.

  • Well, for example #{x,y,z from Z+}:10x+15y+25z=40 if you are interested in a short record. - pavel
  • Please describe in more detail. - Oleg Ovcharenko
  • it is not clear what to do with numbers. Is it only to fold? for example, y + yx will also be 40. - SPrograMMer
  • Just fold. - Oleg Ovcharenko
  • Sorry, but I am not familiar with F # and written here for me is akin to hieroglyphs. You can describe the solution in any of the languages ​​like "c ++, java, lisp scheme or java script". - Oleg Ovcharenko

2 answers 2

And just brute force is no good? http://ideone.com/XcQm0p :

 #include <vector> #include <string> #include <iostream> #include <iomanip> #include <numeric> using namespace std; int Count(const vector<int>& x, int sum) { int count = 0; vector<int> a(x.size(),0); for(;;) { int i = 0; if (inner_product(x.begin(),x.end(),a.begin(),0) == sum) { for(int j = 0; j < x.size(); ++j) { for(int k = 0; k < a[j]; ++k) cout << "+" << x[j]; } cout << " = " << sum << endl; ++count; } do { if (a[i]*x[i] <= sum) { a[i]++; break; } else { a[i] = 0; ++i; } } while( i < a.size()); if (i == a.size()) break; }; return count; } int main(int argc, const char * argv[]) { vector<int> x = { 5, 10, 15, 25 }; cout << Count(x,40) << endl; } 

    I think this is most beautifully solved by recursions:

     var numbers = [10, 15, 25] function get(x, path = '', used = 0) { // переменную used добавил, чтобы не использовать то, что уже прогорело, чтобы не плодить комбинации одного решения. for (var i = used; i < numbers.length; i++) { //Перебираем числа: num = numbers[i] //можно отнять - отнимем и начнем заново: if (num < x) get(x - num, path + ' ' + num, i); //Остаток равен нулю - путь хорош: if (num == x) console.log(path + ' ' + num); if (num > x) return false; } } get(40)