php - Generate all possible 3 positive integers that sum to N -


i not math , can't wrap head around this, want generate every possible 3 positive numbers there sum n example 100, instance:

0 - 0 - 100

0 - 1 - 99

1 - 1 - 98

you don't need answer me php code, general idea on how can generate numbers sufficient.

thanks.

brute force option in case: can use 2 nested loops , takes less 10000 tests only.

  // pseudo code   (i = 0; <= 100; ++i)     (j = 0; j <= 100; ++j) {       if ((i + j) > 100)         break;        k = 100 - - j;        print(i, j, k);      }  

if duplicates e.g. 0, 0, 100 , 0, 100, 0 should excluded, can use modified code:

  // pseudo code   (i = 0; <= 100; ++i)     (j = i; j <= 100; ++j) {       if ((i + j) > 100)         break;        k = 100 - - j;        if (j <= k)         print(i, j, k);      }  

Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -