Product of Three Dice: Summary Statistics Report
Math & Bit ManipulationAuto-graded · 2 test casesLast updated
Enumerate all 216 outcomes of three dice to compute the mean, modes, prime probability and median of their product, and return a fixed risk note.
Product of Three Dice
Three fair six-sided dice, each showing an integer from 1 to 6, are rolled independently. Let the random variable P be the product of the three faces.
Write a function that takes no arguments and returns a dictionary summarizing the distribution of P. Treat the sample space as the 216 equally likely ordered outcomes (a, b, c) with each of a, b, c in 1..6.
Required keys
The returned dictionary must contain exactly these five keys:
"expected_value": the mean ofPover all 216 outcomes, as a float."modes": a list of every value ofPthat occurs most often among the 216 ordered outcomes, sorted in ascending order. Each element is anint."prime_probability": the probability thatPis a prime number, as a float. This is the number of outcomes whose product is prime divided by 216. Recall that 1 is not prime."median": the median ofPas anint. Sort the 216 products in ascending order and take the element at zero-based position 108 (the 109th smallest value). This valuemsatisfies bothP(P <= m) >= 0.5andP(P >= m) >= 0.5."skewness": a fixed text note about a seller who sells the derivative at the priceE[P]. It must be exactly this string, character for character:
right-skewed; outcomes substantially above the expected sale price are possible, creating adverse upside exposure for the seller
Context
Imagine you sell a derivative whose payoff is exactly P at a fixed price equal to E[P]. The statistics above describe how the payoff is distributed. The fixed "skewness" text summarizes the resulting risk for the seller.
Constraints
- Each die is fair and independent of the others.
- Use exact enumeration over the 6^3 = 216 ordered outcomes. Different orderings of the same faces count as different outcomes.
- The function takes no parameters.
- The returned dictionary has exactly the five keys listed above.
Examples
Example 1
Input: summarize_dice_product()
Output: {'expected_value': 42.875, 'modes': [12, 24], 'prime_probability': 0.041666666666666664, 'median': 30, 'skewness': 'right-skewed; outcomes substantially above the expected sale price are possible, creating adverse upside exposure for the seller'}
Hints
Hint 1
There are only 216 ordered outcomes, so you can enumerate all of them with three nested loops and build a list of products.
Hint 2
A Counter of the products gives the frequency of each value. Take the maximum frequency and collect every value that reaches it, then sort them.
Hint 3
A product of three integers that are each at least 1 can only be prime if two of the factors are 1. Alternatively, test each product directly against a small primality check. For the median, sort the products and read the element at index 108.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The sample space is tiny: 6 x 6 x 6 = 216 ordered outcomes. Instead of deriving the distribution analytically, enumerate every outcome and compute each statistic directly.
- Products. Build a list of
a * b * cfor all ordered triples. - Expected value. Divide the sum of the products by 216. Since the dice are independent, this equals
3.5^3 = 42.875, which confirms the enumeration. - Modes. Count how often each product appears with a
Counter, find the highest count, and collect every product that reaches it, sorted ascending. The highest count is 15, reached by 12 and 24, so the distribution is bimodal. - Prime probability. Count the products that are prime and divide by 216. Only outcomes with one prime face (2, 3 or 5) and two faces equal to 1 qualify. That gives 3 prime faces x 3 positions = 9 outcomes, so the probability is 9/216 = 1/24.
- Median. Sort the 216 products and take index 108. The cumulative count reaches 105 at product 27 and 117 at product 30, so positions 108 and 109 both hold 30. The median is 30.
- Skewness note. The mean (42.875) is well above the median (30), and the tail extends up to 216. This is a right-skewed distribution, so a seller at
E[P]faces large upside losses. The function returns the required fixed string.
Why it is correct
Every statistic is computed from the complete list of equally likely ordered outcomes, so each one is exact by definition. Sorting the modes and using a deterministic median index removes any ambiguity in the output.
Complexity
- Time: O(n^3 log n^3) with n = 6, which is constant in practice (216 elements).
- Space: O(n^3), also constant here.
Reference solution
from collections import Counter
from typing import Dict
def _is_prime(n: int) -> bool:
if n < 2:
return False
d = 2
while d * d <= n:
if n % d == 0:
return False
d += 1
return True
def summarize_dice_product() -> Dict[str, object]:
products = [a * b * c
for a in range(1, 7)
for b in range(1, 7)
for c in range(1, 7)]
total = len(products)
counts = Counter(products)
expected = sum(products) / total
top = max(counts.values())
modes = sorted(v for v, cnt in counts.items() if cnt == top)
prime_count = sum(1 for p in products if _is_prime(p))
prime_probability = prime_count / total
ordered = sorted(products)
median = ordered[total // 2]
return {
"expected_value": expected,
"modes": modes,
"prime_probability": prime_probability,
"median": median,
"skewness": "right-skewed; outcomes substantially above the expected sale price are possible, creating adverse upside exposure for the seller",
}