Elements of Array which can be expressed as power of some integer to given exponent K | C ++ implementation to print elements of the Array which can be expressed as power of some integer to given exponent K ; Method returns Nth power of A ; Smaller eps , denotes more accuracy ; Initializing difference between two roots by INT_MAX ; x ^ K denotes current value of x ; loop untill we reach desired accuracy ; calculating current value from previous value by newton 's method ; Function to check whether its k root is an integer or not ; Function to find the numbers ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define ll  long long NEW_LINE double nthRoot ( ll A , ll N ) { double xPre = 7 ; double eps = 1e-3 ; double delX = INT_MAX ; double xK ; while ( delX > eps ) { xK = ( ( N - 1.0 ) * xPre + ( double ) A / pow ( xPre , N - 1 ) ) / ( double ) N ; delX = abs ( xK - xPre ) ; xPre = xK ; } return xK ; } bool check ( ll no , int k ) { double kth_root = nthRoot ( no , k ) ; ll num = kth_root ; if ( abs ( num - kth_root ) < 1e-4 ) return true ; return false ; } void printExpo ( ll arr [ ] , int n , int k ) { for ( int i = 0 ; i < n ; i ++ ) { if ( check ( arr [ i ] , k ) ) cout << arr [ i ] << " ▁ " ; } } int main ( ) { int K = 6 ; ll arr [ ] = { 46656 , 64 , 256 , 729 , 16 , 1000 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; printExpo ( arr , n , K ) ; return 0 ; }