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 ; method 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 method ; Loop to Count no . of pairs satisfying a ^ 3 + b ^ 3 = i for N = 1 to 10
using System ; class Test { static int countPairs ( int N ) { int count = 0 ; for ( int i = 1 ; i <= Math . Pow ( N , ( 1.0 / 3.0 ) ) ; i ++ ) { int cb = i * i * i ; int diff = N - cb ; int cbrtDiff = ( int ) Math . Pow ( diff , ( 1.0 / 3.0 ) ) ; if ( cbrtDiff * cbrtDiff * cbrtDiff == diff ) count ++ ; } return count ; } public static void Main ( ) { for ( int i = 1 ; i <= 10 ; i ++ ) Console . Write ( " For ▁ n ▁ = ▁ " + i + " , ▁ " + + countPairs ( i ) + " ▁ pair ▁ exists " + " STRNEWLINE " ) ; } }