Find the n | function to find nth number made of even digits only ; If n = 1 return 0 ; vector to store the digits when converted into base 5 ; Reduce n to n - 1 to exclude 0 ; Reduce n to base 5 number and store digits ; pushing the digits into vector ; variable to represent the number after converting it to base 5. Since the digits are be in reverse order , we traverse vector from back ; return 2 * result ( to convert digits 0 , 1 , 2 , 3 , 4 to 0 , 2 , 4 , 6 , 8. ; Driver Code
< ? php function findNthEvenDigitNumber ( $ n ) { if ( $ n == 1 ) return 0 ; $ v = array ( ) ; $ n = $ n - 1 ; while ( $ n > 0 ) { array_push ( $ v , $ n % 5 ) ; $ n = ( int ) ( $ n / 5 ) ; } $ result = 0 ; for ( $ i = count ( $ v ) - 1 ; $ i >= 0 ; $ i -- ) { $ result = $ result * 10 ; $ result = $ result + $ v [ $ i ] ; } return 2 * $ result ; } echo findNthEvenDigitNumber ( 2 ) . " STRNEWLINE " ; echo findNthEvenDigitNumber ( 10 ) . " STRNEWLINE " ? >