Program to find total amount of money we have in bank in Python

Suppose you put 1Rs in a bank on the first day (Monday). Each subsequent day of the week, you put in 1Rs more than the previous day. On every new Monday, you start with 1Rs more than the previous Monday. This creates a pattern where the amount increases both daily and weekly.

For example, if n = 17 days:

  • Week 1: 1Rs (Mon) + 2Rs (Tue) + 3Rs (Wed) + 4Rs (Thu) + 5Rs (Fri) + 6Rs (Sat) + 7Rs (Sun) = 28Rs

  • Week 2: 2Rs (Mon) + 3Rs (Tue) + 4Rs (Wed) + 5Rs (Thu) + 6Rs (Fri) + 7Rs (Sat) + 8Rs (Sun) = 35Rs

  • Week 3: 3Rs (Mon) + 4Rs (Tue) + 5Rs (Wed) = 12Rs

  • Total: 28 + 35 + 12 = 75Rs

Algorithm Steps

To solve this problem efficiently ?

  • Calculate complete weeks and remaining days

  • For each complete week, calculate the sum using the arithmetic progression formula

  • Add the sum for remaining days in the partial week

  • Return the total amount

Example Implementation

def calculate_bank_money(n):
    """
    Calculate total money in bank after n days following the pattern:
    Week 1: 1,2,3,4,5,6,7
    Week 2: 2,3,4,5,6,7,8
    Week 3: 3,4,5,6,7,8,9
    And so on...
    """
    if n <= 0:
        return 0
    
    total_money = 0
    
    if n > 7:
        # Calculate complete weeks
        complete_weeks = n // 7
        
        # Sum for first week is 28 (1+2+3+4+5+6+7)
        first_week_sum = 28
        total_money = first_week_sum
        
        # Add sum for subsequent complete weeks
        for week in range(1, complete_weeks):
            # Each week starts 1Rs higher than previous week
            week_sum = first_week_sum + 7 * week
            total_money += week_sum
        
        # Calculate remaining days in partial week
        remaining_days = n % 7
        week_number = complete_weeks + 1
        
        for day in range(1, remaining_days + 1):
            daily_amount = day + (week_number - 1)
            total_money += daily_amount
    else:
        # If n <= 7, just sum first n days
        for day in range(1, n + 1):
            total_money += day
    
    return total_money

# Test with the example
n = 17
result = calculate_bank_money(n)
print(f"Total money after {n} days: {result}Rs")

# Test with other values
test_cases = [7, 10, 14, 21]
for days in test_cases:
    money = calculate_bank_money(days)
    print(f"After {days} days: {money}Rs")
Total money after 17 days: 75Rs
After 7 days: 28Rs
After 10 days: 40Rs
After 14 days: 63Rs
After 21 days: 126Rs

Optimized Solution

We can optimize this using mathematical formulas ?

def calculate_bank_money_optimized(n):
    """
    Optimized version using mathematical formulas
    """
    if n <= 0:
        return 0
    
    if n <= 7:
        # Sum of first n natural numbers
        return n * (n + 1) // 2
    
    complete_weeks = n // 7
    remaining_days = n % 7
    
    # Sum for complete weeks
    # Each week k has sum: 28 + 7*(k-1) where k starts from 1
    total = 0
    for k in range(1, complete_weeks + 1):
        total += 28 + 7 * (k - 1)
    
    # Sum for remaining days in partial week
    if remaining_days > 0:
        week_start = complete_weeks + 1
        for day in range(1, remaining_days + 1):
            total += day + (week_start - 1)
    
    return total

# Verify with test cases
n = 17
print(f"Optimized result for {n} days: {calculate_bank_money_optimized(n)}Rs")
Optimized result for 17 days: 75Rs

Pattern Analysis

Week Days Pattern Weekly Sum
1 1-7 1,2,3,4,5,6,7 28
2 8-14 2,3,4,5,6,7,8 35
3 15-21 3,4,5,6,7,8,9 42

Conclusion

This problem demonstrates a compound arithmetic progression where both daily and weekly increments apply. The key insight is breaking it into complete weeks and remaining days for efficient calculation. The optimized solution reduces time complexity while maintaining accuracy.

---
Updated on: 2026-03-25T20:32:30+05:30

618 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements