MEDIUM POW() EXAMPLES
Python
#!/usr/bin/env python3
"""
MEDIUM POW() EXAMPLES
Working with different number types and negative/fractional exponents
"""
print("=" * 60)
print("MEDIUM POW() - Beyond Basic Exponentiation")
print("=" * 60)
# Example 1: Negative exponents
print("\n1. Negative Exponents (Reciprocals)")
print("-" * 40)
print("Negative exponent gives reciprocal (1/x^n):\n")
base = 2
for exp in [1, 2, 3, -1, -2, -3]:
result = pow(base, exp)
print(f"pow({base}, {exp:2d}) = {result:8.4f}")
print("\nWith different bases:")
for base in [10, 5, 3]:
result_pos = pow(base, 2)
result_neg = pow(base, -2)
print(f"pow({base}, 2) = {result_pos:6.1f} | pow({base}, -2) = {result_neg:8.6f}")
# Example 2: Fractional exponents (roots)
print("\n2. Fractional Exponents (Roots)")
print("-" * 40)
print("pow(x, 0.5) is square root, pow(x, 1/3) is cube root:\n")
numbers = [4, 9, 16, 25, 27, 64, 100]
for num in numbers:
sqrt = pow(num, 0.5)
cube_root = pow(num, 1/3)
print(f"Number: {num:3d} | Square root: {sqrt:6.2f} | Cube root: {cube_root:6.2f}")
# Example 3: Float vs Integer results
print("\n3. Integer vs Float Results")
print("-" * 40)
# Positive exponents with integers stay integers
int_result = pow(5, 3)
print(f"pow(5, 3) = {int_result} (type: {type(int_result).__name__})")
# Negative exponents convert to float
float_result = pow(5, -2)
print(f"pow(5, -2) = {float_result} (type: {type(float_result).__name__})")
# Float base always gives float
float_base_result = pow(5.0, 3)
print(f"pow(5.0, 3) = {float_base_result} (type: {type(float_base_result).__name__})")
# Example 4: Compound interest calculation
print("\n4. Practical: Compound Interest")
print("-" * 40)
principal = 1000 # Initial amount
rate = 0.05 # 5% annual interest
years = 10
amount = principal * pow(1 + rate, years)
interest = amount - principal
print(f"Principal: ${principal:,.2f}")
print(f"Interest rate: {rate * 100}% per year")
print(f"Time period: {years} years")
print(f"Final amount: ${amount:,.2f}")
print(f"Interest earned: ${interest:,.2f}")
print("\nYear-by-year breakdown:")
for year in range(1, years + 1):
yearly_amount = principal * pow(1 + rate, year)
print(f" Year {year:2d}: ${yearly_amount:,.2f}")
# Example 5: Scientific calculations (half-life)
print("\n5. Practical: Radioactive Decay (Half-Life)")
print("-" * 40)
initial_amount = 1000 # grams
half_life_periods = [0, 1, 2, 3, 4, 5]
print(f"Initial amount: {initial_amount}g")
print(f"Each period reduces amount by half\n")
print("Period | Remaining")
print("-------|----------")
for period in half_life_periods:
remaining = initial_amount * pow(0.5, period)
print(f" {period} | {remaining:8.2f}g")
# Example 6: Growth vs Decay
print("\n6. Exponential Growth vs Decay")
print("-" * 40)
initial = 100
print(f"Starting value: {initial}\n")
print("Growth (factor > 1) vs Decay (factor < 1):")
print("Period | Growth (1.5x) | Decay (0.5x)")
print("-------|---------------|-------------")
for period in range(0, 6):
growth = initial * pow(1.5, period)
decay = initial * pow(0.5, period)
print(f" {period} | {growth:11.2f} | {decay:11.2f}")
# Example 7: Data size calculations (bytes to GB)
print("\n7. Practical: Data Size Conversions")
print("-" * 40)
print("Powers of 1024 in computing:\n")
sizes = ["Byte", "KB", "MB", "GB", "TB", "PB"]
base_value = 1
for power, size_name in enumerate(sizes):
bytes_count = base_value * pow(1024, power)
print(f"{size_name:4s} = 1024^{power} = {bytes_count:20,} bytes")
# Example 8: Distance calculations (exponential scaling)
print("\n8. Practical: Distance at Constant Acceleration")
print("-" * 40)
# Using simplified formula where distance grows with time squared
initial_velocity = 0
acceleration = 10 # m/s^2
print(f"Acceleration: {acceleration} m/s^2")
print(f"Distance = 0.5 * a * t^2\n")
print("Time (s) | Distance (m)")
print("---------|-------------")
for time in range(0, 11):
distance = 0.5 * acceleration * pow(time, 2)
print(f" {time:2d} | {distance:8.1f}")
# Example 9: Percentage calculations
print("\n9. Practical: Percentage Changes Over Time")
print("-" * 40)
original_price = 50.00
inflation_rate = 0.03 # 3% per year
print(f"Original price: ${original_price:.2f}")
print(f"Inflation: {inflation_rate * 100}% per year\n")
print("Years | Price")
print("------|-------")
for years in [0, 1, 5, 10, 20, 30]:
future_price = original_price * pow(1 + inflation_rate, years)
print(f" {years:2d} | ${future_price:6.2f}")
# Example 10: Inverse calculations
print("\n10. Finding Original Value (Inverse)")
print("-" * 40)
final_value = 200
growth_factor = 1.5
periods = 3
# To find original: divide by growth^periods, or multiply by growth^(-periods)
original_value = final_value * pow(growth_factor, -periods)
verify = original_value * pow(growth_factor, periods)
print(f"Final value after growth: {final_value}")
print(f"Growth factor: {growth_factor}x per period")
print(f"Number of periods: {periods}")
print(f"Original value: {original_value:.2f}")
print(f"Verification: {original_value:.2f} * {growth_factor}^{periods} = {verify:.2f}")
print("\n" + "=" * 60)
print("Key Concepts:")
print(" - Negative exponents: pow(x, -n) = 1 / pow(x, n)")
print(" - Fractional exponents: pow(x, 0.5) = square root")
print(" - Integer exponents keep int type (if base is int)")
print(" - Negative exponents always return float")
print(" - Use for compound interest, decay, growth problems")
print("=" * 60)