Find the value of ln(N!) using Recursion using C++.


Suppose we have a number N, our task is to find ln(N!) using recursion. ln() is basically log base e. To solve this we can use this formula −

$$\ln\lgroup N!\rgroup=\ln\lgroup N*\lgroup N-1\rgroup *\lgroup N-2\rgroup *\dotsm*2*1\rgroup=\ln\lgroup N\rgroup+\ln\lgroup N+1\rgroup+\dotsm+\ln\lgroup 1\rgroup$$

Example

 Live Demo

#include<iostream>
#include<cmath>
using namespace std;
double factLog(int n) {
   if (n <= 1)
      return 0;
   return factLog(n - 1) + log(n);
}
int main() {
   int N = 3;
   cout << factLog(N);
}

Output

1.79176

Updated on: 30-Oct-2019

120 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements