- divisors, factor, Mathematical, Tree

Height of Factor Tree for a given number

Given a positive integer N, the task is to find the height of the Factor Tree of the given integer N.Examples:Input: N = 20Output: 3Explaination: The height of the Factor Tree of 20 shown in the image below is 3. Input: N = 48Output: 5Approach: The given problem can be solved by using the steps discussed below:Initialize a variable, say height as 0 that stores the height of the Factor Tree of the given integer N.Iterate over all the values of i in the range [2, √N] and perform the following steps:Find the smallest divisor of N and if it exists, then increment the value of height by 1.If a divisor i exists, then repeat the above step by updating the value of N to N / i, until N > 1.If no divisors of N exist break the loop.After completing the above steps, print the value of height as the answer.Below is the implementation of the above approach:C++  #include using namespace std;  int factorTree(int N){        int height = 0;          while (N > 1) {                        bool flag = false;                          for (int i = 2; i