Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing and Restoring Variations in a Long

Tags:

c#

.net

math

logic

I have n-values within the same fix minimum and maximum range.

// value range
int minValue = 0;
int maxValue = 100;
int valueCount = 3; // number of values

// Example 1
int a = 0;
int b = 0;
int c = 0;

// Example 2
int a = 100;
int b = 100;
int c = 100;

// Example 3
int a = 12;
int b = 80;
int c = 27;

Example 1 and 2 showing the minimum and maximum variation of these 3 values, where Example 3 shows some random values.

The possible number of all variations for these 3 values with the given minimum and maximum value is 100 * 100 * 100 = 1.000.000.

So if i define that 0 stands for the variation of Example 1 and that 1.000.000 stands for the variation of example 2. How do i calculate a value that stands for Example 3?

Or better, how do i loop thru all possible variations and how to restore them.

for(int i = 0; i <= 1000000; i++)
{
    int[] values = GetValues(i, minValue, maxValue, valueCount);
}

So what i'm looking for is something that works like the GetValues() methode above.

like image 924
endeffects Avatar asked Nov 24 '25 14:11

endeffects


1 Answers

It's just like finding the indexes for a n-dimensional array when you know the offset of memory for that cell.

public static IEnumerable<int> GetValues(long i, int minValue, int maxValue, int valueCount)
{
    var range = maxValue - minValue + 1;

    for (int j = 0; j < valueCount; j++)
    {
        yield return (int)(i % range + minValue);
        i = i / range;
    }
}

As I mentioned in the comment the example you provided is wrong.

There are 1030301 number of variation, because each item can have 101 different value (from 0 to 100) and total number of variations is 101*101*101 = 11030301.

like image 158
Hamid Pourjam Avatar answered Nov 27 '25 04:11

Hamid Pourjam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!