Largest number that is not a perfect square


In this article, we will discuss two different approaches to find out the largest number smaller than a given number which is not a perfect square. In the first approach, we will run a loop to check for every number until we find the desired number while in the second approach, we will use the concept of square root to generate the perfect square number just smaller than the given number and based on this, we will find out the largest number smaller than "nums" which is not a perfect square.

Let us first understand the problem statement.

Problem Statement

We are given a number "nums", our task is to find out the largest number smaller than "nums" which is not a perfect square.

Perfect square − A number is called a perfect square if it can be expressed as a product of two integers that are equal. Some examples of a perfect square are − 49, 64, 81 etc.

Let us now understand the problem statement using an example −

Input: nums = 15
Output: The largest number smaller than 15, which is not a perfect square is 14. 
Input: nums = 17
Output: The largest number smaller than 17, which is not a perfect square is 15. 

Approach 1: Simple approach using a loop only

To find the largest number that is not a perfect square, we take an input from the user and check each number less than the input until we find a number that is not a perfect square.

To check if a number is a perfect square, we calculate its square root using the sqrt() function and check if the square of the result is equal to the original number.

We use a while loop to repeatedly check if a number is a perfect square by decreasing it by 1 each time until we find a number that is not a perfect square.

When the loop finds the first number that is not a perfect square, it stores that number as the largest number that is not a perfect square.

Finally, we print out the result.

Example

The code for this approach is given below −

#include <bits/stdc++.h>
using namespace std;
bool isPerfectSquare(int nums) {
   int sqrt_n = sqrt( nums );
   return (sqrt_n * sqrt_n == nums);
}

// main code
int main() {
   int nums =15;
   int temp = nums ;

   // keep decreasing the temp until we get a non – perfect square number
   while ( isPerfectSquare( temp ) ) {
      temp-- ;
   }
   cout <<   " The largest number that is not a perfect square is: " <<  temp  << endl;
   return 0;
}

Output

The largest number that is not a perfect square is: 15
  • Time Complexity − The time complexity of this algorithm is O(sqrt(n)), where n is the input. Here, we check each number less than n until we find a number that is not a perfect square. As the number of perfect squares less than n is proportional to the square root of n, the algorithm takes O(sqrt(n)) time.

  • Space Complexity − The auxiliary space complexity of this algorithm is O(1), as we are just using constant extra space.

Approach 2: The formula - based approach

Let us first understand our intuition behind this approach.

Can we say if the number just smaller than "nums" is not a perfect square?

i.e., if (nums-1) is not a perfect square, it can be concluded that nums − 1 is the largest number smaller than nums which is not a perfect square.

But if "nums" -1 is a perfect square, then we have to decrement "nums" by 2 in order to obtain a non - perfect square number so the largest number smaller than "nums" which is not a perfect square is "nums" − 2.

So we can conclude that we just need to generate the perfect square number just smaller than "nums" and then find out the result accordingly.

Algorithm

Let us now implement this approach into an algorithm.

  • Step 1 − Take "nums" as input.

  • Step 2 − Calculate the perfect square of the number just smaller than "nums".

  • Step 3 − If the just smaller perfect square of the number obtained is equal to number -1, we have to substract 2 from the answer (as "nums" − 1 is a perfect square number).

  • Step 4 − Else, we have to substract 1 from the number as we know, the number obtained is neither a perfect square nor greater than or equal to the orginal number.

Example

#include <iostream>
#include <cmath>
using namespace std;

int justsmallerperfectsq(int nums){
   int ps = floor(sqrt(nums));  
      
   // If nums is already a perfect square decrease ps by 1.
   if (ps * ps == nums )
      ps -= 1; 
   return ps * ps;
}

// bolean function to check if the number is a perfect square or not
int main() {
   int nums = 17;
   int ans =0;
   int temp =  justsmallerperfectsq(nums);
   if(temp == nums-1){
      ans =  nums - 2;
   } else { 
      ans = nums-1;
   }
   cout << "The largest number smaller than " << nums << " that is not a perfect square is: " << ans << endl;
   return 0;
}

Output

The largest number smaller than 17 that is not a perfect square is: 15

Conclusion

We can conclude from the above approach that the number just smaller than a given number "nums" can also be the largest number smaller than "nums" which is not a perfect square if "nums" -1 is not a perfect square.

Updated on: 05-Oct-2023

73 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements