Jump to content

Recommended Posts

I have a calculation that is working, but it is so long and I am just wondering if there is a better way. Basically, the calculation take a yearly bill and a given inflation rate and calculates how much is spent over 5, 15 and 25 year periods.

 

Here is my code:

$inflrate = 1.05; //5% inflation

//calculate yearly bill
$yearlybill = $avg_bill * 12; //total estimated yearly cost

$cost5 = $yearlybill + $yearlybill * $inflrate + $yearlybill * pow($inflrate,2) + $yearlybill * pow($inflrate,3) + $yearlybill * pow($inflrate,4);

$cost15 = $yearlybill + $yearlybill * $inflrate + $yearlybill * pow($inflrate,2) + $yearlybill * pow($inflrate,3) + $yearlybill * pow($inflrate,4) + $yearlybill * pow($inflrate,5) + $yearlybill * pow($inflrate,6) + $yearlybill * pow($inflrate,7) + $yearlybill * pow($inflrate, + $yearlybill * pow($inflrate,9) + $yearlybill * pow($inflrate,10) + $yearlybill * pow($inflrate,11) + $yearlybill * pow($inflrate,12) + $yearlybill * pow($inflrate,13) + $yearlybill * pow($inflrate,14);

$cost25 = $yearlybill + $yearlybill * $inflrate + $yearlybill * pow($inflrate,2) + $yearlybill * pow($inflrate,3) + $yearlybill * pow($inflrate,4) + $yearlybill * pow($inflrate,5) + $yearlybill * pow($inflrate,6) + $yearlybill * pow($inflrate,7) + $yearlybill * pow($inflrate, + $yearlybill * pow($inflrate,9) + $yearlybill * pow($inflrate,10) + $yearlybill * pow($inflrate,11) + $yearlybill * pow($inflrate,12) + $yearlybill * pow($inflrate,13) + $yearlybill * pow($inflrate,14) + $yearlybill * pow($inflrate,15) + $yearlybill * pow($inflrate,16) + $yearlybill * pow($inflrate,17) + $yearlybill * pow($inflrate,18) + $yearlybill * pow($inflrate,19) + $yearlybill * pow($inflrate,20) + $yearlybill * pow($inflrate,21) + $yearlybill * pow($inflrate,22) + $yearlybill * pow($inflrate,23) + $yearlybill * pow($inflrate,24);

 

So basically $cost5 would return the total amount the person would pay in 5 years with the same inflation rate (which would compound every year).

 

So, my question is, is there some function that i dont know about that would cut this code down to a single line? I want to get the total cost for 5, 10, 15, 20 and 25 years, but i dont want 10 pages of repeating code like this.

 

Also, if there is any way I could have a function where i could simply input the number of years and get a result, that would be ideal.

 

Thanks

 

 

You would want to look up what exponential growth is. Generally speaking, when something grows with a specific percentage then it's growing exponentially, because it will grow "more" each time. That's opposed to grow with a constant factor, which would then be linear.

<?php
$inflrate = 1.05; //5% inflation

//calculate yearly bill
$yearlybill = $avg_bill * 12; //total estimated yearly cost

$cost5 = $inflrate != 1 ? $yearlybill * (pow($inflrate,5) - 1) / ($inflrate - 1) : $yearlybill * 5;

$cost15 = $inflrate != 1 ? $yearlybill * (pow($inflrate,15) - 1) / ($inflrate - 1) : $yearlybill * 15;

$cost25 = $inflrate != 1 ? $yearlybill * (pow($inflrate,25) - 1) / ($inflrate - 1) : $yearlybill * 25;
?>

Thanks again Sasa! that worked great, give me the same answer but so much less code.

 

@DarkWater your equation is partially right P = 5000(1 + .05)^t will give you the total that was spent that year (t). As I mentioned in the question this is a compounding interest equation, so if you want to get how much was spent over 5 years this is the equation (mathematically):

P = 5000 + 5000(1 + .05) + 5000(1 + .05)^2 + 5000(1 + .05)^3 + 5000(1 + .05)^4

 

1st year = 5000 (not counting inflation)

2nd year = 5000 * 1.05

3rd year = 5000(1.05)^2

4th year = 5000(1.05)^3

5th year = 5000(1.05)^4

 

Anyway, Im sure you already know this stuff, but just in case someone is searching forums in need of an answer this might help.

 

Peace guys and thanks allot for the help!!!

 

 

I know you already have an answer, but I think I just worked out a slightly shorter way to get it.

 

Let's say that A is $yearlybill, r is $inflrate, n is the number of years and P is the cost

 

[tex]P = \displaystyle\sum_{i=0}^n A\left(r\right)^i[/tex]

 

We can move the A out:

[tex]P = A\displaystyle\sum_{i=0}^n r^i[/tex]

 

And then we can simplify the sum to:

[tex]P = A\left(\frac{1-r^{n+1}}{1-r}\right)[/tex]

 

So basically:

[tex]P = 5000\left(\frac{1-1.05^6}{1-1.05}\right)[/tex]

 

I got 34009.5640625. Is that the number you get with your code? If it is, then this expression is very easy to put into PHP.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.