Given an integer **N** and the sum of its divisors. The task is to find the sum of the inverse of the divisors of **N**.

**Examples:**

Input:N = 6, Sum = 12

Output:2.00

Divisors of N are {1, 2, 3, 6}

Sum of inverse of divisors is equal to (1/1 + 1/2 + 1/3 + 1/6) = 2.0

Input:N = 9, Sum = 13

Output:1.44

**Naive Approach:** Calculate all the divisors of the given integer **N**. Then calculate the sum of the inverse of the calculated divisors. This approach would give TLE when the value of **N** is large.

**Time Complexity:** O(sqrt(N))

**Efficient Approach:** Let the number **N** has **K** divisors say **d _{1}, d_{2}, …, d_{K}**. It is given that

**d**

_{1}+ d_{2}+ … + d_{K}= SumThe task is to calculate

**(1 / d**.

_{1}) + (1 / d_{2}) + … + (1 / d_{K})Multiply and divide the above equation by

**N**. The equation becomes

**[(N / d**

_{1}) + (N / d_{2}) + … + (N / d_{K})] / NNow it is easy to see that **N / d _{i}** would represent some another divisor of

**N**for all

**1 ≤ i ≤ K**. The numerator is equal to the sum of the divisors. Hence sum of inverse of the divisors is equal to

**Sum / N**.

Below is the implementation of the above approach:

## C++

`// C++ implementation of above approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the ` `// sum of inverse of divisors ` `double` `SumofInverseDivisors(` `int` `N, ` `int` `Sum) ` `{ ` ` ` ` ` `// Calculating the answer ` ` ` `double` `ans = (` `double` `)(Sum)*1.0 / (` `double` `)(N); ` ` ` ` ` `// Return the answer ` ` ` `return` `ans; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `N = 9; ` ` ` ` ` `int` `Sum = 13; ` ` ` ` ` `// Function call ` ` ` `cout << setprecision(2) << fixed ` ` ` `<< SumofInverseDivisors(N, Sum); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of above approach ` `import` `java.math.*; ` `import` `java.io.*; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to return the ` `// sum of inverse of divisors ` `static` `double` `SumofInverseDivisors(` `int` `N, ` `int` `Sum) ` `{ ` ` ` ` ` `// Calculating the answer ` ` ` `double` `ans = (` `double` `)(Sum)*` `1.0` `/ (` `double` `)(N); ` ` ` ` ` `// Return the answer ` ` ` `return` `ans; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main (String[] args) ` `{ ` ` ` ` ` `int` `N = ` `9` `; ` ` ` `int` `Sum = ` `13` `; ` ` ` ` ` `// Function call ` ` ` `System.out.println (SumofInverseDivisors(N, Sum)); ` `} ` `} ` ` ` `// This code is contributed by jit_t. ` |

*chevron_right*

*filter_none*

## Python

`# Python implementation of above approach ` ` ` `# Function to return the ` `# sum of inverse of divisors ` `def` `SumofInverseDivisors( N, ` `Sum` `): ` ` ` ` ` `# Calculating the answer ` ` ` `ans ` `=` `float` `(` `Sum` `)` `*` `1.0` `/` `float` `(N); ` ` ` ` ` `# Return the answer ` ` ` `return` `round` `(ans,` `2` `); ` ` ` ` ` `# Driver code ` `N ` `=` `9` `; ` `Sum` `=` `13` `; ` `print` `SumofInverseDivisors(N, ` `Sum` `); ` ` ` `# This code is contributed by CrazyPro ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of above approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to return the ` `// sum of inverse of divisors ` `static` `double` `SumofInverseDivisors(` `int` `N, ` `int` `Sum) ` `{ ` ` ` ` ` `// Calculating the answer ` ` ` `double` `ans = (` `double` `)(Sum)*1.0 / (` `double` `)(N); ` ` ` ` ` `// Return the answer ` ` ` `return` `ans; ` `} ` ` ` `// Driver code ` `static` `public` `void` `Main () ` `{ ` ` ` ` ` `int` `N = 9; ` ` ` `int` `Sum = 13; ` ` ` ` ` `// Function call ` ` ` `Console.Write(SumofInverseDivisors(N, Sum)); ` `} ` `} ` ` ` `// This code is contributed by ajit ` |

*chevron_right*

*filter_none*

**Output:**

1.44

**Time Complexity:** O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Find sum of divisors of all the divisors of a natural number
- Check if a number has an odd count of odd divisors and even count of even divisors
- Count array elements having modular inverse under given prime number P equal to itself
- XOR of array elements whose modular inverse with a given number exists
- Nearest smaller number to N having multiplicative inverse under modulo N equal to that number
- Find the value of P and modular inverse of Q modulo 998244353
- Check if the given array is same as its inverse permutation
- Fast method to calculate inverse square root of a floating point number in IEEE 754 format
- Divisors of n-square that are not divisors of n
- Maximum possible prime divisors that can exist in numbers having exactly N divisors
- Check if count of even divisors of N is equal to count of odd divisors
- Minimum value exceeding X whose count of divisors has different parity with count of divisors of X
- Adjoint and Inverse of a Matrix
- Decimal Equivalent of Gray Code and its Inverse
- Find the largest good number in the divisors of given number N
- Chinese Remainder Theorem | Set 2 (Inverse Modulo based Implementation)
- Modular multiplicative inverse from 1 to n
- Fast inverse square root
- Program to implement Inverse Interpolation using Lagrange Formula
- Modular multiplicative inverse

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.