
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Count square and non-square numbers before n in C++
We are given a number N. The goal is to find ordered pairs of positive numbers such that the sum of their cubes is N.
Naive Approach
Traverse all numbers from 1 to N and check if it is a perfect square. If floor(sqrt(i))==ceil(sqrt(i)).
Then the number is a perfect square.
Efficient Approach
Perfect squares below N can be found using formula: floor(sqrt(N)).
Let’s understand with examples.
Input
N=20
Output
Count of square numbers: 4 Count of non-square numbers: 16
Explanation
Square numbers are 1, 4, 9 and 16. Rest all are non-squares and less than 20.
Input
N=40
Output
Count of square numbers: 6 Count of non-square numbers: 34
Explanation
Square numbers are 1, 4, 9, 16, 25, 36. Rest all are non-squares and less than 40.
Naive Approach
Approach used in the below program is as follows
We take integer N.
Function squareNums(int n) takes n and returns the count of numbers below n that are perfect squares or non-squares.
Take the initial variable count as 0.
Traverse using for loop from i=1 to i<=n
If floor(sqrt(i))==ceil(sqrt(i)), then the number is a perfect square so increment count.
At the end of all loops count will have a total number that are perfect squares.
N-squares will be numbers that are non squares
Example
#include <bits/stdc++.h> #include <math.h> using namespace std; int squareNums(int n){ int count = 0; for (int i = 1; i <= n; i++){ if(floor(sqrt(i))==ceil(sqrt(i))) { count++; } } return count; } int main(){ int N = 40; int squares=squareNums(N); cout <<endl<<"Count of squares numbers: "<<squares; cout <<endl<<"Count of non-squares numbers: "<<N-squares; return 0; }
Output
If we run the above code it will generate the following output −
Count of squares numbers: 6 Count of non-squares numbers: 34
Efficient Approach
Approach used in the below program is as follows
We take integer N.
Take variable squares = floor(sqrt(N)).
Variable squares will have a number of perfect squares below N.
N-squares will be the number of non-squares below N.
Example
#include <bits/stdc++.h> #include <math.h> using namespace std; int main(){ int N = 40; int squares=floor(sqrt(N)); cout <<endl<<"Count of squares numbers: "<<squares; cout <<endl<<"Count of non-squares numbers: "<<N-squares; return 0; }
Output
If we run the above code it will generate the following output −
Count of squares numbers: 6 Count of non-squares numbers: 34
- Related Articles
- Print the Non Square Numbers in C
- Count numbers upto N which are both perfect square and perfect cube in C++
- Sum of square of first n odd numbers
- Sum of square-sums of first n natural numbers
- Replace Odd Numbers with Square root & Even Numbers with Square in Java
- Square and Square root in Arduino
- Sum of Square Numbers in C++
- Print n numbers such that their sum is a perfect square
- In the following APs, find the missing terms in the boxes:(i) $2, \square, 26$(ii) $\square, 13, \square, 3$(iii) $5, \square, \square, 9\frac{1}{2}$(iv) $-4, \square, \square, \square, \square, 6$(v) $\square, 38, \square, \square, \square, -22$
- Write true (T) or false (F) for the following statements.(i) The number of digits in a square number is even.(ii) The square of a prime number is prime.(iii) The sum of two square numbers is a square number.(iv) The difference of two square numbers is a square number.(v) The product of two square numbers is a square number.(vi) No square number is negative.(vii) There is not square number between 50 and 60.(viii) There are fourteen square numbers upto 200.
- Count Square Submatrices with All Ones in C++
- Difference between sum of the squares of and square of sum first n natural numbers.
- Find the perfect square numbers between 30 and 40.
- PHP program to calculate the sum of square of first n natural numbers
- In the following APs, find the missing terms in the boxes:$-4, \square, \square, \square, \square, 6$
