int floorSqrt(int x){
        if (x == 0 || x == 1)
        return x;
      
        int i = 1, result = 1;
        while (result <= x){
          i++;
          result = i * i;
        }
        return i - 1;
      }

      Cpp language logo
      Floor of square root

      Axt

      This code calculates the floor square root of a number. It starts by checking to see if the number is zero or one. If it is zero, or if it is one, then the function returns the number itself. If the number is not zero or one, then it calculates the square root of the number by counting up from 1, and dividing that number by the number that the function is calculating the square root of. Once that calculation is done, it sets a variable called i to 1, and the function keeps counting up from 1 while the variable i is set to the number that the function is calculating the floor square root of. When i reaches the number that the function is calculating the floor square root of, the function sets the variable result to that number, and the function loops back to the top of the code. The code then returns the value of i, which is the value of the floor square root of the number that was calculated.

      Shortcut: fsqrt

      0 Comments

        Add Comment

        Log in to add a comment

        Codiga - All rights reserved 2022.