GCD Sum Problem Statement

Given an integer 'N', the task is to find the sum of the greatest common divisor (GCD) of all pairs (i, j) such that 1 <= i < j <= N.

Input:

T (the number of test cases)
For each test case:
N (an integer)

Output:

An integer representing the sum of GCD of all pairs of numbers from 1 to N.
A new line for each test case.

Example:

Input:
T = 1
N = 3
Output:
3
Explanation:

For N = 3, the pairs are (1, 2), (1, 3), and (2, 3). The GCDs are 1, 1, and 1 respectively; sum is 3.

Constraints:

  • 1 <= T <= 5
  • 1 <= N <= 5000
Note:

You do not need to perform input/output operations as it has been handled. Implement the function and return the result.

AnswerBot
4mo

Calculate the sum of GCD of all pairs of numbers from 1 to N.

  • Iterate through all pairs of numbers from 1 to N and calculate GCD

  • Add all the calculated GCDs to get the final sum

  • Optimize the GCD calculat...read more

Help your peers!
Select
Add answer anonymously...
Quikr Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits