Perfect power (1, 4, 8, 9, 16, 25, 27, …)
A perfect power is a number that can be expressed as power of another positive integer.
Given a number n, find count of numbers from 1 to n that are of type xy where x >= 1 and y > 1
Examples :
Input : n = 10
Output : 4
1 4 8 and 9 are the numbers that are
of form x ^ y where x > 0 and y > 1
Input : n = 50
Output : 10
A simple solution is to go through all powers of numbers from i = 2 to square root of n.
C++
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int powerNumbers( int n)
{
vector< int > v;
v.push_back(1);
for (ll i = 2; i * i <= n; i++) {
ll j = i * i;
v.push_back(j);
while (j * i <= n) {
v.push_back(j * i);
j = j * i;
}
}
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
return v.size();
}
int main()
{
cout << powerNumbers(50);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
public class GFG {
static int powerNumbers( int n)
{
HashSet<Integer> v = new HashSet<Integer>();
v.add( 1 );
for ( int i = 2 ; i * i <= n; i++) {
int j = i * i;
v.add(j);
while (j * i <= n) {
v.add(j * i);
j = j * i;
}
}
return v.size();
}
public static void main(String args[])
{
System.out.print(powerNumbers( 50 ));
}
}
|
Python3
def powerNumbers(n):
v = set ();
v.add( 1 );
for i in range ( 2 , n + 1 ):
if (i * i < = n):
j = i * i;
v.add(j);
while (j * i < = n):
v.add(j * i);
j = j * i;
return len (v);
print (powerNumbers( 50 ));
|
C#
using System;
using System.Collections.Generic;
using System.Linq;
class GFG {
static int powerNumbers( int n)
{
HashSet< int > v = new HashSet< int >();
v.Add(1);
for ( int i = 2; i * i <= n; i++) {
int j = i * i;
v.Add(j);
while (j * i <= n) {
v.Add(j * i);
j = j * i;
}
}
return v.Count;
}
public static void Main()
{
Console.WriteLine(powerNumbers(50));
}
}
|
PHP
<?php
function powerNumbers( $n )
{
$v = array ();
array_push ( $v , 1);
for ( $i = 2; $i * $i <= $n ; $i ++)
{
$j = $i * $i ;
array_push ( $v , $j );
while ( $j * $i <= $n )
{
array_push ( $v , $j * $i );
$j = $j * $i ;
}
}
sort( $v );
$v = array_unique ( $v );
return count ( $v );
}
echo (powerNumbers(50));
?>
|
Javascript
<script>
function powerNumbers(n)
{
let v = new Set();
v.add(1);
for (let i = 2; i * i <= n; i++) {
let j = i * i;
v.add(j);
while (j * i <= n) {
v.add(j * i);
j = j * i;
}
}
return v.size;
}
document.write(powerNumbers(50));
</script>
|
Time Complexity: O(nlogn)
Auxiliary Space: O(n)
Efficient Solution
We divide output set into subsets.
Even Powers: Simply we need to square root n. The count of even powers smaller than n is square root of n. For example even powers smaller than 25 are (1, 4, 9, 16 and 25).
Odd Powers: We modify above function to consider only odd powers.
C++
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int powerNumbers( int n)
{
vector< int > v;
for (ll i = 2; i * i * i <= n; i++) {
ll j = i * i;
while (j * i <= n) {
j *= i;
ll s = sqrt (j);
if (s * s != j)
v.push_back(j);
}
}
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
return v.size() + (ll) sqrt (n);
}
int main()
{
cout << powerNumbers(50);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG
{
static long powerNumbers( int n)
{
HashSet<Long> v = new HashSet<Long>();
for ( long i = 2 ; i * i * i <= n; i++)
{
long j = i * i;
while (j * i <= n)
{
j *= i;
long s = ( long )Math.sqrt(j);
if (s * s != j)
v.add(j);
}
}
return v.size() + ( long )Math.sqrt(n);
}
public static void main(String args[])
{
System.out.print(powerNumbers( 50 ));
}
}
|
Python3
import math
def powerNumbers( n):
v = []
for i in range ( 2 ,
int (math. pow (n, 1.0 /
3.0 )) + 1 ) :
j = i * i
while (j * i < = n) :
j = j * i
s = int (math.sqrt(j))
if (s * s ! = j):
v.append(j)
v.sort()
v = list ( dict .fromkeys(v))
return len (v) + int (math.sqrt(n))
if __name__ = = '__main__' :
print (powerNumbers( 50 ))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static long powerNumbers( int n)
{
HashSet< long > v = new HashSet< long >();
for ( long i = 2; i * i * i <= n; i++)
{
long j = i * i;
while (j * i <= n)
{
j *= i;
long s = ( long )Math.Sqrt(j);
if (s * s != j)
v.Add(j);
}
}
return v.Count + ( long )Math.Sqrt(n);
}
static void Main()
{
Console.Write(powerNumbers(50));
}
}
|
PHP
<?php
function powerNumbers( $n )
{
$v = array ();
for ( $i = 2; $i * $i * $i <= $n ; $i ++)
{
$j = $i * $i ;
while ( $j * $i <= $n )
{
$j *= $i ;
$s = sqrt( $j );
if ( $s * $s != $j )
array_push ( $v , $j );
}
}
sort( $v );
$uni = array_unique ( $v );
for ( $i = 0; $i < count ( $uni ); $i ++)
{
$key = array_search ( $uni [ $i ], $v );
unset( $v [ $key ]);
}
return count ( $v ) + 3 +
intval (sqrt( $n ));
}
echo (powerNumbers(50));
?>
|
Javascript
<script>
function powerNumbers(n)
{
let v = new Set();
for (let i = 2; i * i * i <= n; i++)
{
let j = i * i;
while (j * i <= n)
{
j *= i;
let s = parseInt(Math.sqrt(j), 10);
if (s * s != j)
v.add(j);
}
}
return v.size + parseInt(Math.sqrt(n), 10);
}
document.write(powerNumbers(50));
</script>
|
Time Complexity: O(nlogn)
Auxiliary Space: O(n^(1/4))
Last Updated :
14 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...