Register
It is currently Thu Feb 22, 2018 10:37 am

compound.pl an example in perl


All times are UTC


Post new topic Reply to topic  [ 3 posts ] 
Author Message
 PostPosted: Fri Feb 04, 2011 10:57 pm   
Site Admin
User avatar

Joined: Thu Apr 17, 2003 3:44 am
Posts: 12300
Location: Iowa
Just a quick perl script that can show how fast things can compound.
Decided to write it in perl, as I didn't want to go figure out bc man pages again. lol.

In this example, say you started with $1,000 ... and you get paid every 2 weeks.
Every 2 weeks you add $50 to your stash... and you expect to make at least 1% on
your money every 2 weeks (i don't care how...and don't want to know) lol.

This script would tell you how much money you would have at the end of a year based
on that input.

Code:
#!/usr/bin/perl
# compounding.pl
use strict;
use warnings;

my $x = 1000.00; #Set this to any number
my $i = 0;
                           #Set the 24 to the number you want
    for ($i = 1; $i <= 24; $i++) {
    $x = $x * 1.01; $x = $x + 50.00; #add in another number in the middle of the process if needed.
    print "Loop $i Value=$x\n";
    }


Perl made the calculations very easy to do...

Code:
./compounding.pl
Loop 1 Value=1060
Loop 2 Value=1120.6
Loop 3 Value=1181.806
Loop 4 Value=1243.62406
Loop 5 Value=1306.0603006
Loop 6 Value=1369.120903606
Loop 7 Value=1432.81211264206
Loop 8 Value=1497.14023376848
Loop 9 Value=1562.11163610617
Loop 10 Value=1627.73275246723
Loop 11 Value=1694.0100799919
Loop 12 Value=1760.95018079182
Loop 13 Value=1828.55968259974
Loop 14 Value=1896.84527942573
Loop 15 Value=1965.81373221999
Loop 16 Value=2035.47186954219
Loop 17 Value=2105.82658823761
Loop 18 Value=2176.88485411999
Loop 19 Value=2248.65370266119
Loop 20 Value=2321.1402396878
Loop 21 Value=2394.35164208468
Loop 22 Value=2468.29515850553
Loop 23 Value=2542.97811009058
Loop 24 Value=2618.40789119149


Nothing special, just thought it was interesting how easy it was to do math in perl.

_________________

Zbook (Laptop)- Mint 18 64-bit - I7-4800mq - 32GB ram
Phantom (Desktop)- Mint 64-bit - i7-5770 - 32GB ram
Nas4Free (NAS) - 6X3TB File Server.
Sky (HP BladeCenter) - 16 blades - 536 GB ram-G7/G6/G5 blades.


Top
 Profile WWW  
 PostPosted: Fri Feb 04, 2011 11:54 pm   
Sr. Member

Joined: Sun Dec 12, 2004 6:08 am
Posts: 1200
Location: Cupertino, CA
"Rule of 72" (or 70 for more simplicity) for doubling period is the easiest way to do this in your head IMO.

~7% (annual) growth for 10 years = you double your money
~3.5% growth for 20 years = you double your money

And not to nit-pick, but there are 26 pay-periods per year if you are paid every 2 weeks (bi-weekly) - not 24 (that's bi-monthly) :)


Top
 Profile  
 PostPosted: Sun Feb 06, 2011 4:32 pm   
Site Admin
User avatar

Joined: Thu Apr 17, 2003 3:44 am
Posts: 12300
Location: Iowa
If you can do compounding in your head 26 times while adding a number in there as well every time... your way better at math than I am ;)

I've known about the rule of 72 for ... well, lets just say a very long time lol.
Quote:
The rule of 72 in plain English is this: How many years does it take to double my money at interest rate X.
The formula is 72 divided by X = years it takes to double your money.
So, if I make 12% on my money... 72 divided by 12 is 6 years... it will take approx 6 years to double my money at 12%.

70 may be technically closer as a starting base, but 72 has more divisors, making it much quicker to do in your head.


And not to nit-pick (you started it) 26.07 pay periods. except Once every 11 years 27 pay dates will occur in one calendar year. :P :D :D :D (I actually had to look up the payperiod/paydates information, I knew it would be slightly different for leap years.)


I wrote another perl script to show how long it takes to double your money at X interest rate.

Code:
[ --optimus-->  ~/Perl
[davec] cat double.pl
#!/usr/bin/perl
# compounding.pl
use strict;
use warnings;

my $x = 1000.00; # Set this to any number .. the amount your starting with
my $y = 2 * $x;  # Double $x ... see how many years it takes to reach this amount
my $z = 1.035 ;    # This is the percentage rate. (thats 3.5%)

my $i = 0;
    #Lets find out how many years it takes to double our money.
    for ($i = 1; $x <= $y; $i++){
    $x = $x * $z;
    print "Loop $i Value=$x\n";
    }


Today is Sun Feb 06 10:30:23
[ --optimus-->  ~/Perl
[davec] ./double.pl
Loop 1 Value=1035
Loop 2 Value=1071.225
Loop 3 Value=1108.717875
Loop 4 Value=1147.523000625
Loop 5 Value=1187.68630564687
Loop 6 Value=1229.25532634452
Loop 7 Value=1272.27926276657
Loop 8 Value=1316.8090369634
Loop 9 Value=1362.89735325712
Loop 10 Value=1410.59876062112
Loop 11 Value=1459.96971724286
Loop 12 Value=1511.06865734636
Loop 13 Value=1563.95606035348
Loop 14 Value=1618.69452246585
Loop 15 Value=1675.34883075216
Loop 16 Value=1733.98603982848
Loop 17 Value=1794.67555122248
Loop 18 Value=1857.48919551527
Loop 19 Value=1922.5013173583
Loop 20 Value=1989.78886346584
Loop 21 Value=2059.43147368715


Today is Sun Feb 06 10:30:34
[ --optimus-->  ~/Perl
[davec]

_________________

Zbook (Laptop)- Mint 18 64-bit - I7-4800mq - 32GB ram
Phantom (Desktop)- Mint 64-bit - i7-5770 - 32GB ram
Nas4Free (NAS) - 6X3TB File Server.
Sky (HP BladeCenter) - 16 blades - 536 GB ram-G7/G6/G5 blades.


Top
 Profile WWW  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


Powered by phpBB © 2012 phpBB Group
© 2003 - 2012 USA LINUX USERS GROUP