Wednesday, June 29, 2022

Divisor's problem

 Divisor's problem



Problem Statement

Sara is solving a math problem in which she has given an integer N and her task is to find the number of operations required to convert N into 1.
Where in one operation you replace the number with its second-highest divisor.

static int DivisorProblem(int N){
    //Enter your code here
    int step = 0;
    while(N!=1){
        if ((N%2)==0){
            N=N/2;
            step += 1;
        }else if((N%3)==0){
            N=N/3;
            step += 1;
        }else if((N%5)==0){
            N=N/5;
            step += 1;
        }else if((N%7)==0){
            N=N/7;
            step += 1;
        }else{
            N=1;
            step += 1;
        }
    }    
        return step;
    }

No comments:

Post a Comment

ads vert

Basic HTML Tables - Layout, HTML Tables, Attributes, Aside, Footer, Tr tag, Td tag, Th tag, Tbody

  Basic HTML Tables - Layout, HTML Tables, Attributes, Aside, Footer, Tr tag, Td tag, Th tag, Tbody < table >      < thead >    ...