Check if a number can be represented as sum of two positive perfect cubes | C # program for the above approach ; Function to check if N can be represented as sum of two perfect cubes or not ; Stores the perfect cubes of first N natural numbers ; Traverse the map ; Stores first number ; Stores second number ; Search the pair for the first number to obtain sum N from the Map ; If N cannot be represented as sum of two positive perfect cubes ; Driver Code ; Function call to check if N can be represented as sum of two perfect cubes or not
using System ; using System . Collections . Generic ; using System . Linq ; class GFG { public static void sumOfTwoPerfectCubes ( int N ) { Dictionary < int , int > cubes = new Dictionary < int , int > ( ) ; for ( int i = 1 ; i * i * i <= N ; i ++ ) cubes . Add ( ( i * i * i ) , i ) ; var val = cubes . Keys . ToList ( ) ; foreach ( var key in val ) { int firstNumber = cubes [ 1 ] ; int secondNumber = N - cubes [ 1 ] ; if ( cubes . ContainsKey ( secondNumber ) ) { Console . Write ( " True " ) ; return ; } } Console . Write ( " False " ) ; } static public void Main ( ) { int N = 28 ; sumOfTwoPerfectCubes ( N ) ; } }