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
1mo

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!
Add answer anonymously...
Quikr Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

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

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter