Skip to content
Related Articles
Open in App
Not now

Related Articles

Count integers whose square lie in given range and digits are perfect square

Improve Article
Save Article
  • Last Updated : 26 Dec, 2022
Improve Article
Save Article

Given two integers L and R. Then the task is to output the count of integers X, such that L ≤ X2 ≤ R and X2 only consist of digits, which are perfect squares. 

Examples:

Input: L = 167, R = 456
Output:  2
Explanation: Two numbers are 20 and 21, Their squares are 400 and 441 respectively. It can be verified that squares of both 20 and 21 are in the range of L and R and contains only digits which are square numbers. ie. 0, 4, 1.

Input: L = 4567, R = 78990
Output: 11

Approach: Implement the idea below to solve the problem

Find the nearest numbers from L and R both using floor(), ceil(), and in-built sqrt() function. Traverse all the integer values between those obtained two numbers and check if a number exists such that they only contain digits 0, 1, 4, 9, or not. Count those numbers in a variable.

Steps were taken to solve the problem:

  • Initialize a variable ans = 0, to store the count of perfect square occurrences.
  • Take an integer variable X, Which will be the perfect square nearest to R and can be obtained as X = (Math.floor(Math.sqrt(R))).
  • Take an integer variable Y, Which will be the nearest perfect square number to L and can be obtained as (Math.floor(Math.sqrt(L))).
  • Traverse all the integer values between X and Y using Loop and check:
    • How many numbers are there such that, They only contain 0, 1, 4, and 9 as their digits?
    • If all digits are only 0, 1, 4, or 9, then increment the ans by 1.
  • Return the ans.

Below is the implementation of the above approach.

C++




// C++ implementation
#include <bits/stdc++.h>
using namespace std;
 
// Function to count the number of valid X
long countSquare(long X, long Y)
{
   
  // Finding nearest integers such
  // that  their squares are in range
  // of L and R(both inclusive)
  Y = (int)sqrt(Y);
  X = ceil(sqrt(X));
   
  // Variable to store answer or
  // count of required numbers
  long ans = 0;
 
  // Traversing from nearest X to Y
  while (X <= Y) {
 
    long k = X * X;
 
    // Initialise flag to check
    // which digit is present
    bool flag = true;
 
    // Checking that square of
    // current number consists only
    // digits 0, 1, 4, 9 or not
    while (k > 0) {
 
      long rem = k % 10;
 
      // If any other digit is
      // present flag = false
      if (!(rem == 0 || rem == 1 || rem == 4
            || rem == 9)) {
        flag = false;
        break;
      }
 
      k /= 10;
    }
    if (flag) {
 
      // Incrementing count
      // variable
      ans++;
    }
 
    // Incrementing value of X
    X++;
  }
 
  // Return maximum count of
  // numbers obtained
  return ans;
}
 
int main()
{
 
  // Driver code
 
  // Input values X and Y
  long X = 167;
  long Y = 456;
 
  // Function call
  cout << (countSquare(X, Y));
 
  return 0;
}
 
// This code is contributed by ksam24000

Java




// Java code to implement the approach
 
import java.io.*;
import java.lang.*;
import java.util.*;
 
// Name of the class has to be "GFG"
// only if the class is public.
class GFG {
 
    // Driver code
    public static void main(String[] args)
    {
        // Input values X and Y
        long X = 167;
        long Y = 456;
 
        // Function call
        System.out.println(countSquare(X, Y));
    }
 
    // Function to count the number of valid X
    static long countSquare(long X, long Y)
    {
        // Finding nearest integers such
        // that  their squares are in range
        // of L and R(both inclusive)
        Y = (long)(Math.floor(Math.sqrt(Y)));
        X = (long)(Math.ceil(Math.sqrt(X)));
 
        // Variable to store answer or
        // count of required numbers
        long ans = 0;
 
        // Traversing from nearest X to Y
        while (X <= Y) {
 
            long k = X * X;
 
            // Initialise flag to check
            // which digit is present
            boolean flag = true;
 
            // Checking that square of
            // current number consists only
            // digits 0, 1, 4, 9 or not
            while (k > 0) {
 
                long rem = k % 10;
 
                // If any other digit is
                // present flag = false
                if (!(rem == 0 || rem == 1 || rem == 4
                      || rem == 9)) {
                    flag = false;
                    break;
                }
 
                k /= 10;
            }
            if (flag) {
 
                // Incrementing count
                // variable
                ans++;
            }
 
            // Incrementing value of X
            X++;
        }
 
        // Return maximum count of
        // numbers obtained
        return ans;
    }
}

Python3




# Python implementation of the approach
import math
 
# Function to count the number of valid X
def countSquare(X, Y):
 
    # Finding nearest integers such
    # that  their squares are in range
    # of L and R(both inclusive)
    Y = int(math.sqrt(Y))
    X = math.ceil(math.sqrt(X))
 
    # Variable to store answer or
    # count of required numbers
    ans = 0
 
    # Traversing from nearest X to Y
    while (X <= Y):
 
        k = X * X
 
        # Initialise flag to check
        # which digit is present
        flag = True
 
        # Checking that square of
        # current number consists only
        # digits 0, 1, 4, 9 or not
        while (k > 0):
 
            rem = k % 10
 
            # If any other digit is
            # present flag = false
            if (not(rem == 0 or rem == 1 or rem == 4 or rem == 9)):
                flag = False
                break
 
            k //= 10
 
        if (flag):
 
            # Incrementing count
            # variable
            ans += 1
 
        # Incrementing value of X
        X += 1
 
    # Return maximum count of
    # numbers obtained
    return ans
 
# Driver code
 
# Input values X and Y
X = 167
Y = 456
 
# Function call
print(countSquare(X, Y))
 
# This code is contributed by Tapesh(tapeshdua420)

C#




// C# program for above approach
using System;
 
class GFG
{
 
  // Function to count the number of valid X
  static long countSquare(long X, long Y)
  {
    // Finding nearest integers such
    // that  their squares are in range
    // of L and R(both inclusive)
    Y = (long)(Math.Floor(Math.Sqrt(Y)));
    X = (long)(Math.Ceiling(Math.Sqrt(X)));
 
    // Variable to store answer or
    // count of required numbers
    long ans = 0;
 
    // Traversing from nearest X to Y
    while (X <= Y) {
 
      long k = X * X;
 
      // Initialise flag to check
      // which digit is present
      bool flag = true;
 
      // Checking that square of
      // current number consists only
      // digits 0, 1, 4, 9 or not
      while (k > 0) {
 
        long rem = k % 10;
 
        // If any other digit is
        // present flag = false
        if (!(rem == 0 || rem == 1 || rem == 4
              || rem == 9)) {
          flag = false;
          break;
        }
 
        k /= 10;
      }
      if (flag) {
 
        // Incrementing count
        // variable
        ans++;
      }
 
      // Incrementing value of X
      X++;
    }
 
    // Return maximum count of
    // numbers obtained
    return ans;
  }
 
  // Driver Code
  public static void Main()
  {
    // Input values X and Y
    long X = 167;
    long Y = 456;
 
    // Function call
    Console.Write(countSquare(X, Y));
  }
}
 
// This code is contributed by sanjoy_62.

Javascript




// Javascript implementation of the approach
function countSquare(X, Y) {
    // Finding nearest integers such
    // that  their squares are in range
    // of L and R(both inclusive)
    Y = Math.floor(Math.sqrt(Y));
    X = Math.ceil(Math.sqrt(X));
 
    // Variable to store answer or
    // count of required numbers
    var ans = 0;
 
    // Traversing from nearest X to Y
    while (X <= Y) {
        var k = X * X;
 
        // Initialise flag to check
        // which digit is present
        var flag = true;
 
        // Checking that square of
        // current number consists only
        // digits 0, 1, 4, 9 or not
        while (k > 0) {
            var rem = k % 10;
 
            // If any other digit is
            // present flag = false
            if (!(rem == 0 || rem == 1 || rem == 4 || rem == 9)) {
                flag = false;
                break;
            }
 
            k = Math.floor(k / 10);
        }
 
        if (flag) {
            // Incrementing count
            // variable
            ans += 1;
        }
 
        // Incrementing value of X
        X += 1;
    }
 
    // Return maximum count of
    // numbers obtained
    return ans;
}
 
// Driver code
 
// Input values X and Y
var X = 167;
var Y = 456;
 
// Function call
console.log(countSquare(X, Y));
 
// This code is contributed by Tapesh(tapeshdua420)

Output

2

Time Complexity: O(sqrt(R))
Auxiliary Space: O(1)

Related Articles:


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!