How to Find the Product of Unique Prime Factors of a Number in Python

In this tutorial, we’ll learn how to program How to Find the Product of Unique Prime Factors of a Number in Python. The objective is to accurately find the product of all unique prime factors of a given number. This tutorial will guide you through the process step by step to ensure an efficient and reliable approach to calculating the product. So, let’s get started!

This topic is straightforward to understand. Just follow the instructions I provide, and you’ll complete it with ease. The program I’ll demonstrate will show you the correct way to find and calculate the product of unique prime factors of a number. So, let’s dive into the coding process!

Getting Started:

First you will have to download & install the Python IDLE's, here's the link for the Integrated Development And Learning Environment for Python https://www.python.org/downloads/.

Creating Main Function

This is the main function of the application. The following code will display a simple GUI in terminal console that will display program. To do this, simply copy and paste these blocks of code into the IDLE text editor.
  1. def productPrimeFactors(n):
  2.         product = 1
  3.  
  4.         for i in range(2, n+1):
  5.                 if (n % i == 0):
  6.                         isPrime = 1
  7.  
  8.                         for j in range(2, int(i/2 + 1)):
  9.                                 if (i % j == 0):
  10.                                         isPrime = 0
  11.                                         break
  12.  
  13.                         if (isPrime):
  14.                                 product = product * i
  15.  
  16.         return product
  17.  
  18. while True:
  19.         print("\n============= Find the Product of Unique Prime Factors of a Number =============\n")
  20.  
  21.         my_number = int(input("Enter a number: "))
  22.         print(productPrimeFactors(my_number))
  23.  
  24.         opt = input("\nDo you want to try again?(yes/no): ")
  25.  
  26.         if opt.lower() == 'yes':
  27.                 ret=False
  28.         elif opt.lower() == 'no':
  29.                 ret=True
  30.                 print("Exiting program....")
  31.         else:
  32.                 print("Please enter yes/no:")
  33.                 break
  34.  
  35.         if ret == False:
  36.                 continue

This program calculates the product of all unique prime factors of a given number by checking which numbers divide the input without a remainder, verifying if those divisors are prime, multiplying only the prime ones, and continuously allows the user to input new numbers until they choose to exit.

Output:

There you have it we successfully created How to Find the Product of Unique Prime Factors of a Number in Python. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

More Tutorials for Python Language

Python Tutorials

Add new comment