Problem 1: Roots
Uncomment the line
import math and use
math.sqrt to solve the following problem.
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:
You will need to use the quadratic equation to solve for the roots:
Problem 2: Reciprocals
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:
Problem 3: Triangular Numbers
range function to help solve this problem.
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:
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.
Do not use the triangular number formula inside your for loop. The formula is only provided to check your answers.
Problem 4: Factorial
Your answer for problem 4 should be similar to problem 3.
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
Similarly to problem 3, your solution should be able to correctly calculate any factorial just by changing a variable.
Problem 5: Multiple Factorials
You will need to use a nested for loop to solve this problem.
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
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
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
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.
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.
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:
Similar to problems 3 - 5, you should assign a variable (e.g.,
n = 10) to determine the number of fractions to add.