The Lumenary · Grades 6–8
Programming for mathematics
Name: ______________________
Date: ______________________
You will learn to
- Write programs with variables, loops, conditionals and functions to compute sums, tables and sequences
- Explain the difference between assignment (
=in a program) and equality in mathematics - Use a program to test a conjecture on many cases, and explain why this is evidence but not proof, while one counterexample is a disproof
- Convert between decimal and binary by hand and in code, and explain why every whole number has exactly one binary representation without leading zeros
- Explain why
0.1 + 0.2 == 0.3isFalsein Python, and use exact fractions instead
- 1.
What is the value of
xafter these three lines run?x = 3 x = x + 4 x = 2 * x - 2.
What does the Python line
count = count + 1do?- A.It increases the value stored in
countby - B.Nothing, because the equation has no solution
- C.It causes an error, because
countappears on both sides - D.It tests whether
countequalscount + 1
- A.It increases the value stored in
- 3.
Write the binary number in decimal.
- 4.
Write in binary, without leading zeros. (Type just the digits, for example
101.) - 5.
What does this program print?
total = 0 for i in range(1, 6): if i % 2 == 1: total = total + i print(total) - 6.
In Python,
0.1 + 0.2 == 0.3evaluates toFalse. Why?- A. and cannot be stored exactly in binary, so the computer adds two slightly wrong numbers
- B.It is a bug in Python that will be fixed in a later version
- C.Python rounds every decimal to a whole number
- D.Python adds from right to left, so the order is wrong
- 7.
The uniqueness proof for binary representations starts: “the last binary digit of must be the remainder of on division by ”. Why is that true?
- A.Every other digit stands for an even amount (), so only the last digit decides whether is even or odd
- B.Because we define the last digit to be the remainder
- C.Because the last digit is the smallest digit
- D.Because binary numbers always end in
- 8.
Write
to_binary(n)that returns the binary representation of a whole numbernas a string without leading zeros, using repeated division by 2 (don't use Python's built-inbin).to_binary(0)should be"0".Computational — Practice
- 9.
This function is meant to compute . It runs without any error — but it is wrong. Find and fix the bug. Then check your fixed version against the formula .
Computational — Practice
- 10.
In the Lean section, which of these machine-checked statements tells you that the sum equals ?
- A.
gauss_formula : 2 * sumTo n = n * (n + 1)(proved by induction) - B.
loop_agrees_tested : ∀ n, n < 40 → sumFor n = sumTo n(proved by running both programs) - C.
sum_for_examples : sumFor 10 = 55 ∧ sumFor 100 = 5050 - D.
sum_odds_eq_square : sumOdds n = n * n
- A.