public class PigMath
extends java.lang.Object
Constructor and Description |
---|
PigMath() |
Modifier and Type | Method and Description |
---|---|
static void |
main(java.lang.String[] args)
Test probabilityOfRolling(100).
|
static BigFraction |
probabilityOfRolling(int target)
Return the probability of rolling a Pig turn total of at least a given target number of points.
|
public static BigFraction probabilityOfRolling(int target)
The algorithm can be described as follows:
Create an array p indexed by turn total t for computing p[t], the probability for reaching at least the target turn total from the current turn total t.
Initially, p[t] = 0 for t < target and p[t] = 1 for t ≥ target. Then, working from t=target-1 down to t=0, you can compute each p[t] = p[t+2]/6 + p[t+3]/6 + p[t+4]/6 + p[t+5]/6 + p[t+6]/6.
This is called retrograde analysis, and you'll be working with BigFraction objects for exact computation.
Example: To compute the probability of reaching a target of 3, array p contains BigFraction values for {0, 0, 0, 1, 1, 1, 1, 1, 1}.
Working backwards from t = 2 down to 0. p[2] = p[2+2]/6 + p[2+3]/6 + p[2+4]/6 + p[2+5]/6 + p[2+6]/6 = 1/6 + 1/6 + 1/6 + 1/6 + 1/6 = 5/6.
p[1] = p[1+2]/6 + p[1+3]/6 + p[1+4]/6 + p[1+5]/6 + p[1+6]/6 = 1/6 + 1/6 + 1/6 + 1/6 + 1/6 = 5/6.
p[0] = p[0+2]/6 + p[0+3]/6 + p[0+4]/6 + p[0+5]/6 + p[0+6]/6 = (5/6)/6 + 1/6 + 1/6 + 1/6 + 1/6 = 29/36.
Use loops for (1) initializing your p values, (2) iterating down through the turn totals, and (3) iterating through the roll numbers 2-6 for each turn total.
target
- a Pig turn totalpublic static void main(java.lang.String[] args)
args
- (unused)