Check whether a given Number is Power-Isolated or not
Last Updated :
21 Sep, 2023
Given a integer N, with prime factorisation n1p1 * n2p2 …… The task is to check if the integer N is power-isolated or not.
An integer is said to be power-isolated if n1 * p1 * n2 * p2 ….. = N.
Examples:
Input: N = 12
Output: Power-isolated Integer.
Input: N = 18
Output: Not a power-isolated integer.
Approach: For an integer to be power-isolated the product of its prime factors and their power is equal to integer itself. So, for calculating same you have to find all prime factors of the given integer and their respective powers too. Later, calculate their product and check whether product is equal to the integer or not.
Algorithm:
- Find the prime factor with their factor and store them in key-value pair.
- Later calculate product of all factors and their powers.
- if product is equal to integer, print true else false.
Below is the implementation of the above algorithm:
C++
#include <bits/stdc++.h>
using namespace std;
void checkIfPowerIsolated( int num)
{
int input = num;
int count = 0;
int factor[num + 1] = { 0 };
if (num % 2 == 0) {
while (num % 2 == 0) {
++count;
num /= 2;
}
factor[2] = count;
}
for ( int i = 3; i * i <= num; i += 2) {
count = 0;
while (num % i == 0) {
++count;
num /= i;
}
if (count > 0)
factor[i] = count;
}
if (num > 1)
factor[num] = 1;
int product = 1;
for ( int i = 0; i < num + 1; i++) {
if (factor[i] > 0)
product = product * factor[i] * i;
}
if (product == input)
cout << "Power-isolated Integer\n" ;
else
cout << "Not a Power-isolated Integer\n" ;
}
int main()
{
checkIfPowerIsolated(12);
checkIfPowerIsolated(18);
checkIfPowerIsolated(35);
return 0;
}
|
Java
class GFG {
static void checkIfPowerIsolated( int num)
{
int input = num;
int count = 0 ;
int [] factor = new int [num + 1 ];
if (num % 2 == 0 ) {
while (num % 2 == 0 ) {
++count;
num /= 2 ;
}
factor[ 2 ] = count;
}
for ( int i = 3 ; i * i <= num; i += 2 ) {
count = 0 ;
while (num % i == 0 ) {
++count;
num /= i;
}
if (count > 0 )
factor[i] = count;
}
if (num > 1 )
factor[num] = 1 ;
int product = 1 ;
for ( int i = 0 ; i < num + 1 ; i++) {
if (factor[i] > 0 )
product = product * factor[i] * i;
}
if (product == input)
System.out.print( "Power-isolated Integer\n" );
else
System.out.print(
"Not a Power-isolated Integer\n" );
}
public static void main(String[] args)
{
checkIfPowerIsolated( 12 );
checkIfPowerIsolated( 18 );
checkIfPowerIsolated( 35 );
}
}
|
Python3
def checkIfPowerIsolated(num):
input1 = num
count = 0
factor = [ 0 ] * (num + 1 )
if (num % 2 = = 0 ):
while (num % 2 = = 0 ):
count + = 1
num / / = 2
factor[ 2 ] = count
i = 3
while (i * i < = num):
count = 0
while (num % i = = 0 ):
count + = 1
num / / = i
if (count > 0 ):
factor[i] = count
i + = 2
if (num > 1 ):
factor[num] = 1
product = 1
for i in range ( 0 , len (factor)):
if (factor[i] > 0 ):
product = product * factor[i] * i
if (product = = input1):
print ( "Power-isolated Integer" )
else :
print ( "Not a Power-isolated Integer" )
checkIfPowerIsolated( 12 )
checkIfPowerIsolated( 18 )
checkIfPowerIsolated( 35 )
|
C#
using System;
class GFG {
static void checkIfPowerIsolated( int num)
{
int input = num;
int count = 0;
int [] factor = new int [num + 1];
if (num % 2 == 0) {
while (num % 2 == 0) {
++count;
num /= 2;
}
factor[2] = count;
}
for ( int i = 3; i * i <= num; i += 2) {
count = 0;
while (num % i == 0) {
++count;
num /= i;
}
if (count > 0)
factor[i] = count;
}
if (num > 1)
factor[num] = 1;
int product = 1;
for ( int i = 0; i < num + 1; i++) {
if (factor[i] > 0)
product = product * factor[i] * i;
}
if (product == input)
Console.Write( "Power-isolated Integer\n" );
else
Console.Write( "Not a Power-isolated Integer\n" );
}
static void Main()
{
checkIfPowerIsolated(12);
checkIfPowerIsolated(18);
checkIfPowerIsolated(35);
}
}
|
Javascript
<script>
function checkIfPowerIsolated(num)
{
let input = num;
let count = 0;
let factor = new Array(0);
if (num % 2 == 0)
{
while (num % 2 == 0)
{
++count;
num/=2;
}
factor[2] = count;
}
for (let i = 3; i*i <= num; i += 2)
{
count = 0;
while (num % i == 0)
{
++count;
num /= i;
}
if (count > 0)
factor[i] = count;
}
if (num > 1)
factor[num] = 1;
let product = 1;
for (let i = 0; i < num + 1; i++)
{
if (factor[i] > 0)
product = product * factor[i] * i;
}
if (product == input)
document.write( "Power-isolated Integer" + "<br>" );
else
document.write( "Not a Power-isolated Integer" + "<br>" );
}
checkIfPowerIsolated(12);
checkIfPowerIsolated(18);
checkIfPowerIsolated(35);
</script>
|
PHP
<?php
function checkIfPowerIsolated( $num )
{
$input = $num ;
$count = 0;
$factor = array ();
if ( $num %2==0)
{
while ( $num %2==0)
{
++ $count ;
$num /=2;
}
$factor [2] = $count ;
}
for ( $i =3; $i * $i <= $num ; $i +=2)
{
$count = 0;
while ( $num % $i ==0)
{
++ $count ;
$num /= $i ;
}
if ( $count )
$factor [ $i ] = $count ;
}
if ( $num >1)
$factor [ $num ] = 1;
$product = 1;
foreach ( $factor as $primefactor => $power ) {
$product = $product * $primefactor * $power ;
}
if ( $product == $input )
print_r( "Power-isolated Integer\n" );
else
print_r( "Not a Power-isolated Integer\n" );
}
checkIfPowerIsolated(12);
checkIfPowerIsolated(18);
checkIfPowerIsolated(35);
?>
|
Output
Power-isolated Integer
Not a Power-isolated Integer
Power-isolated Integer
Time Complexity: O(num)
Auxiliary Space: O(num)
Approach 2 :
- Define a function named checkIfPowerIsolated that takes an integer num as input.
- Create a variable named input and assign the value of num to it.
- Create a variable named count and assign it a value of 0.
- Create an integer array named factor of size num + 1 and initialize all its elements to 0. This array will store the prime factors and their powers.
- Check if num is divisible by 2. If so, do the following:
a. Create a while loop that runs while num is even.
b. Divide num by 2 and increment the count variable by 1.
c. Store the value of count in the factor array at index 2.
- Check for odd prime factors by creating a for loop that runs from 3 to the square root of num, incrementing by 2 each time. Do the following within this loop:
a. Reset the count variable to 0.
b. Create a while loop that runs while num is divisible by the current value of i.
c. Divide num by i and increment the count variable by 1.
d. Store the value of count in the factor array at index i.
e. If the count value is greater than 0, repeat steps b to d.
- If num is greater than 1, store the value 1 in the factor array at index num.
- Create a variable named product and assign it a value of 1. This variable will store the product of the prime factors and their powers.
- Create a for loop that runs from 0 to num + 1. Do the following within this loop:
a. Check if the value at the current index of the factor array is greater than 0.
b. If so, multiply product by the value of i raised to the power of the value at the current index of the factor array.
- Check if product is equal to input. If so, print “Power-isolated Integer”. If not, print “Not a Power-isolated Integer”.
C++
#include <bits/stdc++.h>
using namespace std;
void checkIfPowerIsolated( int num)
{
int input = num;
int count = 0;
int factor[num + 1] = { 0 };
if (num % 2 == 0) {
while (num % 2 == 0) {
++count;
num /= 2;
}
factor[2] = count;
}
for ( int i = 3; i * i <= num; i += 2) {
count = 0;
while (num % i == 0) {
++count;
num /= i;
}
if (count > 0)
factor[i] = count;
}
if (num > 1)
factor[num] = 1;
int product = 1;
for ( int i = 0; i < num + 1; i++) {
if (factor[i] > 0)
product = product * pow (i, factor[i]);
}
if (product == input)
cout << "Power-isolated Integer\n" ;
else
cout << "Not a Power-isolated Integer\n" ;
}
int main()
{
checkIfPowerIsolated(12);
checkIfPowerIsolated(18);
checkIfPowerIsolated(35);
return 0;
}
|
Java
import java.util.Arrays;
public class GFG {
public static void checkIfPowerIsolated( int num) {
int input = num;
int count = 0 ;
int [] factor = new int [num + 1 ];
if (num % 2 == 0 ) {
while (num % 2 == 0 ) {
++count;
num /= 2 ;
}
factor[ 2 ] = count;
}
for ( int i = 3 ; i * i <= num; i += 2 ) {
count = 0 ;
while (num % i == 0 ) {
++count;
num /= i;
}
if (count > 0 ) {
factor[i] = count;
}
}
if (num > 1 ) {
factor[num] = 1 ;
}
int product = 1 ;
for ( int i = 0 ; i < num + 1 ; i++) {
if (factor[i] > 0 ) {
product *= Math.pow(i, factor[i]);
}
}
if (product == input) {
System.out.println( "Power-isolated Integer" );
} else {
System.out.println( "Not a Power-isolated Integer" );
}
}
public static void main(String[] args) {
checkIfPowerIsolated( 12 );
checkIfPowerIsolated( 18 );
checkIfPowerIsolated( 35 );
}
}
|
Python3
def checkIfPowerIsolated(num):
input_num = num
count = 0
factor = [ 0 ] * (num + 1 )
if num % 2 = = 0 :
while num % 2 = = 0 :
count + = 1
num / / = 2
factor[ 2 ] = count
for i in range ( 3 , int (num * * 0.5 ) + 1 , 2 ):
count = 0
while num % i = = 0 :
count + = 1
num / / = i
if count > 0 :
factor[i] = count
if num > 1 :
factor[num] = 1
product = 1
for i in range (num + 1 ):
if factor[i] > 0 :
product * = i * * factor[i]
if product = = input_num:
print ( "Power-isolated Integer" )
else :
print ( "Not a Power-isolated Integer" )
checkIfPowerIsolated( 12 )
checkIfPowerIsolated( 18 )
checkIfPowerIsolated( 35 )
|
C#
using System;
class GFG
{
static void CheckIfPowerIsolated( int num)
{
int input = num;
int count = 0;
int [] factor = new int [num + 1];
if (num % 2 == 0)
{
while (num % 2 == 0)
{
count++;
num /= 2;
}
factor[2] = count;
}
for ( int i = 3; i * i <= num; i += 2)
{
count = 0;
while (num % i == 0)
{
count++;
num /= i;
}
if (count > 0)
factor[i] = count;
}
if (num > 1)
factor[num] = 1;
int product = 1;
for ( int i = 0; i < num + 1; i++)
{
if (factor[i] > 0)
product *= ( int )Math.Pow(i, factor[i]);
}
if (product == input)
Console.WriteLine( "Power-isolated Integer" );
else
Console.WriteLine( "Not a Power-isolated Integer" );
}
static void Main()
{
CheckIfPowerIsolated(12);
CheckIfPowerIsolated(18);
CheckIfPowerIsolated(35);
}
}
|
Javascript
function checkIfPowerIsolated(num)
{
let input = num;
let count = 0;
let factor = new Array(num + 1).fill(0);
if (num % 2 == 0) {
while (num % 2 == 0) {
++count;
num /= 2;
}
factor[2] = count;
}
for (let i = 3; i * i <= num; i += 2) {
count = 0;
while (num % i == 0) {
++count;
num /= i;
}
if (count > 0)
factor[i] = count;
}
if (num > 1)
factor[num] = 1;
let product = 1;
for (let i = 0; i < num + 1; i++) {
if (factor[i] > 0)
product = product * Math.pow(i, factor[i]);
}
if (product == input)
console.log( "Power-isolated Integer" );
else
console.log( "Not a Power-isolated Integer" );
}
checkIfPowerIsolated(12);
checkIfPowerIsolated(18);
checkIfPowerIsolated(35);
|
Output
Power-isolated Integer
Not a Power-isolated Integer
Power-isolated Integer
Time Complexity: O(sqrt(n))
Auxiliary Space: O(n)
Share your thoughts in the comments
Please Login to comment...