0

I got stuck with below task and spent about 3 hours trying to figure it out.
Task description: A man has a rather old car being worth $2000. He saw a secondhand car being worth $8000. He wants to keep his old car until he can buy the secondhand one. He thinks he can save $1000 each month but the prices of his old car and of the new one decrease of 1.5 percent per month. Furthermore this percent of loss increases by 0.5 percent at the end of every two months. Our man finds it difficult to make all these calculations. How many months will it take him to save up enough money to buy the car he wants, and how much money will he have left over?

My code so far:

def nbMonths(startPriceOld, startPriceNew, savingperMonth, percentLossByMonth)
  dep_value_old = startPriceOld    
  mth_count = 0
  total_savings = 0
  dep_value_new = startPriceNew
  mth_count_new = 0
  while startPriceOld != startPriceNew do    
    if startPriceOld >= startPriceNew
      return mth_count = 0, startPriceOld - startPriceNew
    end    
    dep_value_new = dep_value_new - (dep_value_new * percentLossByMonth / 100)
    mth_count_new += 1
    if mth_count_new % 2 == 0
      dep_value_new = dep_value_new - (dep_value_new * 0.5) / 100
    end
    dep_value_old = dep_value_old - (dep_value_old * percentLossByMonth / 100)
    mth_count += 1
    total_savings += savingperMonth
    if mth_count % 2 == 0
      dep_value_old = dep_value_old - (dep_value_old * 0.5) / 100
    end
    affordability = total_savings + dep_value_old
    if affordability >= dep_value_new
      return mth_count, affordability - dep_value_new
    end
  end
end

print nbMonths(2000, 8000, 1000, 1.5) # Expected result[6, 766])
2
  • 2
    Have you tried breaking the problem down into smaller parts? Commented Nov 10, 2018 at 19:34
  • Yes I have. I have even tried to do it on a piece of paper but got the same result [6, 601]. I can't figure out where the diff of 165 in savings is coming from. I have started from depreciation calculations of old car first. Than added if statement to increase % by 0.5 every 2 months. Once this was up an running I have done the same on paper and once agreed I did the second car. Commented Nov 10, 2018 at 19:42

2 Answers 2

1

The data are as follows.

op = 2000.0  # current old car value
np = 8000.0  # current new car price
sv = 1000.0  # annual savings
dr = 0.015   # annual depreciation, both cars (1.5%)
cr = 0.005.  # additional depreciation every two years, both cars (0.5%)

After n >= 0 months the man's (let's call him "Rufus") savings plus the value of his car equal

sv*n + op*(1 - n*dr - (cr + 2*cr + 3*cr +...+ (n/2)*cr))

where n/2 is integer division. As

cr + 2*cr + 3*cr +...+ (n/2)*cr = cr*((1+2+..+n)/2) = cr*(1+n/2)*(n/2)

the expression becomes

sv*n + op*(1 - n*dr - cr*(1+(n/2))*(n/2))

Similarly, after n years the cost of the car he wants to purchase will fall to

np * (1 - n*dr - cr*(1+(n/2))*(n/2))

If we set these two expressions equal we obtain the following.

sv*n + op - op*dr*n - op*cr*(n/2) - op*cr*(n/2)**2 =
np - np*dr*n - np*cr*(n/2) - np*cr*(n/2)**2

which reduces to

cr*(np-op)*(n/2)**2 + (sv + dr*(np-op))*n + cr*(np-op)*(n/2) - (np-op) = 0

or

cr*(n/2)**2 + (sv/(np-op) + dr)*n + cr*(n/2) - 1 = 0

If we momentarily treat (n/2) as a float division, this expression reduces to a quadratic.

(cr/4)*n**2 + (sv/(np-op) + dr + cr/2)*n - 1 = 0
  = a*n**2 + b*n + c = 0

where

a = cr/4 = 0.005/4 = 0.00125
b = sv/(np-op) + dr + cr/(2*a) = 1000.0/(8000-2000) + 0.015 + 0.005/2 = 0.18417
c = -1

Incidentally, Rufus doesn't have a computer, but he does have an HP 12c calculator his grandfather gave him when he was a kid, which is perfectly adequate for these simple calculations.

The roots are computed as follows.

(-b + Math.sqrt(b**2 - 4*a*c))/(2*a)  #=>    5.24
(-b - Math.sqrt(b**2 - 4*a*c))/(2*a)  #=> -152.58

It appears that Rufus can purchase the new vehicle (if it's still for sale) in six years. Had we been able able to solve the above equation for n/2 using integer division it might have turned out that Rufus would have had to wait longer. That’s because for a given n both cars would have depreciated less (or at least not not more), and because the car to be purchased is more expensive than the current car, the difference in values would be greater than that obtained with the float approximation for 1/n. We need to check that, however. After n years, Rufus' savings and the value of his beater will equal

sv*n + op*(1 - dr*n - cr*(1+(n/2))*(n/2))
  = 1000*n + 2000*(1 - 0.015*n - 0.005*(1+(n/2))*(n/2))

For n = 6 this equals

1000*6 + 2000*(1 - 0.015*6 - 0.005*(1+(6/2))*(6/2))
  = 1000*6 + 2000*(1 - 0.015*6 - 0.005*(1+3)*3)
  = 1000*6 + 2000*0.85
  = 7700

The cost of Rufus' dream car after n years will be

np * (1 - dr*n - cr*(1+(n/2))*(n/2))
  = 8000 * (1 - 0.015*n - 0.005*(1+(n/2))*(n/2))

For n=6 this becomes

8000 * (1 - 0.015*6 - 0.005*(1+(6/2))*(6/2))
  = 8000*0.85
  = 6800

(Notice that the factor 0.85 is the same in both calculations.)

Yes, Rufus will be able to buy the car in 6 years.

Sign up to request clarification or add additional context in comments.

1 Comment

Cary, I'm speechless. Thank you very much. It must have taken you ages to break it down in such a detail
1
def nbMonths(old, new, savings, percent)
  percent = percent.fdiv(100)
  current_savings = 0
  months = 0
  loop do
    break if current_savings + old >= new
    current_savings += savings
    old -= old * percent
    new -= new * percent
    months += 1
    percent += 0.005 if months.odd?
  end
  [months, (current_savings + old - new).round]
end

2 Comments

this is so much cleaner than your first try, nice one.
There are two percentages, 1.5% and 0.5%.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.