Odd First-Success Roll Probability
Problem statement
For this exercise, assume a fair die has faces numbered from 1 through sides, with every face equally likely. Repeated rolls are independent. Roll until the first occurrence of face sides.
Let N be the total number of rolls, starting at 1 and including the successful roll. Return the probability that N is odd.
The reported question uses a six-sided die and asks about the first 6. For this exercise, assume sides is an input generalizing that die size. Complete oddFirstSuccessProbability(sides) and return a double without rounding.
Answers are accepted when abs(actual-expected) <= 1e-9 * max(1,abs(expected)).
Function
oddFirstSuccessProbability(sides: int) → doubleExamples
Example 1
sides = 6return = 0.5454545454545454Reported fixed case under stated assumptions. The exact probability is 6/11.
Example 2
sides = 2return = 0.6666666666666666Authored practice example. The exact probability is 2/3.
Example 3
sides = 3return = 0.6Authored practice example. The exact probability is 3/5.
Constraints
- For this exercise, assume
2 <= sides <= 10^6. - The die is fair; rolls are independent.
- The total count includes the successful roll.