Ruby algorithms loops codewars










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])









share|improve this question



















  • 2




    Have you tried breaking the problem down into smaller parts?
    – lacostenycoder
    Nov 10 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.
    – Krzynek
    Nov 10 at 19:42















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])









share|improve this question



















  • 2




    Have you tried breaking the problem down into smaller parts?
    – lacostenycoder
    Nov 10 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.
    – Krzynek
    Nov 10 at 19:42













0












0








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])









share|improve this question















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])






ruby algorithm math






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 19:23









Cary Swoveland

67.3k53965




67.3k53965










asked Nov 10 at 19:09









Krzynek

465




465







  • 2




    Have you tried breaking the problem down into smaller parts?
    – lacostenycoder
    Nov 10 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.
    – Krzynek
    Nov 10 at 19:42












  • 2




    Have you tried breaking the problem down into smaller parts?
    – lacostenycoder
    Nov 10 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.
    – Krzynek
    Nov 10 at 19:42







2




2




Have you tried breaking the problem down into smaller parts?
– lacostenycoder
Nov 10 at 19:34




Have you tried breaking the problem down into smaller parts?
– lacostenycoder
Nov 10 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.
– Krzynek
Nov 10 at 19:42




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.
– Krzynek
Nov 10 at 19:42












2 Answers
2






active

oldest

votes


















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.






share|improve this answer






















  • Cary, I'm speechless. Thank you very much. It must have taken you ages to break it down in such a detail
    – Krzynek
    Nov 11 at 22:32


















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





share|improve this answer




















  • this is so much cleaner than your first try, nice one.
    – lacostenycoder
    Nov 10 at 20:57










  • There are two percentages, 1.5% and 0.5%.
    – Cary Swoveland
    Nov 11 at 4:35










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53242479%2fruby-algorithms-loops-codewars%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









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.






share|improve this answer






















  • Cary, I'm speechless. Thank you very much. It must have taken you ages to break it down in such a detail
    – Krzynek
    Nov 11 at 22:32















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.






share|improve this answer






















  • Cary, I'm speechless. Thank you very much. It must have taken you ages to break it down in such a detail
    – Krzynek
    Nov 11 at 22:32













1












1








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.






share|improve this answer














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.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 11 at 10:08

























answered Nov 11 at 5:17









Cary Swoveland

67.3k53965




67.3k53965











  • Cary, I'm speechless. Thank you very much. It must have taken you ages to break it down in such a detail
    – Krzynek
    Nov 11 at 22:32
















  • Cary, I'm speechless. Thank you very much. It must have taken you ages to break it down in such a detail
    – Krzynek
    Nov 11 at 22:32















Cary, I'm speechless. Thank you very much. It must have taken you ages to break it down in such a detail
– Krzynek
Nov 11 at 22:32




Cary, I'm speechless. Thank you very much. It must have taken you ages to break it down in such a detail
– Krzynek
Nov 11 at 22:32













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





share|improve this answer




















  • this is so much cleaner than your first try, nice one.
    – lacostenycoder
    Nov 10 at 20:57










  • There are two percentages, 1.5% and 0.5%.
    – Cary Swoveland
    Nov 11 at 4:35















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





share|improve this answer




















  • this is so much cleaner than your first try, nice one.
    – lacostenycoder
    Nov 10 at 20:57










  • There are two percentages, 1.5% and 0.5%.
    – Cary Swoveland
    Nov 11 at 4:35













1












1








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





share|improve this answer












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






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 10 at 20:24









Krzynek

465




465











  • this is so much cleaner than your first try, nice one.
    – lacostenycoder
    Nov 10 at 20:57










  • There are two percentages, 1.5% and 0.5%.
    – Cary Swoveland
    Nov 11 at 4:35
















  • this is so much cleaner than your first try, nice one.
    – lacostenycoder
    Nov 10 at 20:57










  • There are two percentages, 1.5% and 0.5%.
    – Cary Swoveland
    Nov 11 at 4:35















this is so much cleaner than your first try, nice one.
– lacostenycoder
Nov 10 at 20:57




this is so much cleaner than your first try, nice one.
– lacostenycoder
Nov 10 at 20:57












There are two percentages, 1.5% and 0.5%.
– Cary Swoveland
Nov 11 at 4:35




There are two percentages, 1.5% and 0.5%.
– Cary Swoveland
Nov 11 at 4:35

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53242479%2fruby-algorithms-loops-codewars%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Use pre created SQLite database for Android project in kotlin

Darth Vader #20

Ondo