Home > front end >  Formula to align numbers in blocks of 128
Formula to align numbers in blocks of 128

Time:09-07

I would like to know the formula to align using this pattern

From 2088 To 2175 = 128
From 2176 To 2263 = 256
From 2264 To 2351 = 384
From 2352 To 2439 = 512
From 2440 To 2527 = 640
From 2528 To 2615 = 768

Each block of 88 increases the final result in 128, what would be the formula to do this?

CodePudding user response:

You have a linear function, in floating point case you can reconstruct it by two points:

 y = (x * 16 - 32000) / 11

If you want integer steps 128, 256, 384... you can do it by dividing and multiplying by 128

 y = (x * 16 - 32000) / 11 / 128 * 128

Code:

static int Compute(int x) => (x - 2000) / 88 * 128;

Demo:

var tests = new (int from, int to)[] {
  (2088, 2175),
  (2176, 2263),
  (2264, 2351),
  (2352, 2439),
  (2440, 2527),
  (2528, 2615),
};

string report = string.Join(Environment.NewLine, tests
  .Select(test => $"From {test.from} To {test.to} = [{Compute(test.from)} .. {Compute(test.to)}]")
);

Console.Write(report);

Output:

From 2088 To 2175 = [128 .. 128]
From 2176 To 2263 = [256 .. 256]
From 2264 To 2351 = [384 .. 384]
From 2352 To 2439 = [512 .. 512]
From 2440 To 2527 = [640 .. 640]
From 2528 To 2615 = [768 .. 768]
  • Related