Given a number **N** and a prime number **P**, the task is to find the sum of largest divisors of each number in the range **[1, N]**, which is not divisible by **P**.**Examples:**

Input:N = 8, P = 2Output:22Explanation:Numbers are in the range [1, 8].Number Largest Divisor not divisible by P = 2

1 1

2 1

3 3

4 1

5 5

6 3

7 7

81

Sum of all divisors with given constraint = 22.

Input:N = 10, P = 5Output:43Explanation:Numbers are in the range [1, 8].Number Largest Divisor not divisible by P = 5

1 1

2 2

3 3

4 4

5 1

6 6

7 7

8 8

9 9

10 2

Sum of all divisors with given constraint = 43

**Naive Approach:** The naive idea is to find the divisors for each number in the range **[1, N]** and find the largest divisors which is not divisible by **P** and those numbers. Print the sum of all the those largest divisors. **Time Complexity:** O(N^{3/2}) **Auxiliary Space:** O(1)

**Efficient Approach:** The idea is to observe that the largest divisor of a number **N** not divisible by **P** would be **N** itself if **N** is not divisible by **P**. Else the required divisor will be the same as that of **N/P**. Below are the steps:

- If N is not divisible by P, then the largest divisor will be
**N**, add this to the final sum.. - If N is divisible by P, the required divisor will be the same as that of
**N/P**. - So, find the sum of all numbers which are not divisible by
**P**and add to them divisors of those which are divisible by P separately. - The total sum would be
**N*(N + 1)/2**. Subtract the sum of those which are divisible by**P**and add their corresponding value by recursively calling the function to find the sum for**N/P**.

Below is the implementation of the above approach:

## C++

`// C++ program for the above approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the sum of largest ` `// divisors of numbers in range 1 to N ` `// not divisible by prime number P ` `int` `func(` `int` `N, ` `int` `P) ` `{ ` ` ` `// Total sum upto N ` ` ` `int` `sumUptoN = (N * (N + 1) / 2); ` ` ` `int` `sumOfMultiplesOfP; ` ` ` ` ` `// If no multiple of P exist up to N ` ` ` `if` `(N < P) { ` ` ` `return` `sumUptoN; ` ` ` `} ` ` ` ` ` `// If only P itself is in the range ` ` ` `// from 1 to N ` ` ` `else` `if` `((N / P) == 1) { ` ` ` `return` `sumUptoN - P + 1; ` ` ` `} ` ` ` ` ` `// Sum of those that are divisible by P ` ` ` `sumOfMultiplesOfP ` ` ` `= ((N / P) * (2 * P + (N / P - 1) * P)) / 2; ` ` ` ` ` `// Recursively function call to ` ` ` `// find the sum for N/P ` ` ` `return` `(sumUptoN ` ` ` `+ func(N / P, P) ` ` ` `- sumOfMultiplesOfP); ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `// Given N and P ` ` ` `int` `N = 10, P = 5; ` ` ` ` ` `// Function Call ` ` ` `cout << func(N, P) << ` `"\n"` `; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program for the above approach ` `class` `GFG{ ` ` ` `// Function to find the sum of largest ` `// divisors of numbers in range 1 to N ` `// not divisible by prime number P ` `static` `int` `func(` `int` `N, ` `int` `P) ` `{ ` ` ` ` ` `// Total sum upto N ` ` ` `int` `sumUptoN = (N * (N + ` `1` `) / ` `2` `); ` ` ` `int` `sumOfMultiplesOfP; ` ` ` ` ` `// If no multiple of P exist up to N ` ` ` `if` `(N < P) ` ` ` `{ ` ` ` `return` `sumUptoN; ` ` ` `} ` ` ` ` ` `// If only P itself is in the range ` ` ` `// from 1 to N ` ` ` `else` `if` `((N / P) == ` `1` `) ` ` ` `{ ` ` ` `return` `sumUptoN - P + ` `1` `; ` ` ` `} ` ` ` ` ` `// Sum of those that are divisible by P ` ` ` `sumOfMultiplesOfP = ((N / P) * (` `2` `* P + ` ` ` `(N / P - ` `1` `) * P)) / ` `2` `; ` ` ` ` ` `// Recursively function call to ` ` ` `// find the sum for N/P ` ` ` `return` `(sumUptoN + func(N / P, P) - ` ` ` `sumOfMultiplesOfP); ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` ` ` `// Given N and P ` ` ` `int` `N = ` `10` `, P = ` `5` `; ` ` ` ` ` `// Function call ` ` ` `System.out.println(func(N, P)); ` `} ` `} ` ` ` `// This code is contributed by Rajput-Ji ` |

*chevron_right*

*filter_none*

## C#

`// C# program for the above approach ` `using` `System; ` ` ` `class` `GFG{ ` ` ` `// Function to find the sum of largest ` `// divisors of numbers in range 1 to N ` `// not divisible by prime number P ` `static` `int` `func(` `int` `N, ` `int` `P) ` `{ ` ` ` ` ` `// Total sum upto N ` ` ` `int` `sumUptoN = (N * (N + 1) / 2); ` ` ` `int` `sumOfMultiplesOfP; ` ` ` ` ` `// If no multiple of P exist up to N ` ` ` `if` `(N < P) ` ` ` `{ ` ` ` `return` `sumUptoN; ` ` ` `} ` ` ` ` ` `// If only P itself is in the range ` ` ` `// from 1 to N ` ` ` `else` `if` `((N / P) == 1) ` ` ` `{ ` ` ` `return` `sumUptoN - P + 1; ` ` ` `} ` ` ` ` ` `// Sum of those that are divisible by P ` ` ` `sumOfMultiplesOfP = ((N / P) * (2 * P + ` ` ` `(N / P - 1) * P)) / 2; ` ` ` ` ` `// Recursively function call to ` ` ` `// find the sum for N/P ` ` ` `return` `(sumUptoN + func(N / P, P) - ` ` ` `sumOfMultiplesOfP); ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` ` ` `// Given N and P ` ` ` `int` `N = 10, P = 5; ` ` ` ` ` `// Function call ` ` ` `Console.WriteLine(func(N, P)); ` `} ` `} ` ` ` `// This code is contributed by Amit Katiyar ` |

*chevron_right*

*filter_none*

**Output:**

43

**Time Complexity:** O(N) **Auxiliary Space:** 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:

- Largest Divisor of a Number not divisible by a perfect square
- Count of numbers upto M divisible by given Prime Numbers
- Sum of the numbers upto N that are divisible by 2 or 5
- Sum of largest divisible powers of p (a prime number) in a range
- Sum of greatest odd divisor of numbers in given range
- Largest Divisor for each element in an array other than 1 and the number itself
- Smallest prime divisor of a number
- Smallest composite number not divisible by first N prime numbers
- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
- Absolute Difference between the Sum of Non-Prime numbers and Prime numbers of an Array
- Count prime numbers that can be expressed as sum of consecutive prime numbers
- Largest odd divisor Game to check which player wins
- Maximize count of equal numbers in Array of numbers upto N by replacing pairs with their sum
- Largest N digit number divisible by given three numbers
- Count all prime numbers in a given range whose sum of digits is also prime
- Check if a prime number can be expressed as sum of two Prime Numbers
- Check whether the sum of prime elements of the array is prime or not
- Check if LCM of array elements is divisible by a prime number or not
- Split a number as sum of K numbers which are not divisible by K
- Largest number not greater than N which can become prime after rearranging its digits

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.