Rate of Return
Time limit1sMemory limit128 MB
Find the monthly rate that makes the compounded deposits equal the reported fund value.
- Level
Medium4 of 10
- Topics
- Binary search, Math
- Solved
- No attempts yet
Problem
Jill has been putting money into a mutual fund for years. Her income went up and down, so the amount she invested changed every time, and she did not invest on a fixed schedule. She does have a complete record of when she invested and how much.
The fund sends her a report now and then with the current value of the account. Jill wonders whether the same money would have done better in a savings account paying a fixed rate. To decide that she first needs the fixed rate that would have produced the same value. Compute that rate.
Assume Jill added money only at the beginning of a month and that every month has the same length. Interest on a savings account is paid at the end of each month and compounds monthly, so with a monthly rate of , an amount of dollars added at the beginning of month is worth at the end of month .
Jill invested times, adding dollars at the beginning of month , and at the end of month the fund is worth dollars. Find the monthly rate that satisfies
For instance, if Jill adds $100 at the beginning of month 1 and another $100 at the beginning of month 3, and the fund is worth $210 at the end of month 4, the equation becomes , whose root is about .
Input
The input contains several cases. Each case begins with an integer (), the number of times Jill added money. After it come pairs, each pair an integer followed by a real number. The integer is a month number (1 or larger) and the real number is a dollar amount. The first pairs give the month and the amount of each investment, in increasing order of month number. The last pair gives the month of the report and the value of the fund at the end of that month.
One or more whitespace characters (spaces, tabs, line breaks) separate the numbers, so a pair may be split across two lines.
A single integer follows the last case.
Output
For each case print one line in the form Case k: r, where is the case number counting from 1 and is the equivalent fixed monthly rate rounded to five digits after the decimal point. Print one blank line between consecutive cases.
The rate is at least 0 and at most 1. No case has an exact rate that lands on a rounding boundary, so the five printed digits are determined uniquely.