Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8533991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:05:33+00:00 2026-06-11T10:05:33+00:00

Heres the code I have at the moment, its all working as intended, however,

  • 0

Heres the code I have at the moment, its all working as intended, however, the cumulative total isn’t working, and I’m positive I’m doing something absolutely stupid.

assume period = 20
assume inflation = 3
assume nightlycost = 100
assume nights = 7

$yearlycost = $nightlycost*$nights;
while ($period > 0) {
    $period = $period-1;
    $yearlyincrease = ($inflation / 100) * $yearlycost;
    $nightlyincrease = ($inflation / 100) * $nightlycost;
    $nightlycost = $nightlycost + $nightlyincrease;
    $yearlycost = ($yearlycost + $yearlyincrease) + $yearlycost;
}

Result:
Nightly Hotel Rate in 20 years: $180.61 - <?php echo round($nightlycost, 2); ?> correct

Weekly Hotel Rate in 20 years: $1264.27 - <?php echo round($nightlycost, 2) * 7; ?> correct

Total cost to you over 20 years: $988595884.74 - <?php echo round($yearlycost, 2); ?> incorrect

Everything outputs correctly and as expected, except for the yearly cumulative cost. It should take the previous yearly cost and add that years cost+inflation.

Example: first year is 700, so second year should be 700 + 700 + 21 (21 is 3%, the inflation for that year). Second year cumulative total is thus: 1421. Third year will be 1421 + 721 (last years total) + 3% of 721.

Hopefully this is clear enough for you to see where I’m going wrong. Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T10:05:34+00:00Added an answer on June 11, 2026 at 10:05 am

    I find it hard to understand where your code goes wrong, but my intuition is that the last line in your loop body should have a multiplication.

    Basically, you have a base cost for period 0. Then you want to calculate the cumulative cost given inflation after X years. That cost is (pseudocode)

    base = nightlycost + nights
    infl = 1.03
    cumulative = base + base*infl + base*infl^2 + base*infl^3 + ... + base*infl^periods
    

    The last expression can be simplified to

    cumulative = base*((1-infl^periods)/(1-infl))
    

    (This holds according to Eq. 4 here: http://mathworld.wolfram.com/ExponentialSumFormulas.html)

    Example:

    $base = 100*7;
    $infl = 1.03; // 3% of inflation/year
    
    $periods = 2;
    $cumulative = $base * (1-pow($infl, $periods))/(1-$infl);
    print "Cumulative cost after $periods is $cumulative\n";
    
    // Let's try with three periods.
    $periods = 3;
    $cumulative = $base * (1-pow($infl, $periods))/(1-$infl);
    print "Cumulative cost after $periods is $cumulative\n";
    

    Output:

    Cumulative cost after 2 is 1421
    Cumulative cost after 3 is 2163.63
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a custom download app. Its all working except for the
I'm struggling with something rather simple in flash at the moment. Imagine you have
I'm experimenting with routing in ember at the moment, and have a working example.
At the moment I'm working on a simple syntax highlighter and I have couple
Here's the code I have: var commandsBuffer = List[String]() commandsBuffer ::= cmd.exe commandsBuffer ::=
Here's the code I have: from cStringIO import StringIO from lxml import etree xml
Here is the code I have written for Rectangle class. class Rectangle (l: Double,
enter code here I have a table on SQL server 2005 with bigint primary
Here is the code I have to create an UIAlertView with a textbox. UIAlertView
enter code here I have a ListView that have a different Layout XML for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.