Sum of largest divisor of numbers upto N not divisible by given prime number P
Given a number N and a prime number P, the task is to find the sum of the 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 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++
#include <bits/stdc++.h>
using namespace std;
int func( int N, int P)
{
int sumUptoN = (N * (N + 1) / 2);
int sumOfMultiplesOfP;
if (N < P) {
return sumUptoN;
}
else if ((N / P) == 1) {
return sumUptoN - P + 1;
}
sumOfMultiplesOfP
= ((N / P) * (2 * P + (N / P - 1) * P)) / 2;
return (sumUptoN
+ func(N / P, P)
- sumOfMultiplesOfP);
}
int main()
{
int N = 10, P = 5;
cout << func(N, P) << "\n" ;
return 0;
}
|
Java
import java.io.*;
public class GFG{
static int func( int N, int P)
{
int sumUptoN = (N * (N + 1 ) / 2 );
int sumOfMultiplesOfP;
if (N < P)
{
return sumUptoN;
}
else if ((N / P) == 1 )
{
return sumUptoN - P + 1 ;
}
sumOfMultiplesOfP = ((N / P) * ( 2 * P +
(N / P - 1 ) * P)) / 2 ;
return (sumUptoN + func(N / P, P) -
sumOfMultiplesOfP);
}
public static void main(String[] args)
{
int N = 10 , P = 5 ;
System.out.println(func(N, P));
}
}
|
Python3
def func(N, P):
sumUptoN = (N * (N + 1 ) / 2 );
sumOfMultiplesOfP = 0 ;
if (N < P):
return sumUptoN;
elif ((N / P) = = 1 ):
return sumUptoN - P + 1 ;
sumOfMultiplesOfP = (((N / P) *
( 2 * P +
(N / P - 1 ) *
P)) / 2 );
return (sumUptoN +
func(N / P, P) -
sumOfMultiplesOfP);
if __name__ = = '__main__' :
N = 10 ;
P = 5 ;
print (func(N, P));
|
C#
using System;
class GFG{
static int func( int N, int P)
{
int sumUptoN = (N * (N + 1) / 2);
int sumOfMultiplesOfP;
if (N < P)
{
return sumUptoN;
}
else if ((N / P) == 1)
{
return sumUptoN - P + 1;
}
sumOfMultiplesOfP = ((N / P) * (2 * P +
(N / P - 1) * P)) / 2;
return (sumUptoN + func(N / P, P) -
sumOfMultiplesOfP);
}
public static void Main(String[] args)
{
int N = 10, P = 5;
Console.WriteLine(func(N, P));
}
}
|
Javascript
<script>
function func(N, P)
{
let sumUptoN = (N * (N + 1) / 2);
let sumOfMultiplesOfP;
if (N < P)
{
return sumUptoN;
}
else if ((N / P) == 1)
{
return sumUptoN - P + 1;
}
sumOfMultiplesOfP = ((N / P) * (2 * P +
(N / P - 1) * P)) / 2;
return (sumUptoN + func(N / P, P) -
sumOfMultiplesOfP);
}
let N = 10, P = 5;
document.write(func(N, P));
</script>
|
Time Complexity: O(logPN)
Auxiliary Space: O(1)
Last Updated :
23 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...