Skip to content
The Lumenary

Each worksheet prints cleanly on A4 or Letter. Answer keys are on a separate page.

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.3 is False in Python, and use exact fractions instead
  1. 1.

    What is the value of x after these three lines run?

    x = 3
    x = x + 4
    x = 2 * x
    
  2. 2.

    What does the Python line count = count + 1 do?

    • A.It increases the value stored in count by 11
    • B.Nothing, because the equation c=c+1c = c + 1 has no solution
    • C.It causes an error, because count appears on both sides
    • D.It tests whether count equals count + 1
  3. 3.

    Write the binary number 1101012110101_2 in decimal.

  4. 4.

    Write 2626 in binary, without leading zeros. (Type just the digits, for example 101.)

  5. 5.

    What does this program print?

    total = 0
    for i in range(1, 6):
        if i % 2 == 1:
            total = total + i
    print(total)
    
  6. 6.

    In Python, 0.1 + 0.2 == 0.3 evaluates to False. Why?

    • A.0.10.1 and 0.20.2 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. 7.

    The uniqueness proof for binary representations starts: “the last binary digit of nn must be the remainder of nn on division by 22”. Why is that true?

    • A.Every other digit stands for an even amount (2,4,8,…2, 4, 8, \dots), so only the last digit decides whether nn 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 11
  8. 8.

    Write to_binary(n) that returns the binary representation of a whole number n as a string without leading zeros, using repeated division by 2 (don't use Python's built-in bin). to_binary(0) should be "0".

    Computational — Practice

  9. 9.

    This function is meant to compute 12+22+⋯+n21^2 + 2^2 + \dots + n^2. It runs without any error — but it is wrong. Find and fix the bug. Then check your fixed version against the formula n(n+1)(2n+1)6\tfrac{n(n + 1)(2n + 1)}{6}.

    Computational — Practice

  10. 10.

    In the Lean section, which of these machine-checked statements tells you that the sum 1+2+⋯+10001 + 2 + \dots + 1000 equals 1000⋅10012\tfrac{1000 \cdot 1001}{2}?

    • 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
Code: Apache-2.0 · Content: CC BY-SA 4.0