Find the value of ln(N!) using Recursion

Given a number N, the task is to find the log value of the factorial of N i.e. log(N!).

Note: ln means log with base e.

Examples:

Input: N = 2
Output: 0.693147

Input:  N = 3
Output: 1.791759


Approach:

Method -1: Calculate n! first, then take its log value.

Method -2: By using the property of log, i.e. take the sum of log values of n, n-1, n-2 …1.

ln(n!) = ln(n*n-1*n-2*…..*2*1) = ln(n)+ln(n-1)+……+ln(2)+ln(1)

Below is the implementation of the Method-2:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the above approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to calculate the value
double fact(int n)
{
    if (n == 1)
        return 0;
    return fact(n - 1) + log(n);
}
// Driver code
int main()
{
    int N = 3;
    cout << fact(N) << "\n";
    return 0;
}

chevron_right


C

filter_none

edit
close

play_arrow

link
brightness_4
code

// C implementation of the above approach
#include <math.h>
#include <stdio.h>
  
long double fact(int n)
{
    if (n == 1)
        return 0;
    return fact(n - 1) + log(n);
}
  
// Driver code
int main()
{
    int n = 3;
    printf("%Lf", fact(n));
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the above approach
import java.util.*;
import java.io.*;
class logfact {
    public static double fact(int n)
    {
        if (n == 1)
            return 0;
        return fact(n - 1) + Math.log(n);
    }
  
    public static void main(String[] args)
    {
  
        int N = 3;
        System.out.println(fact(N));
    }
}

chevron_right


Python

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python implementation of the above approach
import math
def fact(n):
    if (n == 1):
        return 0
    else
        return fact(n-1) + math.log(n)
N = 3
print(fact(N))

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the above approach
using System;
  
class GFG
{
    public static double fact(int n)
    {
        if (n == 1)
            return 0;
        return fact(n - 1) + Math.Log(n);
    }
  
    // Driver code
    public static void Main()
    {
        int N = 3;
        Console.WriteLine(fact(N));
    }
}
  
// This code is contributed by ihritik

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php 
  
//PHP implementation of the above approach
  
function fact($n)
{
    if ($n == 1)
        return 0;
    return fact($n - 1) + log($n);
}
  
// Driver code
$n = 3;
echo fact($n);
  
// This code is contributed by ihritik
?>

chevron_right


Output:

1.791759


My Personal Notes arrow_drop_up

||Coder💻⌨️|| ||Hacker☣️💉|| ||Editor📹📷|| ||🔰DAY⚔️🛡️⚔️ SCHOLAR🔰#️⃣mmmut||

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : ihritik