Magnanimous Numbers | Java implementation to check if a number is Magnanimous ; Function to check if n is prime ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function to check if the number is Magnanimous or not ; Converting the number to string ; Finding length of string ; Number should not be of single digit ; Loop to find all left and right part of the string ; Driver code
class GFG { static boolean isPrime ( int n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; return true ; } static boolean isMagnanimous ( int N ) { String s = Integer . toString ( N ) ; int l = s . length ( ) ; if ( l < 2 ) return false ; for ( int i = 0 ; i < l - 1 ; i ++ ) { String left = s . substring ( 0 , i + 1 ) ; String right = s . substring ( i + 1 ) ; int x = Integer . valueOf ( left ) ; int y = Integer . valueOf ( right ) ; if ( ! isPrime ( x + y ) ) return false ; } return true ; } public static void main ( String [ ] args ) { int N = 12 ; if ( isMagnanimous ( N ) ) System . out . print ( "YesNEW_LINE"); else System . out . print ( "NoNEW_LINE"); } }