Adding Array Elements

I have a ten element array of integers. I want to sum the elements by groups, therefore, for example, I want to add a value to element 0 with a value in element 1, then with a value in element 2, then 3 and so on to element 9, then add a value in element 1 with a value from 2 , 3 to 9, until each group of 2 values ​​is combined and stored in a variable. Then I want to repeat the process with groups of 3, groups of 4, of 5, up to group 10. Each final result is stored in a separate variable. So far, the only way to understand how to do this is as follows: -

int i = 0;
int p = 1;
int q = 2;
int r = 3;

while (i < NumArray.Length - 3)
{
    while (p < NumArray.Length - 2)
    {
        while (q < NumArray.Length-1)
        {
            while (r < NumArray.Length)
            {
                foursRet += NumArray[i] + NumArray[p] + NumArray[q]+ NumArray[r];
                r++; 
            }
            q++;
            r = q + 1;
        }
        p++;
        q = p + 1;
        r = q + 1;
    }
    i++;
    p = i + 1;
    q = i + 2;
    r = i + 3;
}

4. , - , , . .

+5
2

LINQ *:

using System; // Output is below
using System.Linq;
using System.Diagnostics;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var inputArray = Enumerable.Range(0, 10).ToArray();

            var grouped =
                from Buckets in Enumerable.Range(1, inputArray.Length)
                from IndexInBucket in Enumerable.Range(0, inputArray.Length / Buckets)
                let StartPosInOriginalArray = IndexInBucket * Buckets
                select new
                {
                    BucketSize = Buckets,
                    BucketIndex = StartPosInOriginalArray,
                    Sum = inputArray.Skip(StartPosInOriginalArray).Take(Buckets).Sum()
                };

            foreach (var group in grouped)
            {
                Debug.Print(group.ToString());
            }

            Console.ReadKey();
        }
    }
} // SCROLL FOR OUTPUT

{ BucketSize = 1, BucketIndex = 0, Sum = 1 }
{ BucketSize = 1, BucketIndex = 1, Sum = 2 }
{ BucketSize = 1, BucketIndex = 2, Sum = 3 }
{ BucketSize = 1, BucketIndex = 3, Sum = 4 }
{ BucketSize = 1, BucketIndex = 4, Sum = 5 }
{ BucketSize = 1, BucketIndex = 5, Sum = 6 }
{ BucketSize = 1, BucketIndex = 6, Sum = 7 }
{ BucketSize = 1, BucketIndex = 7, Sum = 8 }
{ BucketSize = 1, BucketIndex = 8, Sum = 9 }
{ BucketSize = 1, BucketIndex = 9, Sum = 10 }
{ BucketSize = 2, BucketIndex = 0, Sum = 3 }
{ BucketSize = 2, BucketIndex = 2, Sum = 7 }
{ BucketSize = 2, BucketIndex = 4, Sum = 11 }
{ BucketSize = 2, BucketIndex = 6, Sum = 15 }
{ BucketSize = 2, BucketIndex = 8, Sum = 19 }
{ BucketSize = 3, BucketIndex = 0, Sum = 6 }
{ BucketSize = 3, BucketIndex = 3, Sum = 15 }
{ BucketSize = 3, BucketIndex = 6, Sum = 24 }
{ BucketSize = 4, BucketIndex = 0, Sum = 10 }
{ BucketSize = 4, BucketIndex = 4, Sum = 26 }
{ BucketSize = 5, BucketIndex = 0, Sum = 15 }
{ BucketSize = 5, BucketIndex = 5, Sum = 40 }
{ BucketSize = 6, BucketIndex = 0, Sum = 21 }
{ BucketSize = 7, BucketIndex = 0, Sum = 28 }
{ BucketSize = 8, BucketIndex = 0, Sum = 36 }
{ BucketSize = 9, BucketIndex = 0, Sum = 45 }
{ BucketSize = 10, BucketIndex = 0, Sum = 55 }

* LINQ

+4

, n. m. , , .

, n = 6, m = 4 15 ( - ):

0 1 2 3
0 1 2 4
0 1 3 4
0 2 3 4
1 2 3 4
0 1 2 5
0 1 3 5
0 2 3 5
1 2 3 5
0 1 4 5
0 2 4 5
1 2 4 5
0 3 4 5
1 3 4 5
2 3 4 5

n < 32 ( 31 ), 32- . Gosper hack:

IEnumerable<UInt32> GetIndexBits(Int32 m, Int32 n) {
  unchecked {
    var i = (UInt32) (1 << m) - 1;
    var max = (UInt32) (1 << n);;
    while (i < max) {
      yield return i;
      var u = (UInt32) (i & -i);
      var v = u + i;
      i = v + (((v ^ i)/u) >> 2);
    }
  }
}

m = 4 n = 6 ( ):

001111
010111
011011
011101
011110
100111
101011
101101
101110
110011
110101
110110
111001
111010
111100

, LINQ:

var m = 4;
var numbers = new[] { 1, 2, 3, 4, 5, 6 };
var sum = GetIndexBits(4, numbers.Length)
  .Select(
    bits => Enumerable
      .Range(0, numbers.Length)
      .Where(i => ((1 << i) & bits) != 0)
  )
  .Select(indices => indices.Sum(i => numbers[i]))
  .Sum();

210, , foursRet , NumArray 1 6.

+1

All Articles