Largest number less than N whose each digit is prime number | Number is given as string . ; We stop traversing digits , once it become smaller than current number . For that purpose we use small variable . ; Array indicating if index i ( represents a digit ) is prime or not . ; Store largest ; If there is only one character , return the largest prime less than the number ; If number starts with 1 , return number consisting of 7 ; Traversing each digit from right to left Continue traversing till the number we are forming will become less . ; If digit is prime , copy it simply . ; If not prime , copy the largest prime less than current number ; If not prime , and there is no largest prime less than current prime ; Make current digit as 7 Go left of the digit and make it largest prime less than number . Continue do that until we found a digit which has some largest prime less than it ; If the given number is itself a prime . ; Make last digit as highest prime less than given digit . ; If there is no highest prime less than current digit . ; Once one digit become less than any digit of input replace 7 ( largest 1 digit prime ) till the end of digits of number ; If number include 0 in the beginning , ignore them . Case like 2200 ; Driver Program
function PrimeDigitNumber ( N , size ) { let ans = new Array ( size ) ; let ns = 0 ; let small = 0 ; let i ; let p = [ 0 , 0 , 1 , 1 , 0 , 1 , 0 , 1 , 0 , 0 ] ; let prevprime = [ 0 , 0 , 0 , 2 , 3 , 3 , 5 , 5 , 7 , 7 ] ; if ( size == 1 ) { ans [ 0 ] = String . fromCharCode ( prevprime [ N [ 0 ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] + ' ' . charCodeAt ( 0 ) ) ; ans [ 1 ] = ' \0 ' ; return ans ; } if ( N [ 0 ] == ' ' ) { for ( i = 0 ; i < size - 1 ; i ++ ) ans [ i ] = ' ' ; ans [ size - 1 ] = ' \0 ' ; return ans ; } for ( i = 0 ; i < size && small == 0 ; i ++ ) { if ( p [ N [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] == 1 ) { ans [ ns ++ ] = N [ i ] ; } else { if ( p [ N [ i ] - ' ' ] == 0 && prevprime [ N [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] != 0 ) { ans [ ns ++ ] = String . fromCharCode ( prevprime [ N [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] + ' ' . charCodeAt ( 0 ) ) ; small = 1 ; } else if ( p [ N [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] == 0 && prevprime [ N [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] == 0 ) { let j = i ; while ( j > 0 && p [ N [ j ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] == 0 && prevprime [ N [ j ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] == 0 ) { ans [ j ] = N [ j ] = ' ' ; N [ j - 1 ] = String . fromCharCode ( prevprime [ N [ j - 1 ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] + ' ' . charCodeAt ( 0 ) ) ; ans [ j - 1 ] = N [ j - 1 ] ; small = 1 ; j -- ; } i = ns ; } } } if ( small == 0 ) { if ( prevprime [ N [ size - 1 ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] + ' ' . charCodeAt ( 0 ) != ' ' . charCodeAt ( 0 ) ) ans [ size - 1 ] = String . fromCharCode ( prevprime [ N [ size - 1 ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] + ' ' . charCodeAt ( 0 ) ) ; else { let j = size - 1 ; while ( j > 0 && prevprime [ N [ j ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] == 0 ) { ans [ j ] = N [ j ] = ' ' ; N [ j - 1 ] = String . fromCharCode ( prevprime [ N [ j - 1 ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] + ' ' . charCodeAt ( 0 ) ) ; ans [ j - 1 ] = N [ j - 1 ] ; small = 1 ; j -- ; } } } for ( ; ns < size ; ns ++ ) ans [ ns ] = ' ' ; ans [ ns ] = ' \0 ' ; let k = 0 ; while ( ans [ k ] == ' ' ) k ++ ; return ans . join ( " " ) ; } let N = " " . split ( " " ) ; let size = N . length ; document . write ( PrimeDigitNumber ( N , size ) . join ( " " ) ) ;