From c20bb682f275a2b35138030e74bfd2e14e9b28a0 Mon Sep 17 00:00:00 2001 From: Mike Cifelli Date: Sat, 4 Mar 2017 16:33:05 -0500 Subject: [PATCH] Updated the compound interest script --- lisp/compound-interest-test.lisp | 5 ++++- lisp/compound-interest.lisp | 17 ++++++++--------- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/lisp/compound-interest-test.lisp b/lisp/compound-interest-test.lisp index 0f92231..47f1528 100644 --- a/lisp/compound-interest-test.lisp +++ b/lisp/compound-interest-test.lisp @@ -44,6 +44,9 @@ (assert= 81450 (compound-interest 100000 -5 4)) ) + (defun negative-number-of-years () + (assert= 100000 (compound-interest 100000 5 -4)) + ) + ) ) - diff --git a/lisp/compound-interest.lisp b/lisp/compound-interest.lisp index 263580f..c48f4de 100644 --- a/lisp/compound-interest.lisp +++ b/lisp/compound-interest.lisp @@ -1,20 +1,19 @@ (defun decrement (n) (- n 1)) -(defun percent (n percent) - (if (> percent 0) - (/ (+ (* n percent) 50) 100) - (/ (- (* n percent) 50) 100) +(defun percent (n percentage) + (if (> percentage 0) + (/ (+ (* n percentage) 50) 100) + (/ (- (* n percentage) 50) 100) ) ) -(defun compound-interest (principal rate years) - (if (= years 0) +(defun compound-interest (principal interest-rate years) + (if (< years 1) principal (compound-interest - (+ principal (percent principal rate)) - rate + (+ principal (percent principal interest-rate)) + interest-rate (decrement years) ) ) ) -