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:
OutputPower-isolated Integer
Not a Power-isolated Integer
Power-isolated Integer
Time Complexity: O(num)
Auxiliary Space: O(num)
- 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".
OutputPower-isolated Integer
Not a Power-isolated Integer
Power-isolated Integer
Time Complexity: O(sqrt(n))
Auxiliary Space: O(n)