Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4339528
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T11:13:42+00:00 2026-05-21T11:13:42+00:00

I am given 100 dollars and told that I have to do the following

  • 0

I am given 100 dollars and told that I have to do the following operations on it in order:

Add 97
Add 5
Multiply by a compounded interest of 3%

So essentially, it becomes (100 + 97 + 5) * 1.3 = 208.06

Now here lies my problem, I know to get back to 100 from 208.06, I must do:

208.06 / 1.3 – (97 + 5) = 100

The above is a simple example, but as soon as I start mixing compound,non-compound, flat amounts, and the ability to not only add, but subtract the values, it gets really complicated. For example, if I am given 100 dollars again and told that I have to apply the following adjustments in order, I am not sure how to reverse it:

1) Add a compound 3%
2) Add a compound 4%
3) Subtract 90
4) Subtract 3
5) Add a non-compound 3%
6) Add a non-compound 4%

The calculation for the above is:

((100 * (1.03) * (1.04)) – 90 – 3) * (1 + 0.04 + 0.03)

For the above, I have no idea how to approach reversing it with just knowing the post adjusted value and the 6 adjustments that were added.

The above formula can be simplified to, which makes it easier to see how to reverse it:

(100 * 1.03 * (1.04)) - 93 + 7 = 21.21 and to reverse it, we just do:

(100 * 1.03 * (1.04)) - 93 + 7 = 100

My problem is how to loop through it and apply the operations in the correct order. Do I have to store the order of operations and their amounts in a class and then foreach through each calculation?

I have posted this on Math.StackExchange.com already, but they look at it as one big formula and don’t understand that from a programming perspective, I need to loop through each adjustment and take it off the grand total. This is essentially what I am stuck at, but I am unclear how to approach solving it by looping through each adjustment since it is important that I apply the adjustments in the correct order. I am not really looking for a solution, but more of a point in the right direction.

Here is an implementation. Applying it seems to work with the initial sample, but if I do my other one, it does not seem to work unless I am doing something wrong:

void Main()
{
var ops = new List<Operation>
{

new CompoundInterest(3.0m),
new CompoundInterest(4.0m),
new Subtract(90.0m),
new Subtract(3.0m),
new NoncompoundInterest(3.0m),
new NoncompoundInterest(4.0m)
};

var applied = ops.Aggregate(100m, (acc, o) => o.Apply(acc));

var unapplied = ops.Reverse().Aggregate(applied, (acc, o) => o.Unapply(acc));

}

// Define other methods and classes here
abstract class Operation
{
public abstract decimal Apply(decimal value);
public abstract decimal Unapply(decimal value);
}
class CompoundInterest : Operation
{
public decimal percent {get;set;}
public CompoundInterest(decimal percent)
{
    this.percent = percent;
}

public override decimal Apply(decimal value)
{
    return value * (1m + percent/100m);
}

public override decimal Unapply(decimal value)
{

    return value / (1m + percent/100m);
}
}
class NoncompoundInterest : Operation { 
public decimal percent {get;set;}
public NoncompoundInterest(decimal percent)
{
    this.percent = percent;
}


public override decimal Apply(decimal value)
{
    return value * (percent/100m);
}

public override decimal Unapply(decimal value)
{
    return value / (percent/100m);
}
}

class Add : Operation { 
public decimal amount {get;set;}
public Add(decimal amount)
{
    this.amount = amount;
}


public override decimal Apply(decimal value)
{
    return value + amount;
}

public override decimal Unapply(decimal value)
{
    return value - amount;
}

}

class Subtract : Operation { 
public decimal amount {get;set;}
public Subtract(decimal amount)
{
    this.amount = amount;
}


public override decimal Apply(decimal value)
{
    return value - amount;
}

public override decimal Unapply(decimal value)
{
    return value + amount;
}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T11:13:42+00:00Added an answer on May 21, 2026 at 11:13 am

    One way to think of this would be as a list of operations, with each operation also providing details how to undo itself. Roughly…

    abstract class Operation
    {
        public abstract decimal Apply(decimal value);
        public abstract decimal Unapply(decimal value);
    }
    
    class CompoundInterest : Operation
    {
        public CompoundInterest(decimal percent)
        {
            this.percent = percent;
        }
    
        public override decimal Apply(decimal value)
        {
            return value * (1m + percent/100m);
        }
    
        public override decimal Unapply(decimal value)
        {
            return value / (1m + percent/100m);
        }
    }
    
    class NoncompoundInterest : Operation { ... }
    
    class Add : Operation { ... }
    
    class Subtract : Operation { ... }
    

    Now you can represent your chain of operations in a list:

    var ops = new List<Operation>
    {
        new Add(97m),
        new Add(5m),
        new CompoundInterest(3.0m),
    };
    
    var applied = ops.Aggregate(100m, (acc, o) => o.Apply(acc));
    var unapplied = ops.Reverse().Aggregate(208.06m, (acc, o) => o.Unapply(acc));
    

    Update: Aggregate() works something like like this:

    decimal acc = 100m;
    foreach(Operation o in ops)
        acc = o.Apply(acc);
    decimal applied = acc;
    

    (acc, o) => o.Apply(acc) is a lambda expression representing a function that returns o.Apply(acc) given the current “accumulator” (acc) and the current item in the sequence (o). The “seed” value (100m) is used as the first acc, then the function’s return value is then passed in as acc for the next o, and so on.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a horizontal bar that has been given a width of 100% .
I have given 100% width to input box to fix to the browser width
Given a large table (10-100 million rows) what's the best way to add some
Given a table of the following form CustumerID | Amount ------------------- 1 | 100
I have around 100 models, from MCMCglmm, that give output similar to this: >
I have some temperature values in a given range, lets say between 0-100 Celsius.
Given the following string arrray: string[] ranges = new string[]{0-100, 100-200, 500-1000}; I would
Given the following program, #include <iostream> using namespace std; void foo( char a[100] )
Given that I have a table contains user data, like this: userID calltime result
I have an array given as: $my_fontSizes = array( => 100% Default, 150% =>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.