Rational Sum
Write a python program that prompts the user for a positive integer n and prints the sum of the rational numbers:
$$ \sum_{i = 1}^{n} \frac{1}{i} = \frac{1}{1} + \frac{1}{2} + \ldots +
\frac{1}{n} $$
Example
Enter a positive integer: 10
The rational sum is 2.9289682539682538
Factorial
Write a Python program that prompts the user for a positive integer and prints the factorial of the specified number. The factorial of a number n can be computed using the equation:
n!=n × (n − 1)×…×1
Example
Enter a positive integer: 10
The factorial is 3628800
Leibniz Approximation
The mathematical constant π is an irrational number that represents the ratio between a circle’s diameter and its circumference. The first algorithm designed to approximate π was developed by Archimedes in 250 BC, and was able to approximate its value within 3 significant figures. Today we know that there are an infinite number of digits in π, so lets use a little more sophisticated approach to estimate its value.
Details
Write a Python program that uses the Leibniz aprroximation to calculate and print an esitmate for the value of π. The program should prompt the user to enter the number of terms to use to use when calculating the estimate.
The Leibniz approximation relies on the fact that:
$$ \frac{\pi}{4} = \frac{1}{1} - \frac{1}{3} + \frac{1}{5} - \frac{1}{7} + \ldots $$
Which can be rewritten to approximate the true value of π:
$$ \pi = \frac{4}{1} - \frac{4}{3} + \frac{4}{5} - \frac{4}{7} + \ldots $$
Example
Enter the number of terms: 1000
the approximate value of pi is 3.140592653839794
Hint
- You will need a for loop that uses the accumulator pattern to accomplish this goal. Create a variable (defaulted value to 0) before the for loop. Then you just need to update the value of the variable inside the for loop.
- As you progress through your for loop, the denominator of your fraction increases by a factor of two. Some simple algebra will reveal that during loop iteration i, the denominator is: $$(2
- 1$$
- You need to alternate adding and subtracting. However, subtracting is the same as adding a negative number. So we can accomplish this by multiplying every other fraction by -1. The easy way to do this is to compute:
−1i
Where (i) is your loop variable. - The accumulator be the sum of all of the terms in the sequence, so you only need to add one term to the accumulator during each loop iteration.
Fibonacci
The Fibonacci sequence is a sequence of numbers that looks like:
1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811, 514229, 832040, …
Each number in the sequence, after the first two, is the sum of the previous two numbers in the sequence.
Details
Write a Python program that prints the Fibonacci sequence. The program should prompt the user for the length of the series to print.
Example
Enter the sequence length: 8
1 1 2 3 5 8 13 21
Hint
- Because the program should work for any length sequence, you will need a loop to compute each of the numbers in the sequence.
- What is different between this and other uses of an accumulator is that it needs the previous two iterations to compute the next iteration. To do this, keep two variables, one for the previous value and one for the one before the previous.
- Pay attention to how the program will work for small numbers. If the user enters 1, the loop should run 0 times and print 1. If the user enters 2, the loop should run 1 time. But what are the two previous values when computing the second number in the sequence? Hint: 0 and 1.
Submission
Please show your source code and run your programs for the instructor or lab assistant. Only a programs that have perfect functionality will be accepted as complete.