Find Range Value of the Expression

Given two integers L and R, the task is to calculate the value of the expression:

    $$F = \sum_{i=L}^{R} \frac{1}{i^2 + i} $$

Examples:



Input: L = 6, R = 12
Output: 0.09

Input: L = 5, R = 6
Output: 0.06

Approach: It can be observed that  \frac{1}{i^2 + i} = \frac{1}{i} - \frac{1}{i + 1} .

So, F = \sum_{i=L}^{R} \frac{1}{i^2 + i} = (\frac{1}{L} - \frac{1}{L + 1}) + (\frac{1}{L + 1} - \frac{1}{L + 2}) + .... + (\frac{1}{R} - \frac{1}{R + 1}) = (\frac{1}{L} - \frac{1}{R + 1})

Hence, the answer will be (1 / L) – (1 / (R + 1)).

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to return the value
// of the given expression
double get(double L, double R)
{
  
    // Value of the first term
    double x = 1.0 / L;
  
    // Value of the last term
    double y = 1.0 / (R + 1.0);
  
    return (x - y);
}
  
// Driver code
int main()
{
    int L = 6, R = 12;
  
    // Get the result
    double ans = get(L, R);
    cout << fixed << setprecision(2) << ans;
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
import java.util.*;
  
class GFG
{
  
// Function to return the value
// of the given expression
static double get(double L, double R)
{
  
    // Value of the first term
    double x = 1.0 / L;
  
    // Value of the last term
    double y = 1.0 / (R + 1.0);
  
    return (x - y);
}
  
// Driver code
public static void main(String []args)
{
    int L = 6, R = 12;
  
    // Get the result
    double ans = get(L, R);
    System.out.printf( "%.2f", ans);
}
}
  
// This code is contributed by Surendra_Gangwar

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach
  
# Function to return the value 
# of the given expression 
def get(L, R) :
  
    # Value of the first term 
    x = 1.0 / L; 
  
    # Value of the last term 
    y = 1.0 / (R + 1.0); 
  
    return (x - y); 
  
# Driver code 
if __name__ == "__main__"
  
    L = 6; R = 12
  
    # Get the result 
    ans = get(L, R); 
    print(round(ans, 2)); 
  
# This code is contributed by AnkitRai01

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

      
// C# implementation of the approach
using System;
  
public class GFG
{
   
// Function to return the value
// of the given expression
static double get(double L, double R)
{
   
    // Value of the first term
    double x = 1.0 / L;
   
    // Value of the last term
    double y = 1.0 / (R + 1.0);
   
    return (x - y);
}
   
// Driver code
public static void Main(String []args)
{
    int L = 6, R = 12;
   
    // Get the result
    double ans = get(L, R);
    Console.Write( "{0:F2}", ans);
}
}
  
// This code contributed by PrinciRaj1992

chevron_right


Output:

0.09

Time Complexity: O(1)




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.