Info

In addition to submitting hw1.py on Gradescope, you are also required to submit a reflection survey that asks you how much time you have spent on the assignment, among other questions. These will count for participation points.

Formatting

Warning

Not following this format exactly may lead to points being deducted on your submission.

For this homework, you will need to edit hw1.py so that when it runs, your answers are printed in the following format (replacing ____ with your answer):

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

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

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

Problem 4 solution follows:
10!: ____

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

Problem 6 solution follows:
e: ____

Problem 1: Roots

Tip

Uncomment the line import math and use math.sqrt to solve the following problem.

Tip

If you are using a particular value repeatedly, be sure to store it in a variable.

Compute and print both roots in ascending order of the following quadratic equation:

3x25.86x+2.5408 3x^2-5.86x+2.5408

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

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

Problem 2: Reciprocals

Tip

What do you notice about how the loop is counting, and the denominator of the fractions?

Use a for loop to print the decimal representations of the following fractions, each on their own line:

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

Problem 3: Triangular Numbers

Tip

Use the range function to help solve this problem.

Tip

The code in hw1.py is a nearly complete solution. You only have to replace each ellipsis with an expression.

Use a for loop to compute the 10th triangular number. The nth triangular number is defined as 1+2+3+...+n, or by the formula:

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

We have provided a partial solution to solve this problem in the starter code. Your solution should be able to correctly calculate the 11th, 12th, etc. triangular number by changing the variable n, for any number greater than 0.

Warning

Do not use the triangular number formula inside your for loop. The formula is only provided to check your answers.

Problem 4: Factorial

Info

Your answer for problem 4 should be similar to problem 3.

Warning

Do not use math.factorial() for any problem in this assignment. You should also not use recursion. (And if you don’t know what that means, don’t worry because you can’t use it anyways.)

Use a for loop to compute 10!, the factorial of 10. The factorial of a number, n, can be calculated by

123...n 1 * 2 * 3 * ... * n

Similarly to problem 3, your solution should be able to correctly calculate any factorial just by changing a variable.

Problem 5: Multiple Factorials

Info

You will need to use a nested for loop to solve this problem.

Info

All calculated factorials should be integers. (Note the lack of decimal points in the output below)

Print the first 10 factorials in descending order (10!, 9!, …, 1!).

The first line of your solution should assign a variable num_lines to 10, and then the rest of your solution should print the correct number of lines and factorial on its own line. Similarly to problems 3 and 4, your solution should be able to work for any number just by changing num_lines.

The literal output for this problem will be:

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

Problem 6: Sums of reciprocals of factorials

Info

You will need to use a nested for loop to solve this problem. It is possible, but tricky, to compute this using only one for loop. That is not necessary for this assignment.

Tip

Copy your solution to “Problem 5: Multiple factorials”, then modify it. Rather than printing the factorials, you will add their reciprocals to a running total, then print that total at the end.

Tip

Don’t try to work the very first “1 +” into your loop; do it outside the loops (either at the very beginning or the very end of the outer loop).

Use a for loop to compute the following value:

1+11!+12!+13!+...+110! 1 + \frac{1}{1!} + \frac{1}{2!} + \frac{1}{3!} + ... + \frac{1}{10!}

Similar to problems 3 - 5, you should assign a variable (e.g., n = 10) to determine the number of fractions to add.

Code Quality

Info

For the documentation portion of the quality guide, you should add comments for every problem.

Warning

If you discuss this assignment with one or more classmates, you must specify with whom you collaborated in the header comment in your submission. Otherwise, you may be guilty of academic misconduct and face consequences. Note that you may not collaborate in a way that is prohibited, even if you cite the collaboration.

Your assignment should pass two checks: flake8 and our code quality guidelines. The code quality guidelines are very thorough. For this assignment, the most relevant rules can be found in these sections:

Submission

Tip

To compare your output, we suggest comparing the expected output and the actual output using Diff Checker. To make use of the site, copy the expected output format into the one text window, and your output into the other window. From here, scroll down and click the “find difference” button. The website will tell you if there is a difference between the two. the only difference that shows up should be that instead of having "____", you have the answer to each problem. If you would like, you can copy the expected formatting from this file: hw1format.txt.

Warning

Your file must be named hw1.py in order to be graded correctly.

Warning

The output of your program must match exactly as described in the Formatting section above. You may lose points if it does not.

Submit hw1.py on Gradescope under the assignment Homework 1 and fill out the required reflection survey.

HW1 - Homework 1

Initial Submission by Friday 04/07 at 11:59 pm.

Submit on Gradescope