## Class PigMath

• java.lang.Object
• PigMath

• ```public class PigMath
extends java.lang.Object```
A class for computing mathematical results concerning the dice game of Pig.
• ### Constructor Summary

Constructors
Constructor and Description
`PigMath()`
• ### Method Summary

All Methods
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.
• ### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`
• ### Constructor Detail

• #### PigMath

`public PigMath()`
• ### Method Detail

• #### probabilityOfRolling

`public static BigFraction probabilityOfRolling(int target)`
Return the probability of rolling a Pig turn total of at least a given target number of points.

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.

Parameters:
`target` - a Pig turn total
Returns:
the probability of rolling a Pig turn total of at least a given target number of points
• #### main

`public static void main(java.lang.String[] args)`
Test probabilityOfRolling(100). The result should be "The probability in Pig of rolling at least 100 on a single turn is approximately 0.010197 and exactly 2060507550845146798433160823128452341/202070319366191015160784900114134073344."
Parameters:
`args` - (unused)