mercredi 6 mai 2015

breaking number into sum of other numbers

Your task is to design and implement a function in C#/Java for finding random representations of a natural number N as a list of natural numbers {A1, A2, …, An} such that: N=∑_(i=1)^n▒(A_i ) The prototype of the function should be: List GetRandomList( int N // the number N to break int NumberOfItems, // the number of items to break N into int Low, // the minimal number in the list int High, // the maximal number in the list ); Assuming that there are more than one possible outputs for a given set of parameters, the function should randomly return one with an equal chance to return each possible set on different calls. To achieve that you can use the library function Rand() which has the following prototype: int Rand(int Low, int High); // would return an integer between Low and High (inclusive).

Example: Calling GetRandomList(8, 3, 1, 5) enough times would eventually produce all the following sets: {1 , 2, 5}, {2, 2, 4} , {2, 3, 3}, {1, 3, 4} -- or any internal permutation of these sets. Please aim for a minimal complexity solution.

please help me create an algorithem which is simple but effective. i need it for work. TY

Aucun commentaire:

Enregistrer un commentaire