Problem Description
Given is the arithmetic-geometric sequence u(k) = (900-3k)rk-1.
Let s(n) = Σk=1…nu(k).
Find the value of r for which s(5000) = -600,000,000,000.
Give your answer rounded to 12 places behind the decimal point.
Analysis
Simple problem using a binary search and keep reducing the range until we reach the target. The first thing we did was divide the equation by 3 as that seemed obvious and thought it might help speed things up. So the target became -200,000,000,000 and the equation (300-k) * r**(k-1). Did save a multiplication, but wasn’t really necessary.
Solution
Runs < 1 second in Python.
sn = 5000
sm = -2 * 10**11
s, r, d = 0, 1, 0.1
while abs(s – sm) > 1:
s = sum((300 – k) * r**(k-1) for k in range(1, sn+1))
r += d if s>sm else -d
d /= 2
print ‘Answer to PE 235 = %.12f’ % r
Comments
Worked first time!
Project Euler 235 last updated
Discussion
No comments yet.