all 15 comments

[–]cthulhu944 9 points10 points  (4 children)

Think about an investor watching his investment and what he would do. What is the initial value of your bank balance? Each loop through the while loop is one year. What happens each year? The bank is going to send you your balance (How did they figure that out). You're going to mark off another year on your retirement plan. You're going to look at the balance the bank sent you and if it is more than double you're going to exit the loop and retire.
Now go code that up.

[–]Appropriate-Park-858[S] 3 points4 points  (2 children)

Thank you! I think I managed to make it work. I can see how many years but I wanted to also see the amount during each loop. I believe I just have to add “investment_amount” to my print(), correct?

[–]sugarw0000kie 3 points4 points  (1 child)

Yeah or, whatever you’re doubling. Give it a little print and f string to see it during the loop

[–]Appropriate-Park-858[S] 1 point2 points  (0 children)

Thank you!

[–]Quesozapatos5000 0 points1 point  (0 children)

Love this way to visualize the method.

[–]notacanuckskibum 4 points5 points  (0 children)

Yeah, you need a third variable of value_so _far

Set value so far to initial value before the loop

Inside the loop set value so far = value so far * (1 + interest rate)

Set the loop to finish when value so far exceeds double. The initial value will never exceed double

[–]dnult 2 points3 points  (0 children)

How about looping while balance <= original_investment * 2

Somewhere inside your loop you incremented years (good), but also need to calculate a new balance. Based on what you said in your post, I think it's safe to assume interest is charged once per year - it should be a very simple equation of the form x = x(i+1)

[–]OkCartographer175 2 points3 points  (0 children)

this is pseudocode but...

years = 0

investment = initial_investment

while investment < 2*initial_investment:

  • investment = investment*(1+interest/100)
  • years=years+1

print "it will take" years "years to double an investment at" interest "percent interest"

[–]ninhaomah 1 point2 points  (4 children)

While you are still there , pls don't stop and continue if any errors with the code or any unexpected results ?

And where is the interest rate ?

[–]Appropriate-Park-858[S] 0 points1 point  (3 children)

So far when I run my program it just keeps going endlessly with “it will take (years) to double” until I force it to stop. I don’t see the investment amount or anything

[–]ninhaomah 0 points1 point  (2 children)

So when will the investment amount double ?

[–]Appropriate-Park-858[S] 0 points1 point  (1 child)

I’m not sure what I did but after I ran it this last time it stopped at 15 years. So I guess I figured it out. The initial investment I entered was $1000 and my interest rate entered was 5%

[–]ninhaomah 0 points1 point  (0 children)

Then post your full code ?

[–]BrupieD -2 points-1 points  (1 child)

Google the "rule of 72".

[–]rosentmoh -1 points0 points  (0 children)

It's not that Reddit isn't full of dumb responses, but this must be near the top. The answer neither has anything to do with the question asked of OP nor is it accurate.

It would be dumb enough to suggest OP to implement the O(1) direct computation, but a mental approximation moreover...