Check whether a number can be represented by sum of two squares | An efficient approach based implementation to find if a number can be written as sum of two squares . ; function to check if there exist two numbers sum of whose squares is n . ; store square value in hashmap ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static bool sumSquare ( int n ) { Dictionary < int , int > s = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i * i <= n ; ++ i ) { s . Add ( i * i , 1 ) ; if ( s . ContainsKey ( n - i * i ) ) { Console . WriteLine ( ( int ) Math . Sqrt ( n - i * i ) + " ^ 2 ▁ + ▁ " + i + " ^ 2" ) ; return true ; } } return false ; } public static void Main ( String [ ] args ) { int n = 169 ; Console . WriteLine ( sumSquare ( n ) ? " YES " : " NO " ) ; } }