- Related Questions & Answers
- Count pairs (a, b) whose sum of squares is N (a^2 + b^2 = N) in C++
- Count number of pairs (A <= N, B <= N) such that gcd (A , B) is B in C++
- Count of pairs from 1 to a and 1 to b whose sum is divisible by N in C++
- Find Cube Pairs - (A n^(2/3) Solution) 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)
- Program to find sum of series 1 + 2 + 2 + 3 + 3 + 3 + .. + n in C++
- Sum of the series 1^1 + 2^2 + 3^3 + ... + n^n using recursion in C++
- Sum of the Series 1 + x/1 + x^2/2 + x^3/3 + .. + x^n/n 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++
- Program to find Sum of a Series a^1/1! + a^2/2! + a^3/3! + a^4/4! +…….+ a^n/n! in C++
- C++ Program to find the sum of a Series 1/1! + 2/2! + 3/3! + 4/4! + …… n/n!
- Construct a Turing Machine for L = {a^n b^n | n>=1}
- Program to find N-th term of series a, b, b, c, c, c…in C++
- Construct Deterministic PDA for a^n b^n where n>=1

- 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 cubes is N.

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

Let’s understand with examples.

**Input**

N=35

**Output**

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

**Explanation**

Pairs will be (2,3) and (3,2). 23+33=8+27=35

**Input**

N=100

**Output**

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

**Explanation**

No such pairs possible.

We take integer N.

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

Take the initial variable count as 0 for pairs.

Traverse using for loop to find a.

Start from a=1 to a<=cbrt(n) which is cube root of n.

Calculate cube of b as n-pow(a,3).

Calculate b as cbrt(bcube)

If pow(b,3)==bcube. 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 cubeSum(int n){ int count = 0; for (int a = 1; a < cbrt(n); a++){ int bcube=n - (pow(a,3)); int b = cbrt(bcube); if(pow(b,3)==bcube) { count++; } } return count; } int main(){ int N = 35; cout <<"Count of pairs of (a,b) where a^3+b^3=N: "<<cubeSum(N); return 0; }

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

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

Advertisements