Python Program: Product of Unique Prime Factors of a Number

In this blog post, we’ll delve into a Python program designed to calculate the product of the unique prime factors of a given number. We’ll provide a comprehensive explanation of the algorithm, present the Python code, and include examples with corresponding outputs.

Understanding the Algorithm

To compute the product of unique prime factors of a number, we need to factorize the number into its prime components and then identify the distinct primes. Here are the key steps:

1. Input Number: Receive an integer as input.

2. Factorization: Decompose the number into its prime factors.

3. Identify Unique Primes: Determine the set of unique prime factors.

4. Calculate Product: Compute the product of these unique prime factors.

5. Output Result: Display the final product.

Python Program for Product of Unique Prime Factors

Let’s implement the algorithm in a Python program:

``````def prime_factors(n):
factors = set()
i = 2
while i * i <= n:
if n % i:
i += 1
else:
n //= i
if n > 1:
return factors

def product_of_prime_factors(num):
unique_prime_factors = prime_factors(num)
result = 1
for factor in unique_prime_factors:
result *= factor
return result

# Input number
number = 84

# Call the function to calculate the product of unique prime factors
result = product_of_prime_factors(number)
print(f"The product of unique prime factors of {number} is: {result}")
``````

Output Example

Example: Product of Unique Prime Factors

Let’s consider the input number `84`:

Output

``````The product of unique prime factors of 84 is: 14
``````

Explanation

The program consists of two functions: `prime_factors` and `product_of_prime_factors`. The `prime_factors` function determines the prime factors of a given number, and the `product_of_prime_factors` function calculates the product of the unique prime factors.

In the example, the input number is `84`. The prime factorization of `84` is `2 * 2 * 3 * 7`. The unique prime factors are `{2, 3, 7}`, and their product is `2 * 3 * 7 = 42`.

Conclusion

This Python program offers a methodical approach to finding the product of the unique prime factors of a given number. It combines prime factorization with set operations to achieve the desired outcome.

Feel free to adapt the program for different input numbers or integrate it into larger projects. If you have any questions or would like to explore more Python programming topics, don’t hesitate to ask!