
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Find Cube Pairs - (A n^(2/3) Solution) in C++
A number is given. We have to find two pairs, that can represent the number as sum of two cubes. So we have to find two pairs (a, b) and (c, d) such that the given number n can be expressed as n = a3 + b3 = c3 + d3
The idea is simple. Here every number a, b, c and d are all less than n1/3. For every distinct pair (x, y) formed by number less than n1/3, if their sum (x3 + y3) is equal to the given number, we store them into hash table with the sum value as key, then if the same sum comes again, them simply print each pair
Algorithm
getPairs(n): begin cube_root := cube root of n map as int type key and pair type value for i in range 1 to cube_root, do for j in range i + 1 to cube_root, do sum = i3 + j3 if sum is not same as n, then skip next part, go to second iteration if sum is present into map, then print pair, and (i, j), else insert (i,j) with corresponding sum into the map done done end
Example
#include <iostream> #include <cmath> #include <map> using namespace std; int getPairs(int n){ int cube_root = pow(n, 1.0/3.0); map<int, pair<int, int> > my_map; for(int i = 1; i<cube_root; i++){ for(int j = i + 1; j<= cube_root; j++){ int sum = i*i*i + j*j*j; if(sum != n) continue; if(my_map.find(sum) != my_map.end()){ cout << "(" << my_map[sum].first << ", " << my_map[sum].second << ") and (" << i << ", " << j << ")" << endl; }else{ my_map[sum] = make_pair(i, j); } } } } int main() { int n = 13832; getPairs(n); }
Output
(2, 24) and (18, 20)
- Related Articles
- Count pairs (a, b) whose sum of cubes is N (a^3 + b^3 = N) in C++
- Find (1^n + 2^n + 3^n + 4^n) mod 5 in C++
- Matrix Chain Multiplication (A O(N^3) Solution) in C++
- Count pairs (a, b) whose sum of squares is N (a^2 + b^2 = N) 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++
- 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++ Programe to find n-th term in series 1 2 2 3 3 3 4
- C++ Program to find the sum of a Series 1/1! + 2/2! + 3/3! + 4/4! + …… n/n!
- Find value of (n^1 + n^2 + n^3 + n^4) mod 5 for given n in C++
- Find the solution set of $x^{2}-3 x+2
- Program to find sum of 1 + x/2! + x^2/3! +…+x^n/(n+1)! in C++
- Find Sum of Series 1^2 - 2^2 + 3^2 - 4^2 ... upto n terms in C++
- C++ program to find the sum of the series 1/1! + 2/2! + 3/3! + 4/4! +…….+ n/n!
- C++ program to find the sum of the series 1 + 1/2^2 + 1/3^3 + …..+ 1/n^n

Advertisements