Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <vector>
- #include <cmath>
- // this function adapted from
- // http://www.askamathematician.com/2010/07/q-whats-the-chance-of-getting-a-run-of-k-successes-in-n-bernoulli-trials-why-use-approximations-when-the-exact-answer-is-known/
- double probOfStreak(int numCoins, int minHeads, double headProb)
- {
- std::vector<double> memo(numCoins+1);
- for (int i = 0; i < minHeads; ++i)
- {
- memo[i] = 0;
- }
- for (int i = minHeads; i <= numCoins; ++i)
- {
- double result = std::pow(headProb, minHeads);
- for (int firstTail = 1; firstTail <= minHeads; ++firstTail)
- {
- double pr = memo[i - firstTail];
- result += std::pow(headProb, firstTail-1) * (1 - headProb) * pr;
- }
- memo[i] = result;
- }
- return memo[numCoins];
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement