Starting from:

$25

Recursive Functions and Random function_lab 3 Solution

Objectives
• More work with recursive functions •
Work with the random function
Activities
1. The following continued fraction provides an approximation for π:
4 π
= 1 +
12
2 +
32
2 +
52
2 +
72
2 +
... Notethatthenumeratorofeachcontinuedfractionisthesquareofthenextlargestoddnumber. Also, for the base case, you can use 2 as the denominator in the fraction. For example, if the fraction with 72 2+ ... is the final fraction, you can ignore the diagonal dots (there are no recursive calls past this one). Therefore, the last fraction computed would be 72 2 . Write a Scheme function to compute this continued fraction. Your function should take one parameter, the number of continued fractions to compute recursively. 2. Another method of approximating π is achieved through the product of the following nested radicals:
2 π
=r1 2 ·s1 2
+
1 2r1 2 ·v u u t1 2
+
1 2s1 2
+
1 2r1 2
...
Write a Scheme function which computes the product of these nested radicals for a given number of terms, k. 3. The game of Craps is a dice game in which the player rolls two dice. The player may win the game on the first roll by rolling a 7 or 11. Write a Scheme function that approximates the probability of winning at Craps on the first roll by simulating a large number of dice rolls. You will need to use the random function to simulate a randomly thrown die. The random function takes one integer argument k, and returns a random exact integer in the range 0 to k-1. For example, to simulate the roll of one die: (random 6)
will return an integer in the range 0 to 5. You can add one to obtain roll values in the range 1 to 6. To simulate the rolling of two dice, you will have to use random to simulate each die separately and compute the sum of their values. Your function should take a formal parameter n indicating the number of rolls to simulate and return a decimal representing the percent of rolls won. What win percentage is your simulation approaching as n gets large?

More products