This post from my Math Play blog shows the power of compound interest.
Now, I will give two awk scripts. The first one (compound) calculates compound interest on a lump sum. The second one (compound_add) calculates the total return when we deposit regular contributions, as opposed to a lump sum.
First script (compound):
The run:
[514]-> compound
Usage: /usr/bin/nawk principle rate time
[515]-> compound 100 10 4
Amount 100.00 compounded at 1.1000 for 4 yields:
1: 110.0000
2: 121.0000
3: 133.1000
4: 146.4100
The script:
#! /usr/bin/nawk -f
BEGIN {
if (ARGC < 4)
{
printf ("Usage: %s principle rate time\n",ARGV[0])
exit 1
}
principle=ARGV[1]
rate=ARGV[2]/100+1
time=ARGV[3]
printf ("Amount %.2f compounded at %.4f for %d yields: \n\n",principle,rate,time)
for (i=1;i<=time; i++)
{
principle *= rate
printf ("%d: %.4f\n", i, principle)
}
}
Now, we will look at the second script (compound_add):
The run:
[517]-> compound_add
Usage: /usr/bin/nawk principle rate time
[518]-> compound_add 100 10 4
Amount 100.00 added in each time and compounded at 1.1000 for 4 yields:
1: 110.00 (invested: 100)
2: 231.00 (invested: 200)
3: 364.10 (invested: 300)
4: 510.51 (invested: 400)
The script:
#! /usr/bin/nawk -f
BEGIN {
if (ARGC < 4)
{
printf ("Usage: %s principle rate time\n",ARGV[0])
exit 1
}
principle=ARGV[1]
rate=ARGV[2]/100+1
time=ARGV[3]
printf ("Amount %.2f added in each time and compounded at %.4f for %d yields: \n\n",principle,rate,time)
for (i=1;i<=time; i++)
{
total = (principle + total)*rate
invest += principle
printf ("%d: %.2f (invested: %d)\n", i, total, invest)
}
}
Notice that the two scripts are almost identical. The main difference is in the calculation, which is done as the script loops thru the time intervals.
In "compound", we simply keep multiplying principle by 1 + rate/100, to accumulate the value.
In "compound_add", we have to create a new "total" variable, so we can preserve the original principle value, so we can keep adding it in. In this case, we take the accumulation, add in a new principle, and apply 1 + rate/100 to the whole thing.
In the first example, we add in $100, and let it grow at 10% per period for 4 periods.
In the second example, we contribute $100 in each period, for a total of $400 invested. All contributions grow at 10%/period for however many periods that they are in the account.
0 comments:
Post a Comment