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 = 12Output:40Explanation:

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 = 2Output:3Explanation:

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 that 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 x_{1}, x_{2}, x_{3}, …. x_{n}as the different integers from 1 to N such that their GCD with N is d. - Since,
**GCD(x**then_{i}, N) = d**GCD(x**_{i}/d, N/d) = 1 - So, 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++

`// C++ Program to find the Sum ` `// of GCD of all integers up to N ` `// with N itself ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to Find Sum of ` `// GCD of each numbers ` `int` `getCount(` `int` `d, ` `int` `n) ` `{ ` ` ` ` ` `int` `no = n / d; ` ` ` ` ` `int` `result = no; ` ` ` ` ` `// Consider all prime factors ` ` ` `// of no. and subtract their ` ` ` `// multiples from result ` ` ` `for` `(` `int` `p = 2; p * p <= no; ++p) { ` ` ` ` ` `// Check if p is a prime factor ` ` ` `if` `(no % p == 0) { ` ` ` ` ` `// If yes, then update no ` ` ` `// and result ` ` ` `while` `(no % p == 0) ` ` ` `no /= p; ` ` ` `result -= result / p; ` ` ` `} ` ` ` `} ` ` ` ` ` `// If no has a prime factor greater ` ` ` `// than sqrt(n) then at-most one such ` ` ` `// prime factor exists ` ` ` `if` `(no > 1) ` ` ` `result -= result / no; ` ` ` ` ` `// Return the result ` ` ` `return` `result; ` `} ` ` ` `// Finding GCD of pairs ` `int` `sumOfGCDofPairs(` `int` `n) ` `{ ` ` ` `int` `res = 0; ` ` ` ` ` `for` `(` `int` `i = 1; i * i <= n; i++) { ` ` ` `if` `(n % i == 0) { ` ` ` `// Calculate the divisors ` ` ` `int` `d1 = i; ` ` ` `int` `d2 = n / i; ` ` ` ` ` `// Return count of numbers ` ` ` `// from 1 to N with GCD d with N ` ` ` `res += d1 * getCount(d1, n); ` ` ` ` ` `// Check if d1 and d2 are ` ` ` `// equal then skip this ` ` ` `if` `(d1 != d2) ` ` ` `res += d2 * getCount(d2, n); ` ` ` `} ` ` ` `} ` ` ` ` ` `return` `res; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 12; ` ` ` ` ` `cout << sumOfGCDofPairs(n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find the Sum ` `// of GCD of all integers up to N ` `// with N itself ` `class` `GFG{ ` ` ` `// Function to Find Sum of ` `// GCD of each numbers ` `static` `int` `getCount(` `int` `d, ` `int` `n) ` `{ ` ` ` `int` `no = n / d; ` ` ` `int` `result = no; ` ` ` ` ` `// Consider all prime factors ` ` ` `// of no. and subtract their ` ` ` `// multiples from result ` ` ` `for` `(` `int` `p = ` `2` `; p * p <= no; ++p) ` ` ` `{ ` ` ` ` ` `// Check if p is a prime factor ` ` ` `if` `(no % p == ` `0` `) ` ` ` `{ ` ` ` ` ` `// If yes, then update no ` ` ` `// and result ` ` ` `while` `(no % p == ` `0` `) ` ` ` `no /= p; ` ` ` `result -= result / p; ` ` ` `} ` ` ` `} ` ` ` ` ` `// If no has a prime factor greater ` ` ` `// than Math.sqrt(n) then at-most one such ` ` ` `// prime factor exists ` ` ` `if` `(no > ` `1` `) ` ` ` `result -= result / no; ` ` ` ` ` `// Return the result ` ` ` `return` `result; ` `} ` ` ` `// Finding GCD of pairs ` `static` `int` `sumOfGCDofPairs(` `int` `n) ` `{ ` ` ` `int` `res = ` `0` `; ` ` ` ` ` `for` `(` `int` `i = ` `1` `; i * i <= n; i++) ` ` ` `{ ` ` ` `if` `(n % i == ` `0` `) ` ` ` `{ ` ` ` ` ` `// Calculate the divisors ` ` ` `int` `d1 = i; ` ` ` `int` `d2 = n / i; ` ` ` ` ` `// Return count of numbers ` ` ` `// from 1 to N with GCD d with N ` ` ` `res += d1 * getCount(d1, n); ` ` ` ` ` `// Check if d1 and d2 are ` ` ` `// equal then skip this ` ` ` `if` `(d1 != d2) ` ` ` `res += d2 * getCount(d2, n); ` ` ` `} ` ` ` `} ` ` ` `return` `res; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `n = ` `12` `; ` ` ` ` ` `System.out.print(sumOfGCDofPairs(n)); ` `} ` `} ` ` ` `// This code is contributed by Amit Katiyar ` |

*chevron_right*

*filter_none*

**Output:**

40

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

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:

- Count of numbers upto M divisible by given Prime Numbers
- Sum of the numbers upto N that are divisible by 2 or 5
- Count of numbers upto M with GCD equals to K when paired with M
- Count of Octal numbers upto N digits
- Sum of Fibonacci numbers at even indexes upto N terms
- Program to print tetrahedral numbers upto Nth term
- Program to print pentatope numbers upto Nth term
- Check if Pascal's Triangle is possible with a complete layer by using numbers upto N
- Sum of largest divisor of numbers upto N not divisible by given prime number P
- Count of numbers upto N having absolute difference of at most K between any two adjacent digits
- Count numbers upto N which are both perfect square and perfect cube
- Count of numbers upto N digits formed using digits 0 to K-1 without any adjacent 0s
- Count numbers < = N whose difference with the count of primes upto them is > = K
- Sum of series 2/3 - 4/5 + 6/7 - 8/9 + ------- upto n terms
- Sum of the series (1*2) + (2*3) + (3*4) + ...... upto n terms
- Sum of the Tan(x) expansion upto N terms
- Find the sum of series 0.X + 0.XX + 0.XXX +... upto k terms
- Program to get the Sum of series: 1 - x^2/2! + x^4/4! -.... upto nth term
- Find sum of the series ?3 + ?12 +......... upto N terms
- Find the sum of series 3, -6, 12, -24 . . . upto N terms

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.