Check whether a given Number is Power-Isolated or not

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php 
// PHP program to find whether a number
// is power-isolated or not
  
function checkIfPowerIsolated($num)
{
        $input = $num;
        $count = 0;
        $factor= array();
  
        // for 2 as prime factor
        if($num%2==0)
        {
            while($num%2==0)
            {
                ++$count;
                $num/=2;
            }    
            $factor[2] = $count;
        }
  
        // for odd prime factor
        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;
        // calculate product of powers and prime factors  
        $product  = 1;
        foreach ($factor as $primefactor => $power) {
            $product = $product * $primefactor * $power;
        }
          
        // 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);
  
?>

chevron_right


Output:

Power-isolated Integer
Not a Power-isolated Integer
Power-isolated Integer


My Personal Notes arrow_drop_up

Discovering ways to develop a plane for soaring career goals

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : Mithun Kumar, Code_Mech