Vous êtes sur la page 1sur 18

PAYING OFF CREDIT CARD DEBT

Each month, a credit card statement will come with the option for you to pay
a minimum amount of your charge, usually 2% of the balance due. However,
the credit card company earns money by charging interest on the balance
that you don't pay. So even if you pay credit card payments on time, interest
is still accruing on the outstanding balance.
Say you've made a $5,000 purchase on a credit card with an 18% annual
interest rate and a 2% minimum monthly payment rate. If you only pay the
minimum monthly amount for a year, how much is the remaining balance?
You can think about this in the following way.
At the beginning of month 0 (when the credit card statement arrives),
assume you owe an amount we will call b0 (b for balance; subscript 0 to
indicate this is the balance at month 0).
Any payment you make during that month is deducted from the balance.
Let's call the payment you make in month 0, p0. Thus, your unpaid
balance for month 0,

ub0, is equal to b0p0.


ub0=b0p0

At the beginning of month 1, the credit card company will charge you
interest on your unpaid balance. So if your annual interest rate is r, then at
the beginning of month 1, your new balance is your previous unpaid
balance ub0, plus the interest on this unpaid balance for the month. In
algebra, this new balance would be

b1=ub0+(r/12.0)ub0
In month 1, we will make another payment, p1. That payment has to cover
some of the interest costs, so it does not completely go towards paying off
the original charge. The balance at the beginning of month 2, b2, can be
calculated by first calculating the unpaid balance after paying
adding the interest accrued:

ub1=b1p1
b2=ub1+(r/12.0)ub1

p1, then by

If you choose just to pay off the minimum monthly payment each month,
you will see that the compound interest will dramatically reduce your ability
to lower your debt.
Let's look at an example. If you've got a $5,000 balance on a credit card
with 18% annual interest rate, and the minimum monthly payment is 2% of
the current balance, we would have the following repayment schedule if you
only pay the minimum payment each month:

Month

Balance

5000.00

Minimum
Payment

Unpaid Balance

Interest

100 (= 5000 *

4900 (= 5000 -

73.50 (=

0.02)

100)

0.18/12.0 * 4900)

4973.50 (= 4900

99.47 (=

4874.03 (=

73.11 (=

+ 73.50)

4973.50 * 0.02)

4973.50 - 99.47)

0.18/12.0 *
4874.03)

4947.14 (=

98.94 (=

4848.20 (=

72.72 (=

4874.03 + 73.11)

4947.14 * 0.02)

4947.14 - 98.94)

0.18/12.0 *
4848.20)

You can see that a lot of your payment is going to cover interest, and if you
work this through month 12, you will see that after a year, you will have paid
$1165.63 and yet you will still owe $4691.11 on what was originally a
$5000.00 debt. Pretty depressing!

PROBLEM 1: PAYING THE MINIMUM

(10 points possible)

Write a program to calculate the credit card balance after one year if a
person only pays the minimum monthly payment required by the credit card
company each month.
The following variables contain values as described below:
1. balance - the outstanding balance on the credit card
2. annualInterestRate - annual interest rate as a decimal
3. monthlyPaymentRate - minimum monthly payment rate as a decimal
For each month, calculate statements on the monthly payment and
remaining balance, and print to screen something of the format:
Month: 1
Minimum monthly payment: 96.0
Remaining balance: 4784.0

Be sure to print out no more than two decimal digits of accuracy - so print
Remaining balance: 813.41

instead of
Remaining balance: 813.4141998135

Finally, print out the total amount paid that year and the remaining balance
at the end of the year in the format:
Total paid: 96.0
Remaining balance: 4784.0

A summary of the required math is found below:

Monthly interest rate= (Annual interest rate) / 12.0


Minimum monthly payment = (Minimum monthly payment rate) x
(Previous balance)
Monthly unpaid balance = (Previous balance) - (Minimum monthly
payment)
Updated balance each month = (Monthly unpaid balance) + (Monthly
interest rate x Monthly unpaid balance)
Note that the grading script looks for the order in which each value is printed
out. We provide sample test cases below; we suggest you develop your code
on your own machine, and make sure your code passes the sample test
cases, before you paste it into the box below.
Test Cases to Test Your Code With. Be sure to test these on your own
machine - and that you get the same output! - before running your
code on this webpage!
Click to See Problem 1 Test Cases

Note: Depending on where you round in this problem, your answers may be
off by a few cents in either direction. Do not worry if your solution is within
+/- 0.05 of the correct answer.
Be sure to test these on your own machine - and that you get the same
output! - before running your code on this webpage!
Test Cases:
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.

Test Case 1:
balance = 4213
annualInterestRate = 0.2
monthlyPaymentRate = 0.04
Result Your Code Should Generate:
------------------Month: 1
Minimum monthly payment: 168.52
Remaining balance: 4111.89
Month: 2
Minimum monthly payment: 164.48
Remaining balance: 4013.2
Month: 3
Minimum monthly payment: 160.53
Remaining balance: 3916.89

18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.

Month: 4
Minimum monthly payment: 156.68
Remaining balance: 3822.88
Month: 5
Minimum monthly payment: 152.92
Remaining balance: 3731.13
Month: 6
Minimum monthly payment: 149.25
Remaining balance: 3641.58
Month: 7
Minimum monthly payment: 145.66
Remaining balance: 3554.19
Month: 8
Minimum monthly payment: 142.17
Remaining balance: 3468.89
Month: 9
Minimum monthly payment: 138.76
Remaining balance: 3385.63
Month: 10
Minimum monthly payment: 135.43
Remaining balance: 3304.38
Month: 11
Minimum monthly payment: 132.18
Remaining balance: 3225.07
Month: 12
Minimum monthly payment: 129.0
Remaining balance: 3147.67
Total paid: 1775.55
Remaining balance: 3147.67

47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.

Test Case 2:
balance = 4842
annualInterestRate = 0.2
monthlyPaymentRate = 0.04
Result Your Code Should Generate:
------------------Month: 1
Minimum monthly payment: 193.68
Remaining balance: 4725.79
Month: 2
Minimum monthly payment: 189.03
Remaining balance: 4612.37
Month: 3
Minimum monthly payment: 184.49
Remaining balance: 4501.68
Month: 4
Minimum monthly payment: 180.07
Remaining balance: 4393.64
Month: 5
Minimum monthly payment: 175.75
Remaining balance: 4288.19
Month: 6

73.
74.
75.
76.
77.
78.
79.
80.
81.
82.
83.
84.
85.
86.
87.
88.
89.
90.
91.
92.
93.
94.

95.
96.

Minimum monthly payment: 171.53


Remaining balance: 4185.27
Month: 7
Minimum monthly payment: 167.41
Remaining balance: 4084.83
Month: 8
Minimum monthly payment: 163.39
Remaining balance: 3986.79
Month: 9
Minimum monthly payment: 159.47
Remaining balance: 3891.11
Month: 10
Minimum monthly payment: 155.64
Remaining balance: 3797.72
Month: 11
Minimum monthly payment: 151.91
Remaining balance: 3706.57
Month: 12
Minimum monthly payment: 148.26
Remaining balance: 3617.62
Total paid: 2040.64
Remaining balance: 3617.62

The code you paste into the following box should not specify the values for
the variables balance , annualInterestRate , or monthlyPaymentRate - our test
code will define those values before testing your submission.

PROBLEM 2: PAYING DEBT OFF IN A YEAR

(15 points possible)

Now write a program that calculates the minimum fixed monthly payment
needed in order pay off a credit card balance within 12 months. By a fixed
monthly payment, we mean a single number which does not change each
month, but instead is a constant amount that will be paid each month.
In this problem, we will not be dealing with a minimum monthly payment
rate.
The following variables contain values as described below:

1. balance - the outstanding balance on the credit card


2. annualInterestRate - annual interest rate as a decimal
The program should print out one line: the lowest monthly payment that will
pay off all debt in under 1 year, for example:
Lowest Payment: 180

Assume that the interest is compounded monthly according to the balance at


the end of the month (after the payment for that month is made). The
monthly payment must be a multiple of $10 and is the same for all months.
Notice that it is possible for the balance to become negative using this
payment scheme, which is okay. A summary of the required math is found
below:
Monthly interest rate = (Annual interest rate) / 12.0
Monthly unpaid balance = (Previous balance) - (Minimum fixed monthly
payment)
Updated balance each month = (Monthly unpaid balance) + (Monthly
interest rate x Monthly unpaid balance)
Test Cases to Test Your Code With. Be sure to test these on your own
machine - and that you get the same output! - before running your
code on this webpage!
Click to See Problem 2 Test Cases

Be sure to test these on your own machine - and that you get the same
output! - before running your code on this webpage!
Test Cases:
1.
2.
3.
4.
5.
6.
7.
8.

9.

Test Case 1:
balance = 3329
annualInterestRate = 0.2
Result Your Code Should Generate:
------------------Lowest Payment: 310

10.
11.
12.
13.
14.
15.
16.
17.
18.

Test Case 2:
balance = 4773
annualInterestRate = 0.2
Result Your Code Should Generate:
------------------Lowest Payment: 440

19.
20.
21.
22.
23.
24.
25.
26.
27.
28.

Test Case 3:
balance = 3926
annualInterestRate = 0.2
Result Your Code Should Generate:
------------------Lowest Payment: 360

29.
30.

The code you paste into the following box should not specify the values for
the variables balance or annualInterestRate - our test code will define those
values before testing your submission.

PROBLEM 3: USING BISECTION SEARCH TO MAKE THE


PROGRAM FASTER
(25 points possible)

You'll notice that in Problem 2, your monthly payment had to be a multiple


of $10. Why did we make it that way? You can try running your code locally
so that the payment can be any dollar and cent amount (in other words, the
monthly payment is a multiple of $0.01). Does your code still work? It
should, but you may notice that your code runs more slowly, especially in
cases with very large balances and interest rates. (Note: when your code is
running on our servers, there are limits on the amount of computing time
each submission is allowed, so your observations from running this

experiment on the grading system might be limited to an error message


complaining about too much time taken.)
Well then, how can we calculate a more accurate fixed monthly payment
than we did in Problem 2 without running into the problem of slow code? We
can make this program run faster using a technique introduced in lecture bisection search!
The following variables contain values as described below:
1. balance - the outstanding balance on the credit card
2. annualInterestRate - annual interest rate as a decimal
To recap the problem: we are searching for the smallest monthly payment
such that we can pay off the entire balance within a year. What is a
reasonable lower bound for this payment value? $0 is the obvious anwer,
but you can do better than that. If there was no interest, the debt can be
paid off by monthly payments of one-twelfth of the original balance, so we
must pay at least this much every month. One-twelfth of the original balance
is a good lower bound.
What is a good upper bound? Imagine that instead of paying monthly, we
paid off the entire balance at the end of the year. What we ultimately pay
must be greater than what we would've paid in monthly installments,
because the interest was compounded on the balance we didn't pay off each
month. So a good upper bound for the monthly payment would be onetwelfth of the balance, after having its interest compounded monthly for an
entire year.
In short:
Monthly interest rate = (Annual interest rate) / 12.0
Monthly payment lower bound = Balance / 12
Monthly payment upper bound = (Balance x (1 + Monthly interest rate)12)
/ 12.0
Write a program that uses these bounds and bisection search (for more info
check out the Wikipedia page on bisection search) to find the smallest
monthly payment to the cent (no more multiples of $10) such that we can

pay off the debt within a year. Try it out with large inputs, and notice how
fast it is (try the same large inputs in your solution to Problem 2 to
compare!). Produce the same return value as you did in Problem 2.
Note that if you do not use bisection search, your code will not run - your
code only has 30 seconds to run on our servers.
Test Cases to Test Your Code With. Be sure to test these on your own
machine - and that you get the same output! - before running your
code on this webpage!
Click to See Problem 3 Test Cases

Note: The automated tests are leinient - if your answers are off by a few
cents in either direction, your code is OK.
Be sure to test these on your own machine - and that you get the same
output! - before running your code on this webpage!
Test Cases:
1.
2.
3.
4.
5.
6.
7.
8.

Test Case 1:
balance = 320000
annualInterestRate = 0.2
Result Your Code Should Generate:
------------------Lowest Payment: 29157.09

9.
10.
11.
12.
13.
14.
15.
16.
17.
18.

Test Case 2:
balance = 999999
annualInterestRate = 0.18
Result Your Code Should Generate:
------------------Lowest Payment: 90325.03

19.

Problem set 3

RADIATION EXPOSURE

(25 points possible)

"Radioactive decay" is the process by which an unstable atom loses energy


and emits ionizing particles - what is commonly refered to as radiation.
Exposure to radiation can be dangerous and is very important to measure to
ensure that one is not exposed to too terribly much of it.
The radioactivity of a material decreases over time, as the material decays.
A radioactive decay curve describes this decay. The x-axis measures time,
and the y-axis measures the amount of activity produced by the radioactive
sample. 'Activity' is defined as the rate at which the nuclei within the sample
undergo transititions - put simply, this measures how much radiation is
emitted at any one point in time. The measurement of activity is called
the Becquerel (Bq). Here is a sample radioactive decay curve:

(Click on the pictures to view full-sized images)

Now here's the problem we'd like to solve. Let's say Sarina has moved into a
new apartment. Unbeknownst to her, there is a sample of Cobalt-60 inside
one of the walls of the apartment. Initially that sample had 10 MBq of
activity, but she moves in after the sample has been there for 5 years. She
lives in the apartment for 6 years, then leaves. How much radiation was she
exposed to?
We can actually figure this out using the radioactive decay curve from above.
What we want to know is hertotal radiation exposure from year 5 to year 11.

Total radiation exposure corresponds to the area between the two green
lines at time = 5 and time = 11, and under the blue radioactive decay curve.
This should make intuitive sense - if the x axis measures time, and the y
axis measures activity, then the area under the curve measures (time *
activity) = MBq*years, or, approximately the total number of MBq Sarina
was exposed to in her time in the radioactive apartment (technically, this
result is the combination of gamma rays and beta particles she was exposed
to, but this gets a bit complicated, so we'll ignore it. Sorry, physicists!).

So far, so good. But, how do we calculate this? Unlike a simple shape - say a
square, or a circle - we have no easy way to tell what the area under this
curve is.
However, we have learned a technique that can help us here
- approximation. Let's use an approximation algorithm to estimate the area
under this curve! We'll do so by first splitting up the area into equally-sized
rectangles (in this case, six of them, one rectangle per year):

Once we've done that, we can figure out the area of each rectangle pretty
easily. Recall that the area of a rectangle is found by multiplying the height
of the rectangle by its width. The height of this rectangle:

is the value of the curve at 5.0. If the curve is described by a function, f , we


can obtain the value of the curve by asking for f(5.0) .
f(5.0) = 5.181

The width of the rectangle is 1.0. So the area of this single rectangle
is 1.0*5.181 = 5.181 . To approximate how much radiation Sarina was
exposed to, we next calculate the area of each successive rectangle and then
sum up the areas of each rectangle to get the total. When we do this, we
find that Sarina was exposed to nearly 23 MBq of radiation (technically, her
apartment was bombarded by 23e6 * 3.154e6 = 7.25e13 neutrons, for
those interested...).
Whether or not this will kill Sarina depends exactly on the type of radiation
she was exposed to (see this linkwhich discusses more about the ways of
measuring radiation). Either way, she should probably ask her landlord for a
substantial refund.
In this problem, you are asked to find the amount of radiation a person is
exposed to during some period of time by completing the following function:

def radiationExposure(start, stop, step):


'''
Computes and returns the amount of radiation exposed
to between the start and stop times. Calls the
function f (defined for you in the grading script)
to obtain the value of the function at any point.
start: integer, the time at which exposure begins
stop: integer, the time at which exposure ends
step: float, the width of each rectangle. You can assume that
the step size will always partition the space evenly.
returns: float, the amount of radiation exposed to
between start and stop times.
'''

To complete this function you'll need to know what the value of the
radioactive decay curve is at various points. There is a function f that will be
defined for you that you can call from within your function that describes the
radioactive decay curve for the problem. Do not define f in your code.
You should implement this function on your own machine. Open a new
Canopy Python file and title it "radiationExposure.py". Complete your work
inside this file. Test your code well in Canopy, and when you are convinced it
is correct, cut and paste your definition into this tutor window.
Test Cases to Test Your Code With. Be sure to test these on your own
machine - and that you get the same output! - before running your
code on this webpage!
Click to See Test Cases

Assume that the curve function f is defined as follows:


def f(x):
import math
return 10*math.e**(math.log(0.5)/5.27 * x)

Test case 1:
>>> radiationExposure(0, 5, 1)
39.10318784326239

Test case 2:

>>> radiationExposure(5, 11, 1)


22.94241041057671

Test case 3:
>>> radiationExposure(0, 11, 1)
62.0455982538

Test case 4:
>>> radiationExposure(40, 100, 1.5)
0.434612356115

A note on these test cases: Your answers should be within 0.01 of the
correct answer.
A Mathematical Note of Interest

The technique of finding the area under a curve is called integration. This
comes to us from calculus. What we're doing in this problem is an
approximation of finding the integral under the curve using the summation
of rectangular areas known as a Riemann integral.
This approximation becomes more and more correct the smaller the width of
the rectangles becomes.
So there you have it. If you've not learned calculus before, you've now got
one of the basics - integration - covered!

The code you paste into the following box should not specify the values for
the variables balance or annualInterestRate - our test code will define those
values before testing your submission.

Vous aimerez peut-être aussi