Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to find how many years it will take to reach t amount in Python
Suppose we have some parameters P, O, E, T. If we have P dollars in principal that we want to invest in the stock market. The stock market alternates between first returning E and then O percent interest per year, we have to check how many years it would take to reach at least T dollars.
Problem Understanding
The investment follows an alternating pattern:
- Year 1, 3, 5... − Apply E percent interest
- Year 2, 4, 6... − Apply O percent interest
We need to calculate how many years it takes for the principal to reach or exceed the target amount T.
Example Walkthrough
If the input is P = 200, O = 10, E = 25, T = 300:
- Year 1: Apply 25% interest − 200 × 1.25 = 250
- Year 2: Apply 10% interest − 250 × 1.10 = 275
- Year 3: Apply 25% interest − 275 × 1.25 = 343.75 ? 300
Result: 3 years required.
Algorithm
To solve this, we will follow these steps −
- Initialize answer counter to 0
- While principal amount is less than target, do:
- Apply E percent interest and increment year counter
- If still less than target, apply O percent interest and increment year counter
- Return the total years
Implementation
class Solution:
def solve(self, P, O, E, T):
ans = 0
while P < T:
P *= 1 + (E / 100)
ans += 1
if P < T:
P *= 1 + (O / 100)
ans += 1
return ans
# Test the solution
ob = Solution()
P = 200
O = 10
E = 25
T = 300
print(ob.solve(P, O, E, T))
3
Step-by-Step Execution
Let's trace through the example to see how the algorithm works ?
def solve_with_trace(P, O, E, T):
ans = 0
print(f"Initial: P = {P}, Target = {T}")
while P < T:
# Apply E% interest (odd years: 1, 3, 5...)
P *= 1 + (E / 100)
ans += 1
print(f"Year {ans}: Applied {E}% interest, P = {P:.2f}")
if P < T:
# Apply O% interest (even years: 2, 4, 6...)
P *= 1 + (O / 100)
ans += 1
print(f"Year {ans}: Applied {O}% interest, P = {P:.2f}")
print(f"Target reached in {ans} years")
return ans
# Test with example
solve_with_trace(200, 10, 25, 300)
Initial: P = 200, Target = 300 Year 1: Applied 25% interest, P = 250.00 Year 2: Applied 10% interest, P = 275.00 Year 3: Applied 25% interest, P = 343.75 Target reached in 3 years
Alternative Implementation
Here's a more explicit version that tracks odd and even years separately ?
def calculate_investment_years(principal, odd_rate, even_rate, target):
years = 0
current_amount = principal
while current_amount < target:
years += 1
if years % 2 == 1: # Odd years
current_amount *= 1 + (even_rate / 100)
else: # Even years
current_amount *= 1 + (odd_rate / 100)
return years
# Test the function
result = calculate_investment_years(200, 10, 25, 300)
print(f"Years needed: {result}")
Years needed: 3
Conclusion
This algorithm efficiently calculates investment growth with alternating interest rates. The key insight is applying E% in odd years and O% in even years until the target is reached.
