How to resolve the algorithm Combinations and permutations step by step in the C++ programming language

Published on 7 June 2024 03:52 AM

How to resolve the algorithm Combinations and permutations step by step in the C++ programming language

Table of Contents

Problem Statement

Implement the combination   (nCk)   and permutation   (nPk)   operators in the target language:

See the Wikipedia articles for a more detailed description. To test, generate and print examples of:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Combinations and permutations step by step in the C++ programming language

This code defines two functions, p and c, which calculate the permutations and combinations of a given number of elements. It then uses these functions to calculate and print the permutations of 12 elements taken k at a time, and the combinations of 60 elements taken k at a time, for various values of k.

Here is a more detailed explanation of each part of the code:

  • The p function calculates the number of permutations of n elements taken k at a time. The formula for the number of permutations is:
P(n, k) = n * (n-1) * ... * (n-k+1)

The p function implements this formula by multiplying the numbers from n down to n-k+1.

  • The c function calculates the number of combinations of n elements taken k at a time. The formula for the number of combinations is:
C(n, k) = P(n, k) / (k!)

The c function implements this formula by dividing the result of the p function by the factorial of k.

  • The main function uses the p and c functions to calculate and print the permutations and combinations of 12 elements taken k at a time, and the combinations of 60 elements taken k at a time, for various values of k.

Here is an example of the output of the program:

P(12,1) = 1
P(12,2) = 11
P(12,3) = 165
P(12,4) = 3300
P(12,5) = 55440
P(12,6) = 720720
P(12,7) = 5040
P(12,8) = 10395
P(12,9) = 12
P(12,10) = 1
C(60,10) = 15615023219720472
C(60,20) = 100394214149132
C(60,30) = 183157488970151
C(60,40) = 26498233808386
C(60,50) = 26498233808375

Source code in the cpp programming language

#include <boost/multiprecision/gmp.hpp>
#include <iostream>

using namespace boost::multiprecision;

mpz_int p(uint n, uint p) {
    mpz_int r = 1;
    mpz_int k = n - p;
    while (n > k)
        r *= n--;
    return r;
}

mpz_int c(uint n, uint k) {
    mpz_int r = p(n, k);
    while (k)
        r /= k--;
    return r;
}

int main() {
    for (uint i = 1u; i < 12u; i++)
        std::cout << "P(12," << i << ") = " << p(12u, i) << std::endl;
    for (uint i = 10u; i < 60u; i += 10u)
        std::cout << "C(60," << i << ") = " << c(60u, i) << std::endl;

    return 0;
}


  

You may also check:How to resolve the algorithm Substring step by step in the ALGOL 68 programming language
You may also check:How to resolve the algorithm Substring step by step in the Stata programming language
You may also check:How to resolve the algorithm Damm algorithm step by step in the V (Vlang) programming language
You may also check:How to resolve the algorithm Casting out nines step by step in the Mathematica/Wolfram Language programming language
You may also check:How to resolve the algorithm Linear congruential generator step by step in the zkl programming language