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++
// 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. |
Python3
# 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 |
PHP
<?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); ?> |
Power-isolated Integer Not a Power-isolated Integer Power-isolated Integer
Recommended Posts:
- Check if a number is divisible by all prime divisors of another number
- Check whether all the rotations of a given number is greater than or equal to the given number or not
- Program to check whether a number is Proth number or not
- Check if a number with even number of digits is palindrome or not
- Check if given number is Emirp Number or not
- Check if a number is Triperfect Number
- Check Whether a number is Duck Number or not
- Check whether a number is Non-hypotenuse number
- Check if a number is a Krishnamurthy Number or not
- Check if a number is a Trojan Number
- PHP | Check if a number is armstrong number
- Check whether the given number is Euclid Number or not
- Check if a number is a power of another number
- Check if a number is a Mystery Number
- Check if a number is Flavius Number
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.



