0

I'm trying to find and print the minimum value for n that satisfies ca >= cb:

ap = 80000
bp = 200000
at = 1.03
bt = 1.015
n = 1
ca = ap*at*n
cb = bp*bt*n

while cb > ca:
    n = n + 1
    print(n)

The code just runs and prints n + 1 indefinitely, though. What is the correct approach to this problem?

3
  • Use an if statement Commented Sep 20, 2017 at 5:25
  • The assignments to ca and cb were one-time events. They don't auto-update in response to later changes to n, which seems to be what you were expecting to happen. You need to recalculate them each time through the loop Commented Sep 20, 2017 at 5:36
  • Put the actual question you had in hand. Your logic is not correct. Commented Sep 20, 2017 at 7:39

1 Answer 1

1
ap = 80000
bp = 200000
at = 1.03
bt = 1.015
n = 1
ca = ap*at*n
cb = bp*bt*n

while cb > ca:
  n += 1
  ca = ap*at*n
  cb = bp*bt*n

This won't converge though.

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

4 Comments

this doesn't converge: as long as bp*bt is greater than ap*at, the change of n won't change the truth of cb > ca
this code may come to an end if you use finite data types, like in c, but not in python
Correct @Aemyl I was just editing my answer for that :)
thanks ketan! your answer and a little change in my logic solved my problem

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.