Number of times the largest perfect square number can be subtracted from N

Given a number N. At every step, subtract the largest perfect square( ≤ N) from N. Repeat this step while N > 0. The task is to count the number of steps that can be performed.

Examples:

Input: N = 85
Output: 2
First step, 85 – (9 * 9) = 4
Second step 4 – (2 * 2) = 0

Input: N = 114
Output: 4
First step, 114 – (10 * 10) = 14
Second step 14 – (3 * 3) = 5
Third step 5 – (2 * 2) = 1
Fourth step 1 – (1 * 1) = 0



Approach: Iteratively subtract the largest perfect square (≤ N) from N while N > 0 and count the number of steps.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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.

chevron_right


PHP

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
?>

Output:

2


My Personal Notes arrow_drop_up

Striver(underscore)79 at Codechef and codeforces D

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.