n over k

For a complex value n and a not negative integer value k is the binominal coefficient n over k as followed defined:

n over k image

This is an algorithm to calculate the binominal coeffizient n over k with the GMP library.

main.cpp
1/*
2
3    n_over_k.cpp
4  
5    How to compile with GCC under Linux/UNIX:
6    g++ -lgmp -lgmpxx -o n_over_k n_over_k.cpp
7
8*/
9
10#include <iostream>
11#include <gmp.h>
12#include <gmpxx.h>
13using namespace std;
14
15unsigned long long n_over_k(int, int);
16mpz_class factorial(int);
17
18int main(int argc, char **argv) {
19    int n, k;
20 
21    cout << "n over k:" << endl;
22    cout << "Input n: ";
23    cin >> n;
24    cout << "Input k: ";
25    cin >> k;
26 
27    cout << endl << " " << n << "!";
28    cout << endl << "______________";
29    cout << endl << k << "! * (" << n << " - " << k << ")!";
30    cout << endl << endl << " = " << endl << n_over_k(n,k) << endl;
31 
32    return 0;
33}
34
35unsigned long long n_over_k(int n, int k) {
36    if(n == k) return 1;
37    else if(n < k) return 0;
38    else {
39        mpz_class result = factorial(n) / (factorial(k) * factorial(n - k));
40        return result.get_ui();
41    }
42}
43
44mpz_class factorial(int f) {
45    if(f < 2) return 1;
46    return f * factorial(f - 1);
47}