Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

In this homework, we will write a program that prints square roots, fractions, triangular numbers, factorials, and the approximate value of ee.

By the end of this assignment, students will feel more comfortable:

  1. Writing Python code using variables and loops to solve arithmetic problems.

  2. Running Python programs using the interactive Python console in JupyterHub.

  3. Printing results to match a specification, including whitespace.

Problem 1: Roots

Write code that calls math.sqrt to print the roots of the quadratic equation:

3x25.86x+2.5408=03x^2 - 5.86x + 2.5408 = 0

You will need to use the quadratic equation to solve for the roots:

x=b±b24ac2ax = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}

Print the two possible values for xx such that they replace the ____ (blanks) in the template below in ascending order: the smaller root should appear before the larger root.

Problem 1 solution follows:
Root 1: ____
Root 2: ____

Problem 2: Reciprocals

Write code that uses a for loop to print the fractions:

12,13,...,110\frac{1}{2}, \frac{1}{3}, ..., \frac{1}{10}

As in problem 1, print values such that they replace the ____ (blanks) in the template below.

Problem 2 solution follows:
1/2: ____
1/3: ____
1/4: ____
1/5: ____
1/6: ____
1/7: ____
1/8: ____
1/9: ____
1/10: ____

Problem 3: Triangular numbers

Write code that prints the 10th triangular number in two ways:

  1. Using a for loop over a range to add the integers from 1 through 10.

  2. Using a mathematical formula:

    N(N+1)2\frac{N(N + 1)}{2}

The main.py file includes some sample code that to help you get started. Replace the ... (ellipsis) placeholders with your own code. The code should calculate the 11th, 12th, etc. triangular number by changing the variable n for any integer greater than 0.

main.py
n = 10
triangular = 0
for i in ...:
    triangular = ...
print("Triangular number", n, "via loop:", triangular)
print("Triangular number", n, "via formula:", n * (n + 1) / 2)

As in problem 1 and 2, print values such that they replace the ____ (blanks) in the template.

Problem 3 solution follows:
Triangular number 10 via loop: ____
Triangular number 10 via formula: ____

Problem 4: Factorial

Write code that uses a for loop to print 10!10!, the factorial of 10. The factorial of a number, nn, is given by 1×2×3××n1 \times 2 \times 3 \times \cdots \times n

As in problem 3, the code should correctly calculate any factorial just by changing a variable.

Problem 4 solution follows:
10!: ____

Problem 5: Multiple factorials

Write code that uses a nested for loop to print the first 10 factorials in descending order: 10!,9!,...,1!10!, 9!, ..., 1!

The first line of code should begin by assigning a variable num_lines to 10. Then the rest of the code should print the following output.

Problem 5 solution follows:
10!: 3628800
9!: 362880
8!: 40320
7!: 5040
6!: 720
5!: 120
4!: 24
3!: 6
2!: 2
1!: 1

As in problems 3 and 4, the code should work for any number just by changing num_lines.

Problem 6: Approximating ee

Write code that uses a nested for loop to compute the value

1+11!+12!+13!+...+110!1 + \frac{1}{1!} + \frac{1}{2!} + \frac{1}{3!} + ... + \frac{1}{10!}
Problem 6 solution follows:
e: ____

As in problems 3 through 5, the code should work for any number of fractions to add.

Code quality

Run our linter (automated code style checker) in the Python console with the expression !flake8. Edit the file and save your changes after addressing all reported issues. A successful !flake8 run will print nothing when there are no linting issues to report.

!flake8

Then, review our style guide, paying particular attention to:

Collaboration

If you discuss an assignment with one or more classmates, you must specify with whom you collaborated in a comment at the bottom of your submission. You may discuss with as many classmates as you like, but you must cite all of them in your work. Note that you may not collaborate in a way that is prohibited, even if you cite the collaboration.

At the bottom of your main.py file, state which students or other people (besides the course staff) helped you with the assignment, or that no one did.

Submission

See the output.txt file for a complete example of the expected output with ____ (blanks) instead of actual values. To help you compare, copy-paste the contents of the expected output into the left pane of Diffchecker and your output into the right pane.

To submit: from JupyterHub, open your main.py file, select File | Download, and submit your downloaded main.py file on Gradescope under the assignment Homework: Practical.