A travelling fair spends injection thousand crowns in the village of Dunmore: on food, rooms and repairs. That is round 1 of spending. The villagers who received it spend a share c1 (their marginal propensity to consume) of what they received in the village shops: round 2 is c1 times round 1. The shopkeepers then spend c1 of what they received, and so on: each round is c1 times the previous round.
The village accountant follows the rounds one by one and stops at the first round that is below threshold (that round and all later ones are not counted; a round exactly equal to threshold is counted). If even round 1 is below the threshold, nothing is counted.
If every round were counted forever, the total would be the infinite sum
Write spending_rounds(c1, injection, threshold) that returns a tuple (rounds, total, full_total): how many rounds were counted, the sum of the counted rounds, and the infinite sum.
Press Run with plot of the running totals to see them creep up towards full_total.
Examples
Input: c1 = 0.5, injection = 100, threshold = 12.5
Output: (4, 187.5, 200.0)
Explanation: the rounds are 100, 50, 25, 12.5, 6.25, ... Round 4 equals the threshold,
so it is counted; round 5 is below it, so counting stops. 100 + 50 + 25 + 12.5 = 187.5.
Input: c1 = 0.8, injection = 50, threshold = 20
Output: (5, 168.08, 250.0)
Explanation: 50 + 40 + 32 + 25.6 + 20.48 = 168.08; the next round, 16.384, is below 20.
Constraints
- compute each round by multiplying the previous one by
c1 - answers are compared with a tolerance of
1e-6; do not round
Goals
- Simulate the multiplier as successive rounds of re-spending
- Stop a loop at a stated threshold, including the boundary case
- Compare a partial sum with the closed form of a geometric series