Check whether a given Number is PowerIsolated or not (original) (raw)

Check whether a given Number is Power-Isolated or not

Last Updated : 2 May, 2025

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:

Below is the implementation of the above algorithm:

C++ `

// C++ program to find whether a number // is power-isolated or not #include <bits/stdc++.h> using namespace std;

void checkIfPowerIsolated(int num) { int input = num; int count = 0; int factor[num + 1] = { 0 };

// for 2 as prime factor
if (num % 2 == 0) {
    while (num % 2 == 0) {
        ++count;
        num /= 2;
    }
    factor[2] = count;
}

// for odd prime factor
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;

// calculate product of powers and prime factors
int product = 1;
for (int i = 0; i < num + 1; i++) {
    if (factor[i] > 0)
        product = product * factor[i] * i;
}

// check result for power-isolation
if (product == input)
    cout << "Power-isolated Integer\n";
else
    cout << "Not a Power-isolated Integer\n";

}

// Driver code int main() { checkIfPowerIsolated(12); checkIfPowerIsolated(18); checkIfPowerIsolated(35); return 0; }

// This code is contributed by mits

Java

// Java program to find whether a number // is power-isolated or not class GFG {

static void checkIfPowerIsolated(int num)
{
    int input = num;
    int count = 0;
    int[] factor = new int[num + 1];

    // for 2 as prime factor
    if (num % 2 == 0) {
        while (num % 2 == 0) {
            ++count;
            num /= 2;
        }
        factor[2] = count;
    }

    // for odd prime factor
    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;

    // calculate product of powers and prime factors
    int product = 1;
    for (int i = 0; i < num + 1; i++) {
        if (factor[i] > 0)
            product = product * factor[i] * i;
    }

    // check result for power-isolation
    if (product == input)
        System.out.print("Power-isolated Integer\n");
    else
        System.out.print(
            "Not a Power-isolated Integer\n");
}

// Driver code
public static void main(String[] args)
{
    checkIfPowerIsolated(12);
    checkIfPowerIsolated(18);
    checkIfPowerIsolated(35);
}

}

// This code is contributed by Code_Mech.

Python

Python3 program to find whether a number

is power-isolated or not

def checkIfPowerIsolated(num):

input1 = num
count = 0
factor = [0] * (num + 1)

# for 2 as prime factor
if(num % 2 == 0):
    while(num % 2 == 0):
        count += 1
        num //= 2
    factor[2] = count

# for odd prime factor
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

# calculate product of powers and prime factors
product = 1
for i in range(0, len(factor)):
    if(factor[i] > 0):
        product = product * factor[i] * i

# check result for power-isolation
if (product == input1):
    print("Power-isolated Integer")
else:
    print("Not a Power-isolated Integer")

Driver code

checkIfPowerIsolated(12) checkIfPowerIsolated(18) checkIfPowerIsolated(35)

This code is contributed by mits

C#

// C# program to find whether a number // is power-isolated or not using System;

class GFG { static void checkIfPowerIsolated(int num) { int input = num; int count = 0; int[] factor = new int[num + 1];

    // for 2 as prime factor
    if (num % 2 == 0) {
        while (num % 2 == 0) {
            ++count;
            num /= 2;
        }
        factor[2] = count;
    }

    // for odd prime factor
    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;

    // calculate product of powers and prime factors
    int product = 1;
    for (int i = 0; i < num + 1; i++) {
        if (factor[i] > 0)
            product = product * factor[i] * i;
    }

    // check result for power-isolation
    if (product == input)
        Console.Write("Power-isolated Integer\n");
    else
        Console.Write("Not a Power-isolated Integer\n");
}

// Driver code
static void Main()
{
    checkIfPowerIsolated(12);
    checkIfPowerIsolated(18);
    checkIfPowerIsolated(35);
}

}

// This code is contributed by mits

JavaScript

PHP

input=input = input=num; $count = 0; $factor= array(); // for 2 as prime factor if($num%2==0) { while($num%2==0) { ++$count; $num/=2; } factor[2]=factor[2] = factor[2]=count; } // for odd prime factor for ($i=3; i∗i*ii <= num;num; num;i+=2) { $count = 0; while($num%$i==0) { ++$count; num/=num /= num/=i; } if($count) factor[factor[factor[i] = $count; } if($num>1) factor[factor[factor[num] = 1; // calculate product of powers and prime factors $product = 1; foreach ($factor as primefactor=>primefactor => primefactor=>power) { product=product = product=product * primefactor∗primefactor * primefactorpower; } // check result for power-isolation if ($product == $input) print_r("Power-isolated Integer\n"); else print_r("Not a Power-isolated Integer\n"); } // driver code 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 :

  1. Define a function named checkIfPowerIsolated that takes an integer num as input.
  2. Create a variable named input and assign the value of num to it.
  3. Create a variable named count and assign it a value of 0.
  4. 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.
  5. 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.
  6. 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.
  7. If num is greater than 1, store the value 1 in the factor array at index num.
  8. 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.
  9. 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.
  10. 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 };

// for 2 as prime factor
if (num % 2 == 0) {
    while (num % 2 == 0) {
        ++count;
        num /= 2;
    }
    factor[2] = count;
}

// for odd prime factor
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;

// calculate product of powers and prime factors
int product = 1;
for (int i = 0; i < num + 1; i++) {
    if (factor[i] > 0)
        product = product * pow(i, factor[i]);
}

// check result for power-isolation
if (product == input)
    cout << "Power-isolated Integer\n";
else
    cout << "Not a Power-isolated Integer\n";

}

// Driver code int main() { checkIfPowerIsolated(12); checkIfPowerIsolated(18); checkIfPowerIsolated(35); return 0; }

Java

import java.util.Arrays;

public class GFG { // Function to check if an integer is power-isolated public static void checkIfPowerIsolated(int num) { int input = num; int count = 0; int[] factor = new int[num + 1];

    // Check for 2 as a prime factor
    if (num % 2 == 0) {
        while (num % 2 == 0) {
            ++count;
            num /= 2;
        }
        factor[2] = count;
    }

    // Check for odd prime factors
    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;
    }

    // Calculate the product of powers and prime factors
    int product = 1;
    for (int i = 0; i < num + 1; i++) {
        if (factor[i] > 0) {
            product *= Math.pow(i, factor[i]);
        }
    }

    // Check if the result is a power-isolated integer
    if (product == input) {
        System.out.println("Power-isolated Integer");
    } else {
        System.out.println("Not a Power-isolated Integer");
    }
}

// Driver code
public static void main(String[] args) {
    checkIfPowerIsolated(12);
    checkIfPowerIsolated(18);
    checkIfPowerIsolated(35);
}

}

Python

def checkIfPowerIsolated(num): input_num = num count = 0 factor = [0] * (num + 1)

# for 2 as prime factor
if num % 2 == 0:
    while num % 2 == 0:
        count += 1
        num //= 2
    factor[2] = count

# for odd prime factor
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

# calculate product of powers and prime factors
product = 1
for i in range(num + 1):
    if factor[i] > 0:
        product *= i ** factor[i]

# check result for power-isolation
if product == input_num:
    print("Power-isolated Integer")
else:
    print("Not a Power-isolated Integer")

Driver code

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];

    // for 2 as a prime factor
    if (num % 2 == 0)
    {
        while (num % 2 == 0)
        {
            count++;
            num /= 2;
        }
        factor[2] = count;
    }

    // for odd prime factors
    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;

    // calculate product of powers and prime factors
    int product = 1;
    for (int i = 0; i < num + 1; i++)
    {
        if (factor[i] > 0)
            product *= (int)Math.Pow(i, factor[i]);
    }

    // check result for power-isolation
    if (product == input)
        Console.WriteLine("Power-isolated Integer");
    else
        Console.WriteLine("Not a Power-isolated Integer");
}

// Driver code
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);

// for 2 as prime factor
if (num % 2 == 0) {
    while (num % 2 == 0) {
        ++count;
        num /= 2;
    }
    factor[2] = count;
}

// for odd prime factor
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;

// calculate product of powers and prime factors
let product = 1;
for (let i = 0; i < num + 1; i++) {
    if (factor[i] > 0)
        product = product * Math.pow(i, factor[i]);
}

// check result for power-isolation
if (product == input)
    console.log("Power-isolated Integer");
else
    console.log("Not a Power-isolated Integer");

}

// Driver code 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)