1
$\begingroup$

For the linear equation

x[1] + x[2] + x[3] + x[4] + x[5] == n

Is there any efficient way to find all the non-negative integer solutions of this equation when n is large?

$\endgroup$

2 Answers 2

5
$\begingroup$
FrobeniusSolve[ConstantArray[1, #], #2] &[5,4]

Mathematica graphics

For large n it would be better to use IntegerPartitions to avoid solutions that are identical up to permutation. Using the second and third arguments, IntegerPartitions[#, {5}, Range[0, #]] gives all the solutions {x[1],x[2],x[3],x[4],x[5]} with the structure x[1] >= x[2] >= x[3] >= x[4] >= x[5]

IntegerPartitions[#, {5}, Range[0, #]] &[12]

Mathematica graphics

$\endgroup$
1
  • $\begingroup$ @Wenzhe, my pleasure. Welcome to mma.se. $\endgroup$
    – kglr
    Commented May 10, 2016 at 2:45
2
$\begingroup$

Try this:

PadLeft[IntegerPartitions[n]]
$\endgroup$
1
  • $\begingroup$ PadLeft[IntegerPartitions[12, 5]] $\endgroup$
    – Bob Hanlon
    Commented May 10, 2016 at 3:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.