Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

perform this code in python [Ungraded] 1. Consider the following function definition. For each of the function calls below, write down (i) the values that

perform this code in python [Ungraded] 1. Consider the following function definition. For each of the function calls below, write down (i) the values that the parameters of test take on and (ii) the value that is returned by the function. def test(a = 10, b = 15, c = 20): return (a/b)*c (a) test() (b) test(10.0) (c) test(30) (d) test(40, (e) test(c = (f) test(c = (g) test(30, (h) test(40, 40) 50) 50, c = 40, a = 15) 50, b = 10) 40) (i) test(len("hello"), 1) (j) test(round(-8.678)) 2 Homework 3 October 11, 2018 [Ungraded] 2. Write a program that starts by prompting the user for a positive integer, let us call this n. The program then reads n floating point numbers input by the user (typed one in each line) and outputs the minimum, maximum, and average of these numbers. Here is an example interaction between the program and the user. Enter the positive integer that denote the length of your sequence: 5 Now input your sequence, one number per line. 3.4 4.5 -9.23 3.456 11.8 The minimum is -9.23 The maximum is 11.8 The average is 2.7852 Your program does not need to do any error checking and can assume that the user will correctly follow the programs instructions. [25] 3. The real number e is a constant that appears is many mathematical problems, such as those involving growth or decay, the equation defining the statistical bell curve etc. It occurs frequently in calculus in logarithmic and exponential functions. It is often referred to as the base of the natural log. What is the exact value of e? As with , the value can never be known exactly since e is an irrational number; that is, it cannot be expressed as the ratio of two integers. Such numbers have an infinite number of digits after the decimal point, and there is no repeating pattern in these digits. However, there are methods to approximate the value of e. In other words, we may not be able to find the exact value of e, but we can calculate it to a certain number of digits (after the decimal point). For example, the value e up to the first 15 digits is 2.718281828459045. Powerful computers today have allowed the computation of e almost up to a trillion digits. The number e can be approximated from an infinite series. A series is the sum of a sequence of terms, and an infinite series simply has infinitely many such terms. In this problem, we focus our attention on the following infinite series to approximate e: e= 1+1+1+1+1+1+... 0! 1! 2! 3! 4! 5! Note that in each term of this series, the denominator is of the form n!. As we saw in class, this is called the factorial of n and is simply the product of the first n positive integers. That is, n! = n(n 1)(n 2)...1. We define 0! to be equal to 1. Therefore, 1! = 1, 2! = 2, 3! = 6, 4! = 24, 5! = 120 and so on. As more terms of the series are included, we get better approximations of e, as shown below (well print 15 digits after the decimal point, to compare with the actual value of e above). The sum of the first term of the series equals 1.000000000000000. October 11, 2018 Homework 3 3 The sum of the first two terms of the series is 2.000000000000000. The sum of the first four terms of the series is 2.666666666666667. first digit after decimal point). The sum of the first six terms of the series is 2.716666666666667 (agrees with the actual value of e up to two digits after decimal point). When we add the first eleven terms of the series, you will get a value that agrees with the actual of e up to six digits after the decimal point. In this problem, you are asked to write two functions: Write a function called factorial with a single parameter N (a non-negative integer). Recall that the factorial of N, denoted by N!, equals N( N 1)( N 2)......1. The function should return the factorial of N. Write a function called approx e with a single parameter numterms (a positive inte- ger). This function should return the sum of the first numterms terms of the above series to approximate e. [25] 4. These problems go together and you should submit a single Python file corre- sponding to Parts (a)-(c) of this problem. (a) Write a function that simulates the roll of two n-sided dice and returns the sum of the outcomes of the two dice rolls. The function should take n as a parameter with a default value of 6. (b) Write a function that simulates the roll of two n-sided dice m times and returns the number of times p appears as the sum of the outcomes of the two dice rolls. The function should take n, m, and p as parameters with a default value of 6 for n and 1000 for m. This function should repeatedly call the function you wrote for Part (a). (c) Write a program that prints out, for each integer i, 2 i 12, the number of times (out of 1000 trials) that i appears as the sum of the rolls of two 6-sided dice. In other words, your program should roll two 6-sided dice 1000 times and report the number of times 2 shows up. It should then roll two 6-sided dice 1000 times and report the number of times 3 shows up. It should repeat this process for each i = 2, 3, 4, . . . , 12. Your program should call the function you wrote for Part (b) repeatedly. (d) Examine the numbers you got in Part (c) for each i = 2,3,4,...,12. Are they all roughly equal, i.e., is each number 2 through 12 equally likely to appear as the sum of the two dice rolls? [25] 5. A sequence of integers is said to be Fibonacci-like if each element of the sequence (except the first two elements) is the sum of the previous two integers in the sequence. 4 Homework 3 October 11, 2018 For example, the sequence 10, 14, 24, 38, 62, 100, 162, 262 is Fibonacci-like. Note that the first two integers in the above sequence are arbitrary. Each of the remaining integers is the sum of the two integers just before it in the sequence. For example, 24 = 10 + 14, 38 = 14 + 24, 62 = 24 + 38, and so on. Your program must query Enter the sequence.. The user will input inte- gers separated by a ,. There could be arbitrary amount of blank space between two succes- sive numbers. Your program must read in the user input and determine if the sequence is Fibonacci-like or not by outputting respectively, The sequence is Fibonacci-like. or The sequence is NOT Fibonacci-like. Two sample runs of the program are given below. Enter the sequence. 10, 14, 24, 38, 62, 100, 162, 262 The sequence is Fibonacci-like. Enter the sequence. 1, 4, 5, 8, 13 The sequence is NOT Fibonacci-like. [25] 6. Write a program that queries the user for a positive integer n and generates what we will call as the Ulams sequence for n. Any number in the sequence can be obtained from the previous number, say l, by applying the following rules: (i) if l is even then the next number in the sequence is l/2, (ii) if l is odd then the next number in the sequence is 3l + 1. The sequence starts with n and ends with the first occurrence of 1. Your program must insert spaces between two consecutive numbers in the sequence and there must be exactly 10 numbers on each line except the last one. The numbers in each of the 10 columns must be right-aligned. For this problem, you may assume that all numbers in Ulams sequence will contain at most 7 digits. Your program must terminate when the user enters an invalid input (number 0). Your program must have a function (other than main) that generates the Ulams sequence for its integer parameter. Your implementation must be based on the following sample execution. Enter a positive integer: 26 26 13 40 20 10 5 16 8 4 2 1 Enter a positive integer: 456 456 228 114 57 172 86 43 130 65 196 98 49 148 74 37 112 56 28 14 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1 Enter a positive integer: 0 Thank you for using the program

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Marketing Database Analytics

Authors: Andrew D. Banasiewicz

1st Edition

0415657881, 978-0415657884

More Books

Students also viewed these Databases questions