# Numbers within a range that can be expressed as power of two numbers

Given two integers L and R. Find the number of perfect powers in the given range [L, R]. A number x is said to be perfect power if there exists some integers a > 0, p > 1 such that x = a^{p}.

Examples :

Input : 1 4 Output : 2Explanation :Suitable numbers are 1 and 4 where 1 can be expressed as 1 = 1^{2}and 4 can be expressed as 4 = 2^{2}Input : 12 29 Output : 3Explanation :Suitable numbers are 16, 25 and 27.

**Prerequisites : **Check if a number can be expressed as x^y, Binary Search and Perfect power (1, 4, 8, 9, 16, 25, 27, …)

**Approach : ** Let’s fix some power p. It’s obvious that there are no more than 10^{18/p} numbers x such that x^{p} doesn’t exceed 10^{18} for a particular p. At the same time, only for p = 2 this amount is relatively huge, for all other p ≥ 3 the total amount of such numbers will be of the order of 10^{6}. There are 10^{9} squares in the range [1, 10^{18}], so can’t store them to answer our query.

Either, generate all of powers for p ≥ 2 and dispose of all perfect squares among them or generate only odd powers of numbers like 3, 5, 7, etc. Then answer to query (L, R) is equal to the amount of generated numbers between L and R plus some perfect squares in range.

- The number of perfect squares in the range is the difference of floor value of square root of R and floor value of square root of (L – 1), i.e. (
**floor(sqrt(R)) – floor(sqrt(L – 1)**).*Note that due to precision issues the standard sqrt might produce incorrect values, so either use binary search or***sqrtl**inbuilt function defined in cmath (Check here for more description of sqrtl). -
To generate those odd powers of numbers. First of all, do precomputation of finding such numbers that can be expressed as power of some number upto 10
^{18}so that we can answer many queries and no need to process them again and again for each query. Start by iterating a loop from 2 to 10^{6}(since we are calculating for powers p ≥ 3 and 10^{6}is the maximum number whose power raised to 3 cannot exceed 10^{18}), for each value we insert its square into a set and check further if that value is already a perfect square (already present in the set), we do not find any other powers of that number (since any power of a perfect square is also a perfect square). Otherwise, run an inside loop to find odd powers of the number until it exceeds 10^{18}and insert into another set say ‘s’. By this approach, we haven’t pushed any perfect square in the set ‘s’.

Hence the final answer would be sum of number of perfect squares in the range and difference of upper value of R and lower value of L (using binary search).

**Below is the implementation of above approach in C++.**

`// CPP Program to count the numbers ` `// within a range such that number ` `// can be expressed as power of some ` `// other number ` `#include <bits/stdc++.h> ` ` ` `using` `namespace` `std; ` ` ` `#define N 1000005 ` `#define MAX 1e18 ` ` ` `// Vector to store powers greater than 3 ` `vector<` `long` `int` `> powers; ` ` ` `// set to store perfect squares ` `set<` `long` `int` `> squares; ` ` ` `// set to store powers other ` `// than perfect squares ` `set<` `long` `int` `> s; ` ` ` `void` `powersPrecomputation() ` `{ ` ` ` `for` `(` `long` `int` `i = 2; i < N; i++) ` ` ` `{ ` ` ` `// pushing squares ` ` ` `squares.insert(i * i); ` ` ` ` ` `// if the values is already ` ` ` `// a perfect square means ` ` ` `// present in the set ` ` ` `if` `(squares.find(i) != squares.end()) ` ` ` `continue` `; ` ` ` ` ` `long` `int` `temp = i; ` ` ` ` ` `// run loop until some ` ` ` `// power of current number ` ` ` `// doesn't exceed MAX ` ` ` `while` `(i * i <= MAX / temp) ` ` ` `{ ` ` ` `temp *= (i * i); ` ` ` ` ` `/* pushing only odd powers ` ` ` `as even power of a number ` ` ` `can always be expressed as ` ` ` `a perfect square which is ` ` ` `already present in set squares */` ` ` `s.insert(temp); ` ` ` `} ` ` ` `} ` ` ` ` ` `// Inserting those sorted ` ` ` `// values of set into a vector ` ` ` `for` `(` `auto` `x : s) ` ` ` `powers.push_back(x); ` `} ` ` ` `long` `int` `calculateAnswer(` `long` `int` `L, ` `long` `int` `R) ` `{ ` ` ` `// calculate perfect squares in ` ` ` `// range using sqrtl function ` ` ` `long` `int` `perfectSquares = ` `floor` `(sqrtl(R)) - ` ` ` `floor` `(sqrtl(L - 1)); ` ` ` ` ` `// calculate upper value of R ` ` ` `// in vector using binary search ` ` ` `long` `int` `high = (upper_bound(powers.begin(), ` ` ` `powers.end(), R) - powers.begin()); ` ` ` ` ` `// calculate lower value of L ` ` ` `// in vector using binary search ` ` ` `long` `int` `low = (lower_bound(powers.begin(), ` ` ` `powers.end(), L) - powers.begin()); ` ` ` ` ` `// add into final answer ` ` ` `perfectSquares += (high - low); ` ` ` ` ` `return` `perfectSquares; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `// precompute the powers ` ` ` `powersPrecomputation(); ` ` ` ` ` `// left value of range ` ` ` `long` `int` `L = 12; ` ` ` ` ` `// right value of range ` ` ` `long` `int` `R = 29; ` ` ` ` ` `cout << ` `"Number of powers between "` `<< L ` ` ` `<< ` `" and "` `<< R << ` `" = "` `<< ` ` ` `calculateAnswer(L, R) << endl; ` ` ` ` ` `L = 1; ` ` ` `R = 100000000000; ` ` ` ` ` `cout << ` `"Number of powers between "` `<< L ` ` ` `<< ` `" and "` `<< R << ` `" = "` `<< ` ` ` `calculateAnswer(L, R) << endl; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

Number of powers between 12 and 29 = 3 Number of powers between 1 and 100000000000 = 320990

## Recommended Posts:

- N expressed as sum of 4 prime numbers
- Check if a number can be expressed as power | Set 2 (Using Log)
- Check if a number can be expressed as x^y (x raised to power y)
- Modulo power for large numbers represented as strings
- Sum of all even numbers in range L and R
- Count Odd and Even numbers in a range from L to R
- Sum of all the prime numbers in a given range
- Count of numbers from range [L, R] whose sum of digits is Y
- No of pairs (a[j] >= a[i]) with k numbers in range (a[i], a[j]) that are divisible by x
- Sum of all odd length palindromic numbers within the range [L, R]
- Sum of range in a series of first odd then even natural numbers
- XOR of numbers that appeared even number of times in given Range
- K-Primes (Numbers with k prime factors) in a range
- Count all the numbers in a range with smallest factor as K
- Count numbers in range such that digits in it and it's product with q are unequal

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.