Number of perfect squares between two given numbers Last Updated : 22 Mar, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report Try it on GfG Practice Given two numbers a and b where 1<=a<=b, find the number of perfect squares between a and b (a and b inclusive).ExampleInput: a = 3, b = 8Output: 1Explanation: The only perfect square in given range is 4.Input: a = 9, b = 25Output: 3Explanation: The three perfect squares in given range are 9, 16 and 25Table of Content[Naive Approach] - Counting perfect Squared between A and B [Expected Appproach] - Using Square Root of a and b [Naive Approach] - One by One Counting The idea for this approach is to use a loop to iterate from a to b and count the numbers that are perfect squares. C++ #include <bits/stdc++.h> using namespace std; int countSquares(int a, int b) { int cnt = 0; // Traverse through all numbers for (int i = a; i <= b; i++) // Check if current number 'i' // is perfect square for (int j = 1; j * j <= i; j++) if (j * j == i) cnt++; return cnt; } int main() { int a = 9, b = 25; cout <<countSquares(a, b); return 0; } Java class CountSquares { static int countSquares(int a, int b) { int cnt = 0; // Traverse through all numbers for (int i = a; i <= b; i++) // Check if current number 'i' is perfect // square for (int j = 1; j * j <= i; j++) if (j * j == i) cnt++; return cnt; } } public class PerfectSquares { public static void main(String[] args) { int a = 9, b = 25; CountSquares obj = new CountSquares(); System.out.print(obj.countSquares(a, b)); } } Python def CountSquares(a, b): cnt = 0 # Traverse through all numbers for i in range (a, b + 1): j = 1; while j * j <= i: if j * j == i: cnt = cnt + 1 j = j + 1 i = i + 1 return cnt a = 9 b = 25 print (CountSquares(a, b)) C# using System; class GFG { // Function to count squares static int countSquares(int a, int b) { int cnt = 0; // Traverse through all numbers for (int i = a; i <= b; i++) // Check if current number // 'i' is perfect square for (int j = 1; j * j <= i; j++) if (j * j == i) cnt++; return cnt; } public static void Main() { int a = 9, b = 25; Console.Write(countSquares(a, b)); } } JavaScript function countSquares(a, b) { let cnt = 0; // Traverse through all numbers for (let i = a; i <= b; i++) // Check if current number // 'i' is perfect square for (let j = 1; j * j <= i; j++) if (j * j == i) cnt++; return cnt; } let a = 9; let b = 25; console.log(countSquares(a, b)); OutputCount of squares is 3Time Complexity: O((b-a) * sqrt(b)).Auxiliary Space: O(1)[Expected Approach] - Using Square Roots of a and b The idea for this approach is to simply take square root of ‘a’ and ‘b’ and count the numbers between them by using this formula floor(sqrt(b)) - ceil(sqrt(a)) + 1We take floor of sqrt(b) because we need to consider numbers before b.We take ceil of sqrt(a) because we need to consider numbers after a.Example: b = 24, a = 8. floor(sqrt(b)) = 4, ceil(sqrt(a)) = 3. And number of squares is 4 - 3 + 1 = 2.(9 and 16) . C++ #include <bits/stdc++.h> using namespace std; int countSquares(int a, int b) { return (floor(sqrt(b)) - ceil(sqrt(a)) + 1); } int main() { int a = 9, b = 25; cout <<countSquares(a, b); return 0; } Java class CountSquares { double countSquares(int a, int b) { return (Math.floor(Math.sqrt(b)) - Math.ceil(Math.sqrt(a)) + 1); } } public class PerfectSquares { public static void main(String[] args) { int a = 9, b = 25; CountSquares obj = new CountSquares(); System.out.print((int)obj.countSquares(a, b)); } } Python import math def CountSquares(a, b): return (math.floor(math.sqrt(b)) - math.ceil(math.sqrt(a)) + 1) a = 9 b = 25 print (int(CountSquares(a, b))) C# using System; class GFG { static double countSquares(int a, int b) { return (Math.Floor(Math.Sqrt(b)) - Math.Ceiling(Math.Sqrt(a)) + 1); } public static void Main() { int a = 9, b = 25; Console.Write((int)countSquares(a, b)); } } JavaScript function countSquares(a, b) { return (Math.floor(Math.sqrt(b)) - Math.ceil(Math.sqrt(a)) + 1); } let a = 9; let b = 25; console.log(countSquares(a, b)); OutputCount of squares is 3Time Complexity: O(log b) as any typical implementation of square root for a number n takes time equal to O(Log n)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Number of times the largest perfect square number can be subtracted from N K kartik Improve Article Tags : Mathematical DSA Wipro maths-perfect-square Practice Tags : WiproMathematical Similar Reads Minimum perfect squares to add that sum to given number. Given a positive integer n, the task is to find the minimum number of squares that sum to n. Note: A number can always be represented as a sum of squares of other numbers. Because 1 is a square number and we can always break any number as (1*1 + 1*1 + 1*1 + ... ).Examples : Input: n = 100Output: 1Ex 15+ min read List of Perfect Square Numbers from 1 to 100 A perfect square is an integer that can be expressed as the square of another integer. In mathematical terms, a number n is a perfect square if there exists an integer m such that:n = m2For example, 1,4,9,16, and 25 are perfect squares because they can be expressed as 12, 22, 32, 42, 52 respectively 3 min read 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 Firs 7 min read Minimum number of squares whose sum equals to given number N | set 2 A number can always be represented as a sum of squares of other numbers. Note that 1 is a square, and we can always break a number as (1*1 + 1*1 + 1*1 + â¦). Given a number N, the task is to represent N as the sum of minimum square numbers. Examples: Input : 10 Output : 1 + 9 These are all possible w 8 min read Find minimum number to be divided to make a number a perfect square Given a positive integer n. Find the minimum number which divide n to make it a perfect square.Examples: Input : n = 50 Output : 2 By Dividing n by 2, we get which is a perfect square. Input : n = 6 Output : 6 By Dividing n by 6, we get which is a perfect square. Input : n = 36 Output : 1 A number i 6 min read Minimum number of squares whose sum equals to a given number N | Set-3 A number can always be represented as a sum of squares of other numbers. Note that 1 is a square, and we can always break a number as (1*1 + 1*1 + 1*1 + â¦). Given a number n, find the minimum number of squares that sum to N.Examples:Input: N = 13 Output: 2 Explanation: 13 can be expressed as, 13 = 3 7 min read Like