Python:
import math
def survival_years(total, monthly_spend, annual_return=0.05):
"""
Returns how many years you can survive.
If survival is infinite, returns -1.
"""
yearly_spend = monthly_spend * 12
# No growth case
if annual_return == 0:
return total / yearly_spend
withdrawal_rate = yearly_spend / total
# Infinite survival
if annual_return >= withdrawal_rate:
return -1
# Finite survival with growth
numerator = yearly_spend / (annual_return * total) + 1
years = math.log(numerator) / math.log(1 + annual_return)
return years