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:
3�2−5.86�+2.54083x2−5.86x+2.5408
You will need to use the quadratic equation to solve for the roots:
�=−�±�2−4��2�x=2a−b±b2−4ac
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,...,11021,31,...,101
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:
�(�+1)22n(n+1)
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
1∗2∗3∗...∗�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+1!1+2!1+3!1+...+10!1
Similar to problems 3 - 5, you should assign a variable (e.g., n = 10
) to determine the number of fractions to add.