#### problem1 (Score: 1.5 / 10.0)

Before you turn this problem in, make sure everything runs as expected. First, restart the kernel (in the menubar, select Kernel$\rightarrow$Restart) and then run all cells (in the menubar, select Cell$\rightarrow$Run All).

Make sure you fill in any place that says `YOUR CODE HERE` or "YOUR ANSWER HERE", as well as your name and collaborators below:

In [1]:

    NAME = "Ben Bitdiddle"
    COLLABORATORS = "Alyssa P. Hacker"

For this problem set, we'll be using the Jupyter notebook:

## Part A (2 points)¶

Write a function that returns a list of numbers, such that $x_i=i^2$, for $1\leq i \leq n$. Make sure it handles the case where $n<1$ by raising a `ValueError`.

In [2]:
Student's answer(Top)

    def squares(n):
        """Compute the squares of numbers from 1 to n, such that the
        ith element of the returned list equals i^2.

        """
        if n < 1:
            raise ValueError
        s = []
        for i in range(n):
            s.append(i**2)
        return s

Your function should print `[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]` for $n=10$. Check that it does:

In [3]:

    squares(10)

Out[3]:

    [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

In [4]:
Grade cell: `correct_squares` Score: 0.0 / 1.0 (Top)

    """Check that squares returns the correct output for several inputs"""
    assert squares(1) == [1]
    assert squares(2) == [1, 4]
    assert squares(10) == [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
    assert squares(11) == [1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121]

    ---------------------------------------------------------------------------
    AssertionError                            Traceback (most recent call last)
    Cell In[4], line 2
          1 """Check that squares returns the correct output for several inputs"""
    ----> 2 assert squares(1) == [1]
          3 assert squares(2) == [1, 4]
          4 assert squares(10) == [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

    AssertionError:

In [5]:
Grade cell: `squares_invalid_input` Score: 1.0 / 1.0 (Top)

    """Check that squares raises an error for invalid inputs"""
    try:
        squares(0)
    except ValueError:
        pass
    else:
        raise AssertionError("did not raise")

    try:
        squares(-4)
    except ValueError:
        pass
    else:
        raise AssertionError("did not raise")

## Part B (1 point)¶

Using your `squares` function, write a function that computes the sum of the squares of the numbers from 1 to $n$. Your function should call the `squares` function -- it should NOT reimplement its functionality.

In [6]:
Student's answer(Top)

    def sum_of_squares(n):
        """Compute the sum of the squares of numbers from 1 to n."""
        total = 0
        s = squares(n)
        for i in range(len(s)):
            total += s[i]
        return total

The sum of squares from 1 to 10 should be 385. Verify that this is the answer you get:

In [7]:

    sum_of_squares(10)

Out[7]:

    285

In [8]:
Grade cell: `correct_sum_of_squares` Score: 0.0 / 0.5 (Top)

    """Check that sum_of_squares returns the correct answer for various inputs."""
    assert sum_of_squares(1) == 1
    assert sum_of_squares(2) == 5
    assert sum_of_squares(10) == 385
    assert sum_of_squares(11) == 506

    ---------------------------------------------------------------------------
    AssertionError                            Traceback (most recent call last)
    Cell In[8], line 2
          1 """Check that sum_of_squares returns the correct answer for various inputs."""
    ----> 2 assert sum_of_squares(1) == 1
          3 assert sum_of_squares(2) == 5
          4 assert sum_of_squares(10) == 385

    AssertionError:

In [9]:
Grade cell: `sum_of_squares_uses_squares` Score: 0.5 / 0.5 (Top)

    """Check that sum_of_squares relies on squares."""
    orig_squares = squares
    del squares
    try:
        sum_of_squares(1)
    except NameError:
        pass
    else:
        raise AssertionError("sum_of_squares does not use squares")
    finally:
        squares = orig_squares

## Part C (1 point)¶

Using LaTeX math notation, write out the equation that is implemented by your `sum_of_squares` function.

Student's answer Score: 0.0 / 1.0 (Top)

$\sum_{i=0}^n i^2$

## Part D (2 points)¶

Find a usecase for your `sum_of_squares` function and implement that usecase in the cell below.

In [10]:
Student's answer Score: 0.0 / 2.0 (Top)

    # YOUR CODE HERE
    raise NotImplementedError()

    ---------------------------------------------------------------------------
    NotImplementedError                       Traceback (most recent call last)
    Cell In[10], line 2
          1 # YOUR CODE HERE
    ----> 2 raise NotImplementedError()

    NotImplementedError:

Student's task Score: 0.0 / 4.0 (Top)

## Part E (4 points)¶

State the formulae for an arithmetic and geometric sum and verify them numerically for an example of your choice.

$\sum x^i = \frac{1}{1-x}$