Count pairs ( a , b ) whose sum of cubes is N ( a ^ 3 + b ^ 3 = N ) | C ++ program to count pairs whose sum cubes is N ; Function to count the pairs satisfying a ^ 3 + b ^ 3 = N ; Check for each number 1 to cbrt ( N ) ; Store cube of a number ; Subtract the cube from given N ; Check if the difference is also a perfect cube ; If yes , then increment count ; Return count ; Driver program ; Loop to Count no . of pairs satisfying a ^ 3 + b ^ 3 = i for N = 1 to 10
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countPairs ( int N ) { int count = 0 ; for ( int i = 1 ; i <= cbrt ( N ) ; i ++ ) { int cb = i * i * i ; int diff = N - cb ; int cbrtDiff = cbrt ( diff ) ; if ( cbrtDiff * cbrtDiff * cbrtDiff == diff ) count ++ ; } return count ; } int main ( ) { for ( int i = 1 ; i <= 10 ; i ++ ) cout << " For ▁ n ▁ = ▁ " << i << " , ▁ " << countPairs ( i ) << " ▁ pair ▁ exists STRNEWLINE " ; return 0 ; }