Homework 1: Welcome to Python

Due: 11:00pm on Friday January 15, 2021. Submit via Gradescope. (REQUIRED survey)

Learning Objectives:

You will make use of all of these skills throughout the quarter!

Python is an easy-to-use but powerful programming language that is particularly well-suited to quickly creating small programs.

VS Code is more than just a code editor; it is a simple but powerful IDE, or "integrated development environment", which lets you edit, run, navigate, and debug Python code.

Download the file hw1.py [Tip: To download, right click this link and select "Save link as" or if you click through to see the file in your browser, just right click and select "Save as" or "Save Page as" or whatever wording your browser uses.]

Note: you should NOT make use of any features that we have not discussed in class, unless it is specifically endorsed in the assignment specification. (Such as math.sqrt for problem 1 of this homework).

Note: you should NOT post any of your code onto the discussion board or let other students see your code. If you have questions or problems with your code, try making a private discussion post, sending an email to staff, or attending office hours.

For problems in this homework, you will edit your local copy of the hw1.py file. You may do your work by editing and running hw1.py (we recommend this approach), or by typing code in a Python interpreter and copying it into the appropriate part of hw1.py when you are satisfied with it. When you have completed this homework, running the hw1.py file should compute the answers to all the problems, and print the following output (with the ____ replaced by the values you calculated). Please make your output match the following exactly.

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: ____

Don't forget to include documentation (source code comments, on a line starting with #) as appropriate in file hw1.py.

Tips for Formatting Output

Let's say you're trying to print the following line of output:
1 plus 1 equals 2
Here's some Python code that will do that:
n = 1
print(str(n) + " plus " + str(n) + " equals " + str(n + n))
You can also use:
n = 1
print(str(n), "plus", str(n), "equals", str(n + n))
Note that in the first case, I'm explicitly inserting spaces when I want them, but in the second case, the commas add spaces for me automatically. In the second case it is not necessary to call str() on n and n + n. The following would also work fine and be simpler.
n = 1
print(n, "plus", n, "equals", n + n)

You can print a blank line by calling just print() (and giving it nothing to print).

Checking your work

You'll be writing code to do several common mathematical operations for this assignment. You'll likely recognize most of them. This will be your first time writing them in code though, so it's important that you check your work and verify your code is correct.

For each problem do some examples by hand (on pen-and-paper for instance) and make sure your code is calculating the same values. To test this, you can always change the values of your variables to something simpler, just be sure to change them to the values requested in this document before you turn in your final code.

Problem 1: Roots

Compute and print both roots of the quadratic equation with the smaller root first: 3 x2-5.86 x+ 2.5408. You can assume neither answer will be a complex number.

Hint: recall that the roots of a quadratic equation ax2+bx+c are x=

−b ± √ b2 − 4ac 
          2a          

Hint: use the math.sqrt function to compute the square root. (If you are using the Python interpreter, you need to first do import math. This already appears as a commment in the hw1.py file that you will be editing, so you just need to uncomment that line to have everything set up.)

Problem 2: Reciprocals

Use a for loop to print the decimal representations of 1/2, 1/3, ..., 1/10, one on each line.

Problem 3: Triangular numbers

Use a for loop to compute the 10th triangular number. The nth triangular number is defined as 1+2+3+...+n. (You can also compute the nth triangular number as n*(n+1)/2. Use this formula to double-check that your loop is correct.)

Hint: This outline is an almost-complete solution. You only have to replace each ellipsis by an expression.

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)

Your code should be able to correctly calculate the 11th, 12th, or any other triangular number just by changing the first line to set n to 11, 12, or any other number. Be sure the code you submit calculates the triangle number for n = 10. Using the range function should help you accomplish this.

Problem 4: Factorial

Use a for loop to compute 10!, the factorial of 10. Recall that the factorial of n is 1*2*3*...*n. [Note: you may not use the math.factorial() function for this problem or any other problem in this homework. You should also NOT use recursion. (If you do not know what that means, do not worry because you should not use it :-)]

Hint: Your answer will be similar to your answer to "Problem 3: Triangular numbers".

As in Problem 3, your code should be able to calculate 11!, 12!, or any other number's factorial just by changing the first line to set n to 11, 12, or any other number. Be sure the code you submit calculates the factorial for n = 10.

Problem 5: Multiple factorials

Write code to print the first 10 factorials, in reverse order. In other words, write code that prints 10!, then prints 9!, then prints 8!, ..., then prints 1!. Its literal output will be:

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

The first line of your solution should assign a variable num_lines to 10. Then, as in Problems 3 and 4, the rest of the code should print the correct number of lines and correct factorial on each line for values other than 10 just by setting num_lines to a different value.

Hint: Use two nested for loops.
The outer loop sets the value of n to the values num_lines, num_lines-1, num_lines-2, ..., 1, in succession.
Then, the body of that loop is itself a loop — exactly your solution to "Problem 4: Factorial", without the first line n = 10 that hard-codes the value of n.

Problem 6: Sums of reciprocals of factorials

Compute the following value:
  1 + 1/1! + 1/2! + 1/3! + 1/4! + ... + 1/10!
The value should be close to e (≈ 2.71828), the base of the natural logarithms.

Like with the previous few problems, you should use a variable n = 10 to determine how many fractions to add. In theory, increasing this number should get you closer to the true value of e.

Hint: The easiest way to solve this is with two nested for loops. It is possible, but tricky, to compute this using only one for loop. That is not necessary for this assignment.

Hint: 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.

Hint: 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).

Submit your work

You are almost done!

Now you're done!