
- 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
C++ Program to count square root cube root and both in given range
Suppose we have a number n. We have to count the number of integers x, in range 1 to n such that, that x is a square of a positive integer number or a cube of a positive integer number (or both a square and a cube simultaneously).
Problem Category
Various problems in programming can be solved through different techniques. To solve a problem, we have to devise an algorithm first, and to do that we have to study the particular problem in detail. A recursive approach can be used if there is a recurring appearance of the same problem over and over again; alternatively, we can use iterative structures also. Control statements such as if-else and switch cases can be used to control the flow of logic in the program. Efficient usage of variables and data structures provides an easier solution and a lightweight, low-memory-requiring program. We have to look at the existing programming techniques, such as Divide-and-conquer, Greedy Programming, Dynamic Programming, and find out if they can be used. This problem can be solved by some basic logic or a brute-force approach. Follow the following contents to understand the approach better.
So, if the input of our problem is like n = 25, then the output will be 6
Steps
To solve this, we will follow these steps −
a := square root of n b := cube root of n c := cube root of a return a + b - c
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; int solve(int n){ int a = sqrt(n), b = cbrt(n), c = cbrt(a); return a + b - c; } int main(){ int n = 25; cout << solve(n) << endl; }
Input
25
Output
6
- Related Articles
- What is square root and cube root?
- Java program to find the cube root of a given number
- Python Program to calculate the cube root of the given number
- C++ Program to calculate the cube root of the given number
- Haskell Program to calculate the cube root of the given number
- C++ Range Sum Queries and Update with Square Root
- 8086 program to find the square root of a perfect square root number
- C++ Program to find Numbers in a Range with Given Digital Root
- Square and Square root in Arduino
- Java program to find the square root of a given number
- Haskell Program to calculate the square root of the given number
- Find Cube Root Of 9261
- 8086 program to find Square Root of a number
- 8085 program to find square root of a number
- Making use of cube root table find the cube root of following :a) 11005 b) 1346
