Ugly Numbers | This function divides a by greatest divisible power of b ; Function to check if a number is ugly or not ; Function to get the nth ugly number ; ugly number count ; Check for all integers untill ugly count becomes n ; Driver Code
< ? php function maxDivide ( $ a , $ b ) { while ( $ a % $ b == 0 ) $ a = $ a / $ b ; return $ a ; } function isUgly ( $ no ) { $ no = maxDivide ( $ no , 2 ) ; $ no = maxDivide ( $ no , 3 ) ; $ no = maxDivide ( $ no , 5 ) ; return ( $ no == 1 ) ? 1 : 0 ; } function getNthUglyNo ( $ n ) { $ i = 1 ; $ count = 1 ; while ( $ n > $ count ) { $ i ++ ; if ( isUgly ( $ i ) ) $ count ++ ; } return $ i ; } $ no = getNthUglyNo ( 150 ) ; echo "150th ▁ ugly ▁ no . ▁ is ▁ " . $ no ; ? >