/* loan(C, R, N, Frequency, P) is true if the reimbursement of a loan of */ /* amount C at P percent per annum requires N repayments of amount R at */ /* the given Frequency (years, months, weeks or days). Exactly three of */ /* C, R, N and P must be bound. */ /* e.g. loan(62000, 639.96, 180, months, X) gives X = 9.30 */ loan(Capital, Repayment, Duration, Frequency, Percent):- nonvar(Capital), nonvar(Repayment), nonvar(Duration), nonvar(Frequency), var(Percent), repayments_per_year(Frequency, RepaymentsPerYear), !, RepaymentOverCapital is Repayment / Capital, Percent0 is 10 / RepaymentsPerYear / 100, loan_percent(20, RepaymentOverCapital, Duration, Percent0, Percent1), Percent is Percent1 * RepaymentsPerYear * 100. loan(Capital, Repayment, Duration, Frequency, 0):- nonvar(Capital), nonvar(Duration), nonvar(Frequency), var(Repayment), !, Repayment is Capital / Duration. loan(Capital, Repayment, Duration, Frequency, Percent):- nonvar(Capital), nonvar(Duration), nonvar(Frequency), nonvar(Percent), var(Repayment), repayments_per_year(Frequency, RepaymentsPerYear), !, Percent1 is Percent / RepaymentsPerYear / 100, Repayment is Capital * Percent1 / (1 - aln(-Duration * ln(1 + Percent1))). loan(Capital, Repayment, Duration, Frequency, 0):- nonvar(Capital), nonvar(Repayment), nonvar(Frequency), var(Duration), !, Duration is Capital / Repayment. loan(Capital, Repayment, Duration, Frequency, Percent):- nonvar(Capital), nonvar(Repayment), nonvar(Frequency), nonvar(Percent), var(Duration), repayments_per_year(Frequency, RepaymentsPerYear), !, Percent1 is Percent / RepaymentsPerYear / 100, Duration is ln(Repayment / (Repayment - Percent1 * Capital)) / ln(1 + Percent1). loan(Capital, Repayment, Duration, Frequency, 0):- nonvar(Repayment), nonvar(Duration), nonvar(Frequency), var(Capital), !, Capital is Repayment * Duration. loan(Capital, Repayment, Duration, Frequency, Percent):- nonvar(Repayment), nonvar(Duration), nonvar(Frequency), nonvar(Percent), var(Capital), repayments_per_year(Frequency, RepaymentsPerYear), !, Percent1 is Percent / RepaymentsPerYear / 100, Capital is Repayment / Percent1 * (1 - aln(-Duration * ln(1 + Percent1))). loan_percent(0, _, _, Percent, Percent):-!. loan_percent(I, RepaymentOverCapital, Duration, Percent0, Percent):- I1 is I - 1, Percent1 is RepaymentOverCapital * (1 - aln(-Duration * ln(1 + Percent0))), loan_percent(I1, RepaymentOverCapital, Duration, Percent1, Percent). repayments_per_year(years, 1). repayments_per_year(months, 12). repayments_per_year(weeks, 52). repayments_per_year(days, 365).