scarpacci
scarpacci

Reputation: 9194

Efficient way to perform calculation

I am trying to find the best way to handle the below:

  1. I have a List of Decimals
  2. The list is not fixed....meaning I could have 2 one time and 9 another
  3. I want build a new list of decimals using the algorithm below

I am really struggling with the most efficient way to perform this operation. I am really trying to cut down on the amount of times I have to loop over the data. Is there an obvious approach I am just glossing over?

Any suggestions on a possible path forward would be much appreciated.


  new #1 = #1
  new #2 = #2 * (1 - #1)
  new #3 = #3 * (1 - #1) * (1 - #2) 
  new #4 = #4 * (1 - #1) * (1 - #2) * (1 - #3)

Upvotes: 0

Views: 54

Answers (1)

Alexei Levenkov
Alexei Levenkov

Reputation: 100527

In-place replacement:

List<decimal> data =....
decimal multiplier = 1.0;
for (var i = 0; i < data.Count; i++)
{
   var oldMultipleir = multiplier;
   multiplier *= (1 - data[i]);
   data[i] *= oldMultiplier;
}

Upvotes: 5

Related Questions