adambeazley Posted April 3, 2009 Share Posted April 3, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/152470-is-there-an-easier-way-to-do-this-calculation/ Share on other sites More sharing options...
DarkWater Posted April 3, 2009 Share Posted April 3, 2009 Well, if you were doing this on paper, it would be: [tex]P = 5000\left(1 + .05\right)^t[/tex] Where 5000 is the yearly bill and t is the amount of years. Correct? Quote Link to comment https://forums.phpfreaks.com/topic/152470-is-there-an-easier-way-to-do-this-calculation/#findComment-800795 Share on other sites More sharing options...
Daniel0 Posted April 4, 2009 Share Posted April 4, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/152470-is-there-an-easier-way-to-do-this-calculation/#findComment-800864 Share on other sites More sharing options...
DarkWater Posted April 4, 2009 Share Posted April 4, 2009 Yeah. The only reason that I did 1 + .05 instead of 1.05 is because I was originally compounding it monthly for some reason. Didn't read the question that carefully. xD Quote Link to comment https://forums.phpfreaks.com/topic/152470-is-there-an-easier-way-to-do-this-calculation/#findComment-800878 Share on other sites More sharing options...
Daniel0 Posted April 4, 2009 Share Posted April 4, 2009 Wasn't for you. Yours was entirely correct Quote Link to comment https://forums.phpfreaks.com/topic/152470-is-there-an-easier-way-to-do-this-calculation/#findComment-801037 Share on other sites More sharing options...
sasa Posted April 5, 2009 Share Posted April 5, 2009 <?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; ?> Quote Link to comment https://forums.phpfreaks.com/topic/152470-is-there-an-easier-way-to-do-this-calculation/#findComment-801543 Share on other sites More sharing options...
adambeazley Posted April 6, 2009 Author Share Posted April 6, 2009 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!!! Quote Link to comment https://forums.phpfreaks.com/topic/152470-is-there-an-easier-way-to-do-this-calculation/#findComment-802071 Share on other sites More sharing options...
corbin Posted April 6, 2009 Share Posted April 6, 2009 "this is a compounding interest equation" Technically his was a compounding interest equation. The interest compounds in his. In yours, the interest compounds, and each number is added back to the sum. Quote Link to comment https://forums.phpfreaks.com/topic/152470-is-there-an-easier-way-to-do-this-calculation/#findComment-802876 Share on other sites More sharing options...
DarkWater Posted April 10, 2009 Share Posted April 10, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/152470-is-there-an-easier-way-to-do-this-calculation/#findComment-806644 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.