#include using namespace std; // The probability that a machine produces a defective product is // 1/3. What is the probability that the 1st defect is found during // the first 5 inspections? int main() { double p = 1.0/3.0; auto geometric_dist = [](int n, double p) { // The geometric distribution is a special case of the negative // binomial distribution that deals with the number of Bernoulli // trials required to get a success (i.e., counting the number of // failures before the first success). // g(n,p) = q^(n-1) x p return pow(1-p, n-1) * p; }; double out = 0; // sum the probability of defect for each inspection for (auto n: {1,2,3,4,5}) out += geometric_dist(n, p); cout << fixed << setprecision(3) << out << endl; return 0; }