Pseudocode: computing the total recursively
I have been given a requirement(challenging for me at least) to write a logic in an application. I've to write a business logic wherein it should perform the following functionality
Total current consumption = current from A elements + current from B elements.
A and B are different types of devices
Now lets say the batteries required to supply the current (A+B) be 'X'
Also each X can contribute to the total current consumption, hence i need to calculate the total current consumption again just as first step including the battery current consumption
i.e
`Total current consumed : A + B + X"`
where X" is the current consumption of the battery
Now again i should calculate the batteries required. Let us say this as Y
i.e
to supply A + B + X" we need Y number of batteries.
Now check whether X == Y ?
If same, then return Y and exit
else add more X to the sum (A + B + X") till X =开发者_运维知识库= Y
Can anyone help me with initial set of pseudocode ? Any kind of suggestion is also appreciated
Yes the end result this logic should return is number of batteries required. However it should return this result only after computing the total current consumption recursively till X == Y, where
A : total current consumption of some active elements in a system.
B : total current consumption of some passive elements in a system
Total current consumption is A + B
to supply current of (A+B) amperes i require 'X' no. of batteries.
However each battery also adds some delta amount of current to the total value i.e
A + B + X"
if the batteries required to supply this delta is still 'X', then return X as the end result, else add more batteries --> calculate current --> no of batteries required ---> check again and so on ...
The question isn't very clear (as others noted in comments), so it would be useful if you could write some more concrete or specific example of the calculation. Anyway, it seems to me that you have some calculation with a feedback and you need to reach a point in which the calculation stops changing.
In mathematics, this can be described using a fixed-point. For a given function f (your calculation) the fixpoint is a value such that x = f(x) (meaning that if you recalculate the value again, it will stop changing). I'm not sure if this can help you with your implementation, but it is definitely a useful concept that you can use when thinking about the problem.
Here is an example of a method that calculates a fixed-point of a given function (using C# 3.0 Func<T, T>
delegate). The method is generic and needs to be able to compare the values:
static T FixedPoint<T>(T initial, Func<T, T> calculateNext)
where T : IComparable<T> {
T state = initial;
T previous = default(T);
do {
previous = state;
state = calculateNext(state);
} while (previous.CompareTo(state) != 0);
return state;
}
Wikipedia has an example of calculating fixed-point of a cos function (see the second graph on the right), which you can implement like this:
double val = FixedPoint(-1.0, f => Math.Cos(f));
Console.WriteLine(val);
This is a very general way to describe some looping that runs until it finds a stable point of some calculation. However, your question isn't very clear, so this may not be what you are looking for...
I would do something along the lines of the following:
double CurrentFromEachBattery=100.0;
double CurrentNeededPerBattery=10.0;
int NumberOfBatteriesRequired(double activeCurrent, double passiveCurrent)
{
int batteries=0;
double currCurrent=0.0;
double neededCurrent=activeCurrent+passiveCurrent;
while( currCurrent < neededCurrent )
{
int newBatt = Math.Ceiling((neededCurrent - currCurrent) / CurrentFromEachBattery);
neededCurrent += newBatt * CurrentNeededPerBattery;
currCurrent += newBatt * CurrentFromEachBattery;
batteries += newBatt;
}
return batteries;
}
It seems to me that the pseudo-code is already there, just not very clear. But see if this is what you want:
private const decimal CurrentSuppliedPerBattery = 100;
private const decimal CurrentNeededPerBattery = 5;
private int BatteriesNeeded( List<A> As, List<B> Bs) {
decimal currentToSupply = As.Sum( eachA => eachA.Current ) + Bs.Sum( eachB => eachB.Current );
int batteries = 0;
while(currentToSupply > 0)
{
int extraBatteries = Floor(1.0*currentToSupply/CurrentSuppliedPerBattery );
batteries += extraBatteries;
currentToSupply -= extraBatteries*CurrentSuppliedPerBattery;
currentToSupply += extraBatteries*CurrentNeededPerBattery;
}
return batteries ;
}
ps: You can use System.Linq if you need functions to work on lists, like Sum().
精彩评论