Height of Factor Tree for a given number | Function to find the height of the Factor Tree of the integer N ; Stores the height of Factor Tree ; Loop to iterate over values of N ; Stores if there exist a factor of N or not ; Loop to find the smallest factor of N ; If i is a factor of N ; Increment the height ; If there are no factors of N i . e , N is prime , break loop ; Return Answer ; Driver Code
function factorTree ( N ) { let height = 0 ; while ( N > 1 ) { let flag = false ; for ( let i = 2 ; i <= Math . sqrt ( N ) ; i ++ ) { if ( N % i == 0 ) { N = Math . floor ( N / i ) ; flag = true ; break ; } } height ++ ; if ( ! flag ) { break ; } } return height ; } let N = 48 ; document . write ( factorTree ( N ) ) ;