Check if a number can be expressed as sum of two Perfect powers | C # program for the above approach ; Function that returns true if n can be written as a ^ m + b ^ n ; Taking isSum bool array for check the sum exist or not ; To store perfect squares ; Initially all sums as false ; If sum exist then push that sum into perfect square vector ; Mark all perfect powers as false ; Traverse each perfectPowers ; Calculating Sum with perfect powers array ; Driver Code ; Given number n ; Function call
using System ; using System . Collections . Generic ; class GFG { static bool isSumOfPower ( int n ) { bool [ ] isSum = new bool [ n + 1 ] ; List < int > perfectPowers = new List < int > ( ) ; perfectPowers . Add ( 1 ) ; for ( int i = 0 ; i < ( n + 1 ) ; i ++ ) { isSum [ i ] = false ; } for ( int i = 2 ; i < ( n + 1 ) ; i ++ ) { if ( isSum [ i ] == true ) { perfectPowers . Add ( i ) ; continue ; } for ( int j = i * i ; j > 0 && j < ( n + 1 ) ; j *= i ) { isSum [ j ] = true ; } } for ( int i = 0 ; i < perfectPowers . Count ; i ++ ) { isSum [ perfectPowers [ i ] ] = false ; } for ( int i = 0 ; i < perfectPowers . Count ; i ++ ) { for ( int j = i ; j < perfectPowers . Count ; j ++ ) { int sum = perfectPowers [ i ] + perfectPowers [ j ] ; if ( sum < ( n + 1 ) ) isSum [ sum ] = true ; } } return isSum [ n ] ; } public static void Main ( String [ ] args ) { int n = 9 ; if ( isSumOfPower ( n ) ) { Console . Write ( " true STRNEWLINE " ) ; } else { Console . Write ( " false STRNEWLINE " ) ; } } }