Magnanimous Numbers | 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
function isPrime ( n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( let i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; return true ; } function isMagnanimous ( N ) { let s = N . toString ( ) ; let l = s . length ; if ( l < 2 ) return false ; for ( let i = 0 ; i < l - 1 ; i ++ ) { let left = s . substring ( 0 , i + 1 ) ; let right = s . substring ( i + 1 ) ; let x = parseInt ( left ) ; let y = parseInt ( right ) ; if ( ! isPrime ( x + y ) ) return false ; } return true ; } let N = 12 ; if ( isMagnanimous ( N ) ) document . write ( " " ) ; else document . write ( " " ) ;