- Related Questions & Answers
- Count pairs (a, b) whose sum of cubes is N (a^3 + b^3 = N) in C++
- Count number of pairs (A <= N, B <= N) such that gcd (A , B) is B in C++
- Count number of triplets (a, b, c) such that a^2 + b^2 = c^2 and 1<=a<=b<=c<= n in C++
- Find 2^(2^A) % B in C++
- Count of pairs from 1 to a and 1 to b whose sum is divisible by N in C++
- Sum of the series 2^0 + 2^1 + 2^2 +...+ 2^n in C++
- Minimize the sum of squares of sum of N/2 paired formed by N numbers in C++
- Sum of series 1^2 + 3^2 + 5^2 + . . . + (2*n - 1)^2 in C++
- Sum of series 1^2 + 3^2 + 5^2 + . . . + (2*n – 1)^2
- Find Cube Pairs - (A n^(2/3) Solution) in C++
- Sum of the digits of a number N written in all bases from 2 to N/2 in C++
- Program to find sum of series 1*2*3 + 2*3*4+ 3*4*5 + . . . + n*(n+1)*(n+2) in C++
- C++ program to find the sum of the series (1/a + 2/a^2 + 3/a^3 + … + n/a^n)
- Count triplet pairs (A, B, C) of points in 2-D space that satisfy the given condition in C++
- Sum of the series 1^1 + 2^2 + 3^3 + ... + n^n using recursion in C++

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

We are given a number N. The goal is to find ordered pairs of positive numbers such that the sum of their squares is N.

We will do this by finding solutions to the equation a^{2}+ b^{2} = N. Where a is not more than square root of N and b can be calculated as square root of (N-a^{2}).

Let’s understand with examples.

**Input**

N=100

**Output**

Count of pairs of (a,b) where a^3+b^3=N: 2

**Explanation**

Pairs will be (6,8) and (8,6). 62+82=36+64=100

**Input**

N=11

**Output**

Count of pairs of (a,b) where a^3+b^3=N: 0

**Explanation**

No such pairs possible.

We take integer N.

Function squareSum(int n) takes n and returns the count of ordered pairs with sum of squares as n.

Take the initial variable count as 0 for pairs.

Traverse using for loop to find a.

Start from a=1 to a<=sqrt(n) which is square root of n.

Calculate square of b as n-pow(a,2).

Calculate b as sqrt(bsquare)

If pow(b,2)==bsquare. Increment count by 1.

At the end of all loops count will have a total number of such pairs.

Return the count as result.

#include <bits/stdc++.h> #include <math.h> using namespace std; int squareSum(int n){ int count = 0; for (int a = 1; a <= sqrt(n); a++){ int bsquare=n - (pow(a,2)); int b = sqrt(bsquare); if(pow(b,2)==bsquare){ count++; cout<<a; } } return count; } int main(){ int N =5; cout <<"Count of pairs of (a,b) where a^2+b^2=N: "<<squareSum(N); return 0; }

If we run the above code it will generate the following output −

Count of pairs of (a,b) where a^2+b^2=N: 122

Advertisements