Under construction

Still being built. Things may change

Rate of Return

Time limit1sMemory limit128 MB

Summary
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 ii, an amount of aa dollars added at the beginning of month mm is worth a(1+i)M−m+1a(1+i)^{M-m+1} at the end of month MM.

Jill invested NN times, adding aka_k dollars at the beginning of month mkm_k, and at the end of month MM the fund is worth VV dollars. Find the monthly rate ii that satisfies

∑k=1Nak(1+i)M−mk+1=V\sum_{k=1}^{N} a_k (1+i)^{M - m_k + 1} = V

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 100(1+i)4+100(1+i)2=210100(1+i)^4 + 100(1+i)^2 = 210, whose root is about 0.0163517952340.016351795234.

Input

The input contains several cases. Each case begins with an integer NN (1≤N≤121 \le N \le 12), the number of times Jill added money. After it come N+1N + 1 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 NN pairs give the month and the amount of each investment, in increasing order of month number. The last pair gives the month MM of the report and the value VV 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 −1-1 follows the last case.

Output

For each case print one line in the form Case k: r, where kk is the case number counting from 1 and rr 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.

Examples1

  1. Example 1

    Input
    2   1   100.00    3
    100.00    4   210.00
    
    3
    1 100.00
    2 50.00
    5 200.00
    7 358.41
    
      -1
    
    Expected output
    Case 1: 0.01635
    
    Case 2: 0.00520