Open In App

Find the value of f(n) / f(r) * f(n-r)

Improve
Improve
Like Article
Like
Save
Share
Report

Given two positive integers n and r where n > r >1. The task is to find the value of f(n)/(f(r)*f(n-r)). F(n) is defined as follows: 
 

1-1 *2-2 *3-3 *….. n-n 

Examples: 
 

Input: n = 5, r = 3
Output: 1/200000

Input: n = 3, r = 2
Output: 1/27

 

A naive approach to solve this question is to calculate f(n), f(r) and f(n-r) separately and then calculating the result as per given formula but that will cost a bit high of time complexity. 
A better approach to solve this question is to find the greater value among r and n-r and then after using the property f(n) = f(n-1)* n-n = f(n-1)/nn of given function, eliminate the greater among f(r) and f(n-r) from numerator and denominator. After that calculate the rest of value by using simple loop and power function.
Algorithm: 
 

find max(r, n-r). 
iterate from max(r, n-r) to n 
result = ((result * i-i / (i-max(r, n-r)) -(i-max(r, n-r)) )

Below is the implementation of the above approach: 
 

C++




// CPP to find the value of f(n)/f(r)*f(n-r)
#include <bits/stdc++.h>
using namespace std;
 
// Function to find value of given F(n)
int calcFunction(int n, int r)
{
    int finalDenominator = 1;
    int mx = max(r, n - r);
 
    // iterate over n
    for (int i = mx + 1; i <= n; i++) {
 
        // calculate result
        int denominator = (int)pow(i, i);
        int numerator = (int)pow(i - mx, i - mx);
        finalDenominator = (finalDenominator
                            * denominator)
                           / numerator;
    }
 
    // return the result
    return finalDenominator;
}
 
// Driver code
int main()
{
    int n = 6, r = 2;
    cout << "1/" << calcFunction(n, r) << endl;
 
    return 0;
}


Java




// Java program to find the value of f(n)/f(r)*f(n-r) 
 
class GFG {
// Function to find value of given F(n)
 
    static int calcFunction(int n, int r) {
        int finalDenominator = 1;
        int mx = Math.max(r, n - r);
 
        // iterate over n
        for (int i = mx + 1; i <= n; i++) {
 
            // calculate result
            int denominator = (int) Math.pow(i, i);
            int numerator = (int) Math.pow(i - mx, i - mx);
            finalDenominator = (finalDenominator
                    * denominator)
                    / numerator;
        }
 
        // return the result
        return finalDenominator;
    }
 
// Driver code
    public static void main(String[] args) {
        int n = 6, r = 2;
        System.out.println("1/" + calcFunction(n, r));
 
    }
}
// This code is contributed by RAJPUT-JI


Python 3




# Python3 to find the value of f(n)/f(r)*f(n-r)
 
# Function to find value of given F(n)
def calcFunction(n, r):
 
    finalDenominator = 1
    mx = max(r, n - r)
 
    # iterate over n
    for i in range(mx + 1, n + 1):
 
        # calculate result
        denominator = pow(i, i)
        numerator = pow(i - mx, i - mx)
        finalDenominator = (finalDenominator *
                            denominator) // numerator
 
    # return the result
    return finalDenominator
 
# Driver code
if __name__ == "__main__":
     
    n = 6
    r = 2
    print("1/", end = "")
    print(calcFunction(n, r))
 
# This code is contributed by ita_c


C#




// C# program to find the value of f(n)/f(r)*f(n-r)
using System;
 
public class GFG {
// Function to find value of given F(n)
 
    static int calcFunction(int n, int r) {
        int finalDenominator = 1;
        int mx = Math.Max(r, n - r);
 
        // iterate over n
        for (int i = mx + 1; i <= n; i++) {
 
            // calculate result
            int denominator = (int) Math.Pow(i, i);
            int numerator = (int) Math.Pow(i - mx, i - mx);
            finalDenominator = (finalDenominator
                    * denominator)
                    / numerator;
        }
 
        // return the result
        return finalDenominator;
    }
 
// Driver code
   public static void Main() {
        int n = 6, r = 2;
        Console.WriteLine("1/" + calcFunction(n, r));
 
    }
}
// This code is contributed by RAJPUT-JI


PHP




<?php
// PHP to find the value of f(n)/f(r)*f(n-r)
 
// Function to find value of given F(n)
function calcFunction($n, $r)
{
    $finalDenominator = 1;
    $mx = max($r, $n - $r);
 
    // iterate over n
    for ($i = $mx + 1; $i <= $n; $i++)
    {
 
        // calculate result
        $denominator = pow($i, $i);
        $numerator = pow($i - $mx, $i - $mx);
        $finalDenominator = ($finalDenominator *
                             $denominator) /
                             $numerator;
    }
 
    // return the result
    return $finalDenominator;
}
 
// Driver code
$n = 6; $r = 2;
echo "1/" , calcFunction($n, $r);
 
// This code is contributed by anuj_67..
?>


Javascript




<script>
 
// Javascript to find the value of f(n)/f(r)*f(n-r)
 
// Function to find value of given F(n)
function calcFunction(n, r)
{
    var finalDenominator = 1;
    var mx = Math.max(r, n - r);
 
    // iterate over n
    for (var i = mx + 1; i <= n; i++) {
 
        // calculate result
        var denominator = Math.pow(i, i);
        var numerator = Math.pow(i - mx, i - mx);
        finalDenominator = (finalDenominator
                            * denominator)
                           / numerator;
    }
 
    // return the result
    return finalDenominator;
}
 
// Driver code
var n = 6, r = 2;
document.write( "1/" + calcFunction(n, r));
 
</script>


Output: 

1/36450000

 

Time Complexity: O(nlogn)

Auxiliary Space: O(1)



Last Updated : 29 Jun, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads