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 = 2
Output: 22
Explanation: 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
8 1
Sum of all divisors with given constraint = 22.Input: N = 10, P = 5
Output: 43
Explanation: 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(N3/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; } |
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 |
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 |
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 largest divisible powers of p (a prime number) in a range
- Largest N digit number divisible by given three numbers
- Largest number in [2, 3, .. n] which is co-prime with numbers in [2, 3, .. m]
- Sum of the numbers upto N that are divisible by 2 or 5
- Smallest prime divisor of a number
- Largest Divisor for each element in an array other than 1 and the number itself
- Sum and product of k smallest and k largest prime numbers in the array
- Largest K digit number divisible by X
- Largest number with the given set of N digits that is divisible by 2, 3 and 5
- Largest number divisible by 90 that can be made using 0 and 5
- Check if a prime number can be expressed as sum of two Prime Numbers
- Check if a number is divisible by all prime divisors of another number
- C++ Program for Largest K digit number divisible by X
- Largest number made up of X and Y with count of X divisible by Y and of Y by X
- Largest number smaller than or equal to N divisible by K
- Largest number that divides x and is co-prime with y
- Largest number with prime digits
- Java Program for Largest K digit number divisible by X
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.

