Sum of GCD of all numbers upto N with N itself
Last Updated :
23 Nov, 2022
Given an integer N, the task is to find the sum of Greatest Common Divisors of all numbers up to N with N itself.
Examples:
Input: N = 12
Output: 40
Explanation:
GCD of [1, 12] = 1, [2, 12] = 2, [3, 12] = 3, [4, 12] = 4, [5, 12] = 1, [6, 12] = 6, [7, 12] = 1, [8, 12] = 4, [9, 12] = 3, [10, 12] = 2, [11, 12] = 1, [12, 12] = 12. The sum is (1 + 2 + 3 + 4 + 1 + 6 + 1 + 4 + 3 + 2 + 1 + 12) = 40.
Input: N = 2
Output: 3
Explanation:
GCD of [1, 2] = 1, [2, 2] = 2 and their sum is 3.
Naive Approach: A simple solution is to iterate over all numbers from 1 to N and find their gcd with N itself and keep on adding them.
Time Complexity: O(N * log N)
Efficient Approach:
To optimize the above-mentioned approach, we need to observe that GCD(i, N) gives one of the divisors of N. So, instead of running a loop from 1 to N, we can check for each divisor of N how many numbers are there with GCD(i, N) same as that divisor.
Illustration:
For example N = 12, its divisors are 1, 2, 3, 4, 6, 12.
Numbers in range [1, 12] whose GCD with 12 is:
- 1 are {1, 5, 7, 11}
- 2 are {2, 10}
- 3 are {3, 9}
- 4 are {4, 8}
- 6 is {6}
- 12 is {12}
So answer is; 1*4 + 2*2 + 3*2 + 4*2 + 6*1 + 12*1 = 40.
- So we have to find the number of integers from 1 to N with GCD d, where d is a divisor of N. Let us consider x1, x2, x3, …. xn as the different integers from 1 to N such that their GCD with N is d.
- Since, GCD(xi, N) = d then GCD(xi/d, N/d) = 1
- So, the count of integers from 1 to N whose GCD with N is d is Euler Totient Function of (N/d).
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int getCount( int d, int n)
{
int no = n / d;
int result = no;
for ( int p = 2; p * p <= no; ++p) {
if (no % p == 0) {
while (no % p == 0)
no /= p;
result -= result / p;
}
}
if (no > 1)
result -= result / no;
return result;
}
int sumOfGCDofPairs( int n)
{
int res = 0;
for ( int i = 1; i * i <= n; i++) {
if (n % i == 0) {
int d1 = i;
int d2 = n / i;
res += d1 * getCount(d1, n);
if (d1 != d2)
res += d2 * getCount(d2, n);
}
}
return res;
}
int main()
{
int n = 12;
cout << sumOfGCDofPairs(n);
return 0;
}
|
Java
class GFG{
static int getCount( int d, int n)
{
int no = n / d;
int result = no;
for ( int p = 2 ; p * p <= no; ++p)
{
if (no % p == 0 )
{
while (no % p == 0 )
no /= p;
result -= result / p;
}
}
if (no > 1 )
result -= result / no;
return result;
}
static int sumOfGCDofPairs( int n)
{
int res = 0 ;
for ( int i = 1 ; i * i <= n; i++)
{
if (n % i == 0 )
{
int d1 = i;
int d2 = n / i;
res += d1 * getCount(d1, n);
if (d1 != d2)
res += d2 * getCount(d2, n);
}
}
return res;
}
public static void main(String[] args)
{
int n = 12 ;
System.out.print(sumOfGCDofPairs(n));
}
}
|
Python3
def getCount(d, n):
no = n / / d;
result = no;
for p in range ( 2 , int ( pow (no, 1 / 2 )) + 1 ):
if (no % p = = 0 ):
while (no % p = = 0 ):
no / / = p;
result - = result / / p;
if (no > 1 ):
result - = result / / no;
return result;
def sumOfGCDofPairs(n):
res = 0 ;
for i in range ( 1 , int ( pow (n, 1 / 2 )) + 1 ):
if (n % i = = 0 ):
d1 = i;
d2 = n / / i;
res + = d1 * getCount(d1, n);
if (d1 ! = d2):
res + = d2 * getCount(d2, n);
return res;
if __name__ = = '__main__' :
n = 12 ;
print (sumOfGCDofPairs(n));
|
C#
using System;
class GFG{
static int getCount( int d, int n)
{
int no = n / d;
int result = no;
for ( int p = 2; p * p <= no; ++p)
{
if (no % p == 0)
{
while (no % p == 0)
no /= p;
result -= result / p;
}
}
if (no > 1)
result -= result / no;
return result;
}
static int sumOfGCDofPairs( int n)
{
int res = 0;
for ( int i = 1; i * i <= n; i++)
{
if (n % i == 0)
{
int d1 = i;
int d2 = n / i;
res += d1 * getCount(d1, n);
if (d1 != d2)
res += d2 * getCount(d2, n);
}
}
return res;
}
public static void Main(String[] args)
{
int n = 12;
Console.Write(sumOfGCDofPairs(n));
}
}
|
Javascript
<script>
function getCount(d, n)
{
let no = Math.floor(n / d);
let result = no;
for (let p = 2; p * p <= no; ++p) {
if (no % p == 0) {
while (no % p == 0)
no = Math.floor(no / p);
result = Math.floor(result - result / p);
}
}
if (no > 1)
result = Math.floor(result - result / no);
return result;
}
function sumOfGCDofPairs(n)
{
let res = 0;
for (let i = 1; i * i <= n; i++) {
if (n % i == 0) {
let d1 = i;
let d2 = Math.floor(n / i);
res += d1 * getCount(d1, n);
if (d1 != d2)
res += d2 * getCount(d2, n);
}
}
return res;
}
let n = 12;
document.write(sumOfGCDofPairs(n));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...