Sequences
Sequences (lesson 1.5.1)
0 of 7 correct
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
How to type answers
- Powers with
^:x^7,2^(1/3),a^-2. - Multiply with
*or by writing side by side:3x^2,2(x+1). ln(x)is the natural log,log(x, 2)is log base 2,eis Euler's number,sqrt(x)is a square root.sin(x),cos(x)andtan(x)take radians; writepifor π andsin(x)^2for sin²x.- Your first answer to each question is saved: it counts towards your score and brings the question back for review.
- Exact answers like
ln(20)/ln(3)are best. Decimals need about 4 significant figures.
Finite series
Arithmetic and geometric series (lesson 1.5.2)
0 of 7 correct
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
How to type answers
- Powers with
^:x^7,2^(1/3),a^-2. - Multiply with
*or by writing side by side:3x^2,2(x+1). ln(x)is the natural log,log(x, 2)is log base 2,eis Euler's number,sqrt(x)is a square root.sin(x),cos(x)andtan(x)take radians; writepifor π andsin(x)^2for sin²x.- Your first answer to each question is saved: it counts towards your score and brings the question back for review.
- Exact answers like
ln(20)/ln(3)are best. Decimals need about 4 significant figures.
Infinite series
Infinite series (lesson 1.5.3)
0 of 7 correct
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
How to type answers
- Powers with
^:x^7,2^(1/3),a^-2. - Multiply with
*or by writing side by side:3x^2,2(x+1). ln(x)is the natural log,log(x, 2)is log base 2,eis Euler's number,sqrt(x)is a square root.sin(x),cos(x)andtan(x)take radians; writepifor π andsin(x)^2for sin²x.- Your first answer to each question is saved: it counts towards your score and brings the question back for review.
- Exact answers like
ln(20)/ln(3)are best. Decimals need about 4 significant figures.
Coding exercise
Geometric sums, finite and infinite
Write three functions.
geometric_sum(a, r, n)returns , the sum of terms. Handle , where the formula divides by zero, and , an empty sum.geometric_sum_infinite(a, r)returns . RaiseValueErrorwhen , because the series diverges.terms_needed(a, r, tol)returns the smallest number of terms whose partial sum is withintolof the infinite sum. Check the ratio first and raiseValueErrorwhen : there the gap never shrinks, so a loop would run for ever.
The tests check terms_needed against partial sums added up directly, including a negative ratio and a negative first term.
See the tests
def test_doubling():
"""5 + 10 + 20 + … to 10 terms is 5115"""
expect_close(geometric_sum(5, 2, 10), 5115, what="geometric_sum(5, 2, 10)")
def test_matches_direct_sum():
"""The formula matches a direct sum: 1000 at ratio 1.05, 7 terms"""
expect_close(geometric_sum(1000, 1.05, 7), sum(1000 * 1.05**k for k in range(7)), what="geometric_sum(1000, 1.05, 7)")
def test_ratio_one():
"""With ratio 1, every term is a: 7 terms of 3 make 21"""
expect_close(geometric_sum(3, 1, 7), 21, what="geometric_sum(3, 1, 7)")
def test_no_terms():
"""Zero terms make an empty sum, 0"""
expect_close(geometric_sum(4, 0.5, 0), 0, what="geometric_sum(4, 0.5, 0)")
def test_infinite():
"""1/2 + 1/4 + … adds to 1"""
expect_close(geometric_sum_infinite(0.5, 0.5), 1.0, what="geometric_sum_infinite(0.5, 0.5)")
def test_infinite_negative_ratio():
"""5 − 5/2 + 5/4 − … adds to 10/3"""
expect_close(geometric_sum_infinite(5, -0.5), 10 / 3, what="geometric_sum_infinite(5, -0.5)")
def test_diverges():
"""A ratio of 2 or −1 diverges: raise ValueError"""
for r in (2, -1):
try:
geometric_sum_infinite(1, r)
except ValueError:
continue
raise AssertionError(f"expected ValueError for r = {r}")
def _gap(a, r, n):
"""How far the partial sum of n terms, added up directly, is from a / (1 - r)."""
return abs(a / (1 - r) - sum(a * r**k for k in range(n)))
def test_terms_needed_halves():
"""1/2 + 1/4 + … needs 20 terms to come within 0.000001 of 1"""
expect_equal(terms_needed(0.5, 0.5, 1e-6), 20, what="terms_needed(0.5, 0.5, 1e-6)")
def test_terms_needed_slow_ratio():
"""1 + 0.9 + 0.81 + … needs 66 terms to come within 0.01 of 10"""
expect_equal(terms_needed(1, 0.9, 0.01), 66, what="terms_needed(1, 0.9, 0.01)")
def test_terms_needed_is_smallest():
"""For a negative ratio and a negative first term, n terms are close enough and n − 1 are not"""
for a, r, tol in [(1, -0.5, 0.001), (-2, 0.6, 0.001), (3, 0.25, 1e-5)]:
n = terms_needed(a, r, tol)
assert isinstance(n, int) and n >= 1, f"terms_needed({a}, {r}, {tol}) should be a whole number of terms, got {n!r}"
assert _gap(a, r, n) < tol, f"terms_needed({a}, {r}, {tol}) returned {n}, but {n} terms still miss by {_gap(a, r, n):.3g}"
assert _gap(a, r, n - 1) >= tol, f"terms_needed({a}, {r}, {tol}) returned {n}, but {n - 1} terms are already close enough"
def test_terms_needed_diverges():
"""A ratio of 1.5 has no sum to approach: raise ValueError"""
try:
terms_needed(1, 1.5, 0.01)
except ValueError:
return
raise AssertionError("expected ValueError for r = 1.5")