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

Tip

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

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

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

Tip

Use the range function to help solve this problem.

Warning

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

Use a for loop to compute the 10th triangular number. The nth triangular number is calculated by

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.

Problem 4

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.

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

Info

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

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.

Problem 6

Info

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

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 to determine how many number of fractions to add. Don’t try to incorporate adding 1 into your loop as that will unnecessarily complicate your solution. Instead, add it to the result either prior or after running your for loop.

Quality

Info

For the documentation portion of the quality guide, you should place 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

Warning

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

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

HW1 - Homework 1

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

Submit on Gradescope