Lab: Loops and Simulations
Lab: Loops and Simulations 1. In poker, five cards are dealt to each player from a full deck of 52 cards (without replacement, of course). One relatively rare hand to see is five cards that span 5 consecutive ranks. This is called a “straight”. As two examples, both and are “straights”. The probability that one sees a straight is around 0.003925. Let us define a variant on a straight, called a “every-other straight”, where the 5 ranks present in the hand consist of “every other” rank, starting at the minimum rank seen in the hand. As an example, and would both be every-other-straights. In poker, straights may begin or end with an ace — but to keep things simpler here, let us require that “every-other-straights” can begin with, but not end in an ace. Use R to simulate one million 5-card poker hands. To the nearest thousandth, what is the proportion of “every-other-straights” seen? 2. Remarkably, the Euclidean Algorithm allows one to find the greatest common divisor (GCD) of two numbers without ever factoring the numbers in question. This algorithm finds the GCD of two integers and by finding the remainder resulting from dividing by , and then repeating this process letting and , until the remainder is zero. The last non-zero remainder is the GCD. The calculations below demonstrate this process as they find the GCD of and : 90456 = 0 * 2153106 + 90456 2153106 = 23 * 90456 + 72618 90456 = 1 * 72618 + 17838 72618 = 4 * 17838 + 1266 17838 = 14 * 1266 + 114 1266 = 11 * 114 + 12 114 = 9 * 12 + 6 12 = 2 * 6 + 0 gcd(90456,2153106) = 6 About Number Theory Java Data Structures Precalculus Calculus (7C, 8H, 9D, 10H, JS) (2S, 3S, 4H, 5D, 6S) (AH, 3S, 5D, 7D, 9C) (4D, 6D, 8H, 10C, QC) a b r a b a = b b = r 90456 2153106 Statistics You are interested in the average number of steps the Euclidean Algorithm takes when both numbers involved have 10 digits. To this end, write the following two R functions: num.steps(a,b), which calculates the number of steps the Euclidean Algorithm will need before the GCD is found. The example requires 8 steps (Note, each equation is considered a “step”). avg.steps(num.trials), which is the mean number of steps required for num.trials pairs of integers and , randomly (and uniformly) chosen so that each has exactly 10 digits. Using the last function above (i.e., avg.steps()), with at least 1000 trials, and rounding to the nearest integer — how many steps are required to find the GCD for pairs of numbers each 10 digits long? (As a check of your work, if the two numbers only had 6 digits each, the average number of steps required is close to 12.)