# AS2: How to iterate X times through a percentage calculation (containing a circular reference)?

Here is a question for the Excel / math-wizards.

I'm having trouble doing a calculation which is based on a formula with a circular reference. The calculation has been done in an Excel worksheet.

I've deducted the following equations from an Excel file:

a = 240000

b = 1400 + c + 850 + 2995

c = CEIL( ( a + b ) * 0.015, 100 )

After the iterations the total of A+B is supposed to be 249045 (where b = 9045).

In the Excel file this gives a circular reference, which is set to be allowed to iterate 4 times.

My problem: Recreate the calculation in AS2, going through 4 iterations.

I am not good enough at math to break this problem down.

Can anyone out there help me?

Edit: I've changed the formatting of the number in variable a. Sorry, I'm from DK and we use period as a thousand separator. I've removed it to avoid confusion :-)

2nd edit: The third equation, C uses Excels CEIL() function to round the number to nearest hundredth.

-
Could you give more details as to what you are trying to calculate? How do you know that the final value of b is 9045? –  Benjamin Mar 24 '11 at 17:06

I don't know action script, but I think you want:

``````a = 240000
c = 0
for (i = 0; i < 4; i++){
b = 1400 + c + 850 + 2995
c = (a + b) * 0.015
}
``````

But you need to determine what to use for the initial value of c. I assume that Excel uses 0, since I get the same value when running the above as I get in Excel with iterations = 4, c = 3734.69...

Where do you get the "A + B is supposed to be 249045" value? In Excel and in the above AS, b only reaches 8979 with those values.

-
Thanks for the help! I was a bit unprecise in my question. I've edited my original question to reflect the actual problem & solution, which I found, thanks to Benjamin. –  carlskov Mar 30 '11 at 10:42
``````function calcRegistrationTax( amount, iterations ) {

function roundToWhole( n, to ) {

if( n > 0 )
return Math.ceil( n/ to ) * to;
else if( n < 0)
return Math.floor( n/ to ) * to;
else
}

var a = amount;
var b = 0;
var c = 0
for (var i = 0; i < iterations; i++){
b = basicCost + ( c ) + financeDeclaration + handlingFee;
c = ( a + b ) * basicFeeRatio;
c = roundToWhole( c, 100 );
}

return b;
}

totalAmount = 240000 + calcRegistrationTax( 240000, 4 ); // This gives 249045
``````

This did it, thanks to Benjamin for the help.

-
{"found_math": false, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0}