c# - find numbers that can be formed from sum of numbers from array in ascending order -


i trying find possible values result of sum of values of given array. example if given array a = [50,100,120,260,360] result [0,50,100,120,150,170,200,220,240,250,260,....]. how implement ?

i found 1 article realted find value can't formed using given array.

find smallest number can't formed values of given array

i found 1 more discussion related mathematics , still unable understand how implement it. can have @ find possible values can formed using values

any algorithm or code in c# help.

edit

we can use single value many times.

more results 270 (50*1 + 100*1 + 120) , 300 (100*3), 310 (50 * 1 + 260 *1) etc.

this use:

func<ienumerable<int>, ienumerable<ienumerable<int>>> getallsubsets = null; getallsubsets = xs =>     (xs == null || !xs.any())         ? enumerable.empty<ienumerable<int>>()         : xs.skip(1).any()             ? getallsubsets(xs.skip(1))                 .selectmany(ys => new[] { ys, xs.take(1).concat(ys) })             : new[] { enumerable.empty<int>(), xs.take(1) }; 

then can this:

var = new [] { 50, 100, 120, 260, 360 };  console.writeline(string.join(", ", getallsubsets(a).select(x => x.sum()).orderby(x => x))); 

i this:

0, 50, 100, 120, 150, 170, 220, 260, 270, 310, 360, 360, 380, 410, 410, 430, 460, 480, 480, 510, 530, 530, 580, 620, 630, 670, 720, 740, 770, 790, 840, 890

knowing values can repeated way go:

public ienumerable<int> generateallsums(int[] array) {     var buffer = new linkedlist<int>();     buffer.addfirst(0);     while (true)     {         var first = buffer.first;         var nexts = array.select(a => first.value + a);         foreach (var next in nexts)         {             var x = buffer.first;             while (x.value < next)             {                 x = x.next;                 if (x == null)                 {                     break;                 }             }             if (x == null)             {                 buffer.addlast(next);             }             else if (x.value != next)             {                 buffer.addbefore(x, next);             }         }         buffer.removefirst();         yield return first.value;     } } 

i can call so:

var = new [] { 50, 100, 120, 260, 360, };  console.writeline(string.join(", ", generateallsums(a).take(100))); 

it's important note .take(...) vital sequence infinite.

given .take(100) result:

0, 50, 100, 120, 150, 170, 200, 220, 240, 250, 260, 270, 290, 300, 310, 320, 340, 350, 360, 370, 380, 390, 400, 410, 420, 430, 440, 450, 460, 470, 480, 490, 500, 510, 520, 530, 540, 550, 560, 570, 580, 590, 600, 610, 620, 630, 640, 650, 660, 670, 680, 690, 700, 710, 720, 730, 740, 750, 760, 770, 780, 790, 800, 810, 820, 830, 840, 850, 860, 870, 880, 890, 900, 910, 920, 930, 940, 950, 960, 970, 980, 990, 1000, 1010, 1020, 1030, 1040, 1050, 1060, 1070, 1080, 1090, 1100, 1110, 1120, 1130, 1140, 1150, 1160, 1170


Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -