Dado un número N. En cada paso, reste el cuadrado perfecto más grande (≤ N) de N. Repita este paso mientras N > 0 . La tarea es contar el número de pasos que se pueden realizar.
Ejemplos:
Entrada: N = 85
Salida: 2
Primer paso, 85 – (9 * 9) = 4
Segundo paso 4 – (2 * 2) = 0Entrada: N = 114
Salida: 4
Primer paso, 114 – (10 * 10) = 14
Segundo paso 14 – (3 * 3) = 5
Tercer paso 5 – (2 * 2) = 1
Cuarto paso 1 – (1 * 1 ) = 0
Enfoque : reste iterativamente el cuadrado perfecto más grande (≤ N) de N mientras N > 0 y cuente el número de pasos.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to return the count of steps int countSteps(int n) { // Variable to store the count of steps int steps = 0; // Iterate while N > 0 while (n) { // Get the largest perfect square // and subtract it from N int largest = sqrt(n); n -= (largest * largest); // Increment steps steps++; } // Return the required count return steps; } // Driver code int main() { int n = 85; cout << countSteps(n); return 0; }
Java
// Java implementation of the approach import java.lang.Math; public class GfG{ // Function to return the count of steps static int countSteps(int n) { // Variable to store the count of steps int steps = 0; // Iterate while N > 0 while (n > 0) { // Get the largest perfect square // and subtract it from N int largest = (int)Math.sqrt(n); n -= (largest * largest); // Increment steps steps++; } // Return the required count return steps; } public static void main(String []args){ int n = 85; System.out.println(countSteps(n)); } } // This code is contributed by Rituraj Jain
Python3
# Python3 implementation of the approach from math import sqrt # Function to return the count of steps def countSteps(n) : # Variable to store the count of steps steps = 0; # Iterate while N > 0 while (n) : # Get the largest perfect square # and subtract it from N largest = int(sqrt(n)); n -= (largest * largest); # Increment steps steps += 1; # Return the required count return steps; # Driver code if __name__ == "__main__" : n = 85; print(countSteps(n)); # This code is contributed by Ryuga
C#
// C# implementation of the approach using System; class GfG { // Function to return the count of steps static int countSteps(int n) { // Variable to store the count of steps int steps = 0; // Iterate while N > 0 while (n > 0) { // Get the largest perfect square // and subtract it from N int largest = (int)Math.Sqrt(n); n -= (largest * largest); // Increment steps steps++; } // Return the required count return steps; } // Driver code public static void Main() { int n = 85; Console.WriteLine(countSteps(n)); } } // This code is contributed by Code_Mech.
PHP
<?php // PHP implementation of the approach // Function to return the count of steps function countSteps($n) { // Variable to store the count of steps $steps = 0; // Iterate while N > 0 while ($n) { // Get the largest perfect square // and subtract it from N $largest = (int)sqrt($n); $n -= ($largest * $largest); // Increment steps $steps++; } // Return the required count return $steps; } // Driver code $n = 85; echo countSteps($n); // This code is contributed // by Akanksha Rai ?>
Javascript
<script> // JavaScript implementation of the approach // Function to return the count of steps function countSteps(n) { // Variable to store the count of steps let steps = 0; // Iterate while N > 0 while (n) { // Get the largest perfect square // and subtract it from N let largest = Math.floor(Math.sqrt(n)); n -= (largest * largest); // Increment steps steps++; } // Return the required count return steps; } // Driver code let n = 85; document.write(countSteps(n)); // This code is contributed by Manoj. </script>
Producción:
2
Complejidad de tiempo: O(1)
Espacio Auxiliar: O(1), ya que no se ha ocupado ningún espacio extra.