Height of Factor Tree for a given number | Python 3 program for the above approach ; 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
from math import sqrt NEW_LINE def factorTree ( N ) : NEW_LINE INDENT height = 0 NEW_LINE while ( N > 1 ) : NEW_LINE INDENT flag = False NEW_LINE for i in range ( 2 , int ( sqrt ( N ) ) + 1 , 1 ) : NEW_LINE INDENT if ( N % i == 0 ) : NEW_LINE INDENT N = N // i NEW_LINE flag = True NEW_LINE break NEW_LINE DEDENT DEDENT height += 1 NEW_LINE if ( flag == False ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT return height NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 48 NEW_LINE print ( factorTree ( N ) ) NEW_LINE DEDENT