Skip to content

Instantly share code, notes, and snippets.

@rick7661
Created June 13, 2021 11:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rick7661/06fb32cfa9e07f53edbc01acd2e1f4c4 to your computer and use it in GitHub Desktop.
Save rick7661/06fb32cfa9e07f53edbc01acd2e1f4c4 to your computer and use it in GitHub Desktop.
Calculates total investment return by fixed investment per term, fixed interest per term and number of terms. It is used only to project expected return for a fixed amount, fixed term investment with fixed interest.
import Foundation
// MARK: - Usage: Paste the source code in Xcode Playground and run the examples
// MARK: - Parameters
let fortnightlyInvest: Decimal = 800
let monthlyInvest: Decimal = fortnightlyInvest / 14 * 365 / 12
let yearlyInvest: Decimal = fortnightlyInvest / 14 * 365
let interest: Decimal = 0.08
let years: Int = 10
// MARK: - Functions
/// Total investment return as a formula:
/// `f(p, i, n) = p * (1 + i)^n + f(p, i, n - 1)`
/// - Parameters:
/// - p: Investment per term. E.g. $100 per year, enter 100.
/// - i: Interest per term. E.g. An yearly interst of 10% should be 0.1.
/// - n: Number of terms. E.g. 10 years, enter 10.
/// - Returns: Total return at the end of investment.
func totalReturn_recursion(investmentPerTerm p: Decimal,
interestPerTerm i: Decimal,
numberOfTerms n: Int) -> Decimal {
guard n > 1 else {
return p * (1 + i)
}
return p * pow((1 + i), n) + totalReturn_recursion(investmentPerTerm: p,
interestPerTerm: i,
numberOfTerms: n - 1)
}
/// Total investment return as a formula:
/// `f(p, i, n) = p * [(1 + i)^n + (1 + i)^(n - 1) + ... + (1 + i)^1]`
/// `= p * [(1 + i)^1 + (1 + i)^2 + ... + (1 + i)^n]`
/// - Parameters:
/// - p: Investment per term. E.g. $100 per year, enter 100.
/// - i: Interest per term. E.g. An yearly interst of 10% should be 0.1.
/// - n: Number of terms. E.g. 10 years, enter 10.
/// - Returns: Total return at the end of investment.
func totalReturn_loop(investmentPerTerm p: Decimal,
interestPerTerm i: Decimal,
numberOfTerms n: Int) -> Decimal {
var accumulator: Decimal = 0
for terms: Int in 1 ... n {
accumulator += pow((1 + i), terms)
}
return p * accumulator
}
/// Total investment return as a formula:
/// `f(p, i, n) = p * (1 + i) * ((1 + i)^n - 1) / i`
/// - Parameters:
/// - p: Investment per term. E.g. $100 per year, enter 100.
/// - i: Interest per term. E.g. An yearly interst of 10% should be 0.1.
/// - n: Number of terms. E.g. 10 years, enter 10.
/// - Returns: Total return at the end of investment.
func totalReturn_formula(investmentPerTerm p: Decimal,
interestPerTerm i: Decimal,
numberOfTerms n: Int) -> Decimal {
return p * (1 + i) * (pow((1 + i), n) - 1) / i
}
// MARK: - Examples
// Invest yearly, with fixed yearly interest, for a fix number of years. Recursion.
print(totalReturn_recursion(investmentPerTerm: yearlyInvest,
interestPerTerm: interest,
numberOfTerms: years))
// Invest yearly, with fixed yearly interest, for a fix number of years. Loop.
print(totalReturn_loop(investmentPerTerm: yearlyInvest,
interestPerTerm: interest,
numberOfTerms: years))
// Invest yearly, with fixed yearly interest, for a fix number of years. Formula.
print(totalReturn_formula(investmentPerTerm: yearlyInvest,
interestPerTerm: interest,
numberOfTerms: years))
// Invest monthly, with fixed monthly interest, for a fix number of months. Formula.
print(totalReturn_formula(investmentPerTerm: monthlyInvest,
interestPerTerm: interest / 12,
numberOfTerms: years * 12))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment