Count square and non-square numbers before n - GeeksforGeeks

23 Feb, 2021
Given a number n, we need to count square numbers smaller than or equal to n.Examples :  
Input : n = 5
Output : Square Number : 2
         Non-square numbers : 3
Explanation : Square numbers are 1 and 4.
Non square numbers are 2, 3 and 5.

Input : n = 10
Output : Square Number : 3
         Non-square numbers : 7
Explanation : Square numbers are 1, 4 and 9.
Non square numbers are 2, 3, 5, 6, 7, 8 and 10.
 
A simple solution is to traverse through all numbers from 1 to n and for every number check if n is perfect square or not. An efficient solution is based on below formula.Count of square numbers that are greater than 0 and smaller than or equal to n are floor(sqrt(n)) or ⌊√(n)⌋Count of non-square numbers = n – ⌊√(n)⌋ 
C++
Java
Python3
C#
PHP
Javascript
// CPP program to count squares and// non-squares before a number.#include <bits/stdc++.h>using namespace std; void countSquaresNonSquares(int n){    int sc = floor(sqrt(n));    cout << "Count of squares "         << sc << endl;    cout << "Count of non-squares "         << n - sc << endl;} // Driver Codeint main(){    int n = 10;    countSquaresNonSquares(n);    return 0;}
// Java program to count squares and// non-squares before a number.import java.io.*;import java.math.*; class GFG{    static void countSquaresNonSquares(int n)    {        int sc = (int)(Math.floor(Math.sqrt(n)));        System.out.println("Count of" +                     " squares " + sc);        System.out.println("Count of" +                      " non-squares " +                            (n - sc) );    }     // Driver code    public static void main(String args[])    {        int n = 10;        countSquaresNonSquares(n);    }} // This code is contributed// by Nikita Tiwari.
# Python 3 program to count# squares and non-squares# before a number.import math def countSquaresNonSquares(n) :    sc = (math.floor(math.sqrt(n)))    print("Count of squares ", sc)    print("Count of non-squares ", (n - sc) )          # Driver coden = 10countSquaresNonSquares(n) # This code is contributed# by Nikita Tiwari.
// C# program to count squares and// non-squares before a number.using System; class GFG{static void countSquaresNonSquares(int n){    int sc = (int)Math.Sqrt(n);    Console.WriteLine( "Count of " +                        "squares " +                               sc) ;    Console.WriteLine("Count of " +                   "non-squares " +                         (n - sc));}     // Driver Code    static public void Main ()    {    int n = 10;    countSquaresNonSquares(n);    }} // This code is contributed by anuj_67.
<?php// PHP program to count// squares and non-squares// before a number. // function to count squares// and non-squares before a// numberfunction countSquaresNonSquares($n){    $sc = floor(sqrt($n));    echo("Count of squares " .                  $sc . "\n");    echo("Count of non-squares " .                      ($n - $sc));} // Driver code$n = 10;countSquaresNonSquares($n); // This code is contributed by Ajit.?>
<script> // Javascript program to count squares and// non-squares before a number. function countSquaresNonSquares(n){    let sc = Math.floor(Math.sqrt(n));    document.write("Count of squares "        + sc + "<br>");    document.write("Count of non-squares "        + (n - sc) + "<br>");} // Driver Code      let n = 10;    countSquaresNonSquares(n); //This code is contributed by Mayank Tyagi </script>
Output :  
Count of squares 3
Count of non-squares 7
 
jit_t
vt_m
mayanktyagi1709
maths-perfect-square
number-theory
Mathematical
number-theory
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Merge two sorted arrays
Modulo Operator (%) in C/C++ with Examples
Prime Numbers
Program to find GCD or HCF of two numbers
Sieve of Eratosthenes
Print all possible combinations of r elements in a given array of size n
Operators in C / C++
The Knight's tour problem | Backtracking-1
Program for factorial of a number
Program for Decimal to Binary Conversion