Given a positive integer n, the task is to find the count of perfect powers from 1 to n.
Note: A perfect power is a number that can be expressed as a power of another positive integer i.e. it can be expressed as xy where x >= 1 and y > 1
Examples :
Input: n = 10
Output: 4
Explanation: 1, 4, 8, and 9 are the numbers that are of the form x ^ y where x > 0 and y > 1.Input: n = 50
Output: 10
Explanation: 1, 4, 8, 9, 16, 25, 27, 32, 36, 49 are the numbers that are of the form x ^ y where x > 0 and y > 1.
[Naive Approach] - O(n log n) Time and O(n) Space
The idea is to iterate through all integers from 1 to square root n and find all possible perfect powers. To find the perfect powers, run a loop starting from i * i, multiplied by i in each iteration until the value is not greater than n.
Below is given the implementation:
// CPP program to find count of
// perfect power from 1 to n
#include <bits/stdc++.h>
using namespace std;
// Function to find count of perfect power
int perfectPowers(int n) {
// to store all unique power numbers
set<int> res;
res.insert(1);
for (int i = 2; i * i <= n; i++) {
for(int j = i * i; j <= n; j *= i) {
res.insert(j);
}
}
return res.size();
}
int main() {
int n = 50;
cout << perfectPowers(n);
return 0;
}
// Java program to find count of
// perfect power from 1 to n
import java.util.*;
class GfG {
// Function to find count of perfect power
static int perfectPowers(int n) {
// to store all unique power numbers
Set<Integer> res = new HashSet<>();
res.add(1);
for (int i = 2; i * i <= n; i++) {
for (int j = i * i; j <= n; j *= i) {
res.add(j);
}
}
return res.size();
}
public static void main(String[] args) {
int n = 50;
System.out.println(perfectPowers(n));
}
}
# Python program to find count of
# perfect power from 1 to n
# Function to find count of perfect power
def perfectPowers(n):
# to store all unique power numbers
res = {1}
for i in range(2, int(n**0.5) + 1):
j = i * i
while j <= n:
res.add(j)
j *= i
return len(res)
# Driver code
if __name__ == "__main__":
n = 50
print(perfectPowers(n))
// C# program to find count of
// perfect power from 1 to n
using System;
using System.Collections.Generic;
class GfG {
// Function to find count of perfect power
static int perfectPowers(int n) {
// to store all unique power numbers
HashSet<int> res = new HashSet<int>();
res.Add(1);
for (int i = 2; i * i <= n; i++) {
for (int j = i * i; j <= n; j *= i) {
res.Add(j);
}
}
return res.Count;
}
static void Main(string[] args) {
int n = 50;
Console.WriteLine(perfectPowers(n));
}
}
// JavaScript program to find count of
// perfect power from 1 to n
// Function to find count of perfect power
function perfectPowers(n) {
// to store all unique power numbers
let res = new Set();
res.add(1);
for (let i = 2; i * i <= n; i++) {
for (let j = i * i; j <= n; j *= i) {
res.add(j);
}
}
return res.size;
}
// Driver code
let n = 50;
console.log(perfectPowers(n));
Output
10
Time Complexity: O(n log n)
Space Complexity: O(n)
[Expected Approach] - O(n log n) Time and O(n ^ (1/4)) Space
The idea is to separate the odd powers from even powers and calculate their numbers separately. For calculating all the even powers less than and equal to n, we only need the square root of n. Because, the count of even powers smaller than n is equal to the square root of n. For odd powers, the above approach can be used.
Below is given the implementation:
// CPP program to find count of
// perfect power from 1 to n
#include <bits/stdc++.h>
using namespace std;
// Function to find count of perfect power
int perfectPowers(int n) {
// to store all unique power numbers
set<int> res;
// Iterate for base number i
// from 2 to cube root of n
for (int i = 2; i * i * i <= n; i++) {
// starting from i square
int j = i * i;
// increase the power of j till it is <= n
while (j * i <= n) {
j *= i;
// only adding those values of j which
// don't have a integral square root
int s = sqrt(j);
if (s * s != j)
res.insert(j);
}
}
// Num of even powers is equal to the square root of n.
int numOfEven = (int)sqrt(n);
// Returning number of odd and even powers
return res.size() + numOfEven;
}
int main()
{
int n = 50;
cout << perfectPowers(n);
return 0;
}
// Java program to find count of
// perfect power from 1 to n
import java.util.*;
class GfG {
// Function to find count of perfect power
static int perfectPowers(int n) {
// to store all unique power numbers
Set<Integer> res = new HashSet<>();
// Iterate for base number i
// from 2 to cube root of n
for (int i = 2; i * i * i <= n; i++) {
// starting from i square
int j = i * i;
// increase the power of j till it is <= n
while (j * i <= n) {
j *= i;
// only adding those values of j which
// don't have an integral square root
int s = (int) Math.sqrt(j);
if (s * s != j) {
res.add(j);
}
}
}
// Num of even powers is equal to the square root of n.
int numOfEven = (int) Math.sqrt(n);
// Returning number of odd and even powers
return res.size() + numOfEven;
}
public static void main(String[] args) {
int n = 50;
System.out.println(perfectPowers(n));
}
}
# Python program to find count of
# perfect power from 1 to n
# Function to find count of perfect power
def perfectPowers(n):
# to store all unique power numbers
res = set()
# Iterate for base number i
# from 2 to cube root of n
for i in range(2, int(n**(1/3)) + 1):
# starting from i square
j = i * i
# increase the power of j till it is <= n
while j * i <= n:
j *= i
# only adding those values of j which
# don't have an integral square root
s = int(j**0.5)
if s * s != j:
res.add(j)
# Num of even powers is equal to the square root of n.
numOfEven = int(n**0.5)
# Returning number of odd and even powers
return len(res) + numOfEven
# Driver code
if __name__ == "__main__":
n = 50
print(perfectPowers(n))
// C# program to find count of
// perfect power from 1 to n
using System;
using System.Collections.Generic;
class GfG {
// Function to find count of perfect power
static int perfectPowers(int n) {
// to store all unique power numbers
HashSet<int> res = new HashSet<int>();
// Iterate for base number i
// from 2 to cube root of n
for (int i = 2; i * i * i <= n; i++) {
// starting from i square
int j = i * i;
// increase the power of j till it is <= n
while (j * i <= n) {
j *= i;
// only adding those values of j which
// don't have an integral square root
int s = (int)Math.Sqrt(j);
if (s * s != j) {
res.Add(j);
}
}
}
// Num of even powers is equal to the square root of n.
int numOfEven = (int)Math.Sqrt(n);
// Returning number of odd and even powers
return res.Count + numOfEven;
}
static void Main(string[] args) {
int n = 50;
Console.WriteLine(perfectPowers(n));
}
}
// JavaScript program to find count of
// perfect power from 1 to n
// Function to find count of perfect power
function perfectPowers(n) {
// to store all unique power numbers
let res = new Set();
// Iterate for base number i
// from 2 to cube root of n
for (let i = 2; i * i * i <= n; i++) {
// starting from i square
let j = i * i;
// increase the power of j till it is <= n
while (j * i <= n) {
j *= i;
// only adding those values of j which
// don't have an integral square root
let s = Math.floor(Math.sqrt(j));
if (s * s !== j) {
res.add(j);
}
}
}
// Num of even powers is equal to the square root of n.
let numOfEven = Math.floor(Math.sqrt(n));
// Returning number of odd and even powers
return res.size + numOfEven;
}
// Driver code
let n = 50;
console.log(perfectPowers(n));
Output
10
Time Complexity: O(n log n)
Auxiliary Space: O(n ^ (1/4)), since we are only storing the odd powers less than n