Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Python Program for Product of unique prime factors of a number
Next article icon

Python Program for Product of unique prime factors of a number

Last Updated : 14 Mar, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

Given a number n, we need to find the product of all of its unique prime factors. Prime factors: It is basically a factor of the number that is a prime number itself. Examples:

Input: num = 10  Output: Product is 10  Explanation:  Here, the input number is 10 having only 2 prime factors and they are 5 and 2.  And hence their product is 10.    Input : num = 25  Output: Product is 5  Explanation:  Here, for the input to be 25  we have only one unique prime factor i.e 5.  And hence the required product is 5.

Method 1 (Simple) Using a loop from i = 2 to n and check if i is a factor of n then check if i is prime number itself if yes then store product in product variable and continue this process till i = n. 

Python3
# Python program to find sum of given # series.   def productPrimeFactors(n):     product = 1      for i in range(2, n+1):         if (n % i == 0):             isPrime = 1              for j in range(2, int(i/2 + 1)):                 if (i % j == 0):                     isPrime = 0                     break              # condition if \'i\' is Prime number             # as well as factor of num             if (isPrime):                 product = product * i      return product   # main() n = 44 print(productPrimeFactors(n))  # Contributed by _omg 

Output:

22

Time complexity: O(n^2/2)
Auxiliary space: O(1)

Method 2 (Efficient) : The idea is based on Efficient program to print all prime factors of a given number 

Python3
# Python program to find product of  # unique prime factors of a number  import math  def productPrimeFactors(n):     product = 1          # Handle prime factor 2 explicitly so that     # can optimally handle other prime factors.     if (n % 2 == 0):         product *= 2         while (n%2 == 0):             n = n/2                  # n must be odd at this point. So we can     # skip one element (Note i = i +2)     for i in range (3, int(math.sqrt(n)), 2):         # While i divides n, print i and         # divide n         if (n % i == 0):             product = product * i             while (n%i == 0):                 n = n/i                      # This condition is to handle the case when n     # is a prime number greater than 2     if (n > 2):         product = product * n              return product           # main() n = 44 print (int(productPrimeFactors(n)))  # Contributed by _omg 

Output:

22

Time complexity: O(sqrt(n)), where n is the input number.
Auxiliary space: O(1), as the program only uses a constant amount of memory to store the product and the loop variables.

Please refer complete article on Product of unique prime factors of a number for more details!


Next Article
Python Program for Product of unique prime factors of a number

K

kartik
Improve
Article Tags :
  • DSA
  • Python Programs

Similar Reads

    Python program to find product of given number of consecutive elements
    Given a List, the task is to write a python program that can construct a list with products of consecutive elements for a given number of elements. Input : test_list = [5, 6, 2, 1, 7, 5, 3], K = 3 Output : [60, 12, 14, 35, 105] Explanation : 5 * 6 * 2 = 60, 6 * 2 * 1 = 12.. And so on. Input : test_l
    3 min read
    Factorial of a Number - Python
    The factorial of a number is the product of all positive integers less than or equal to that number. For example, the factorial of 5 (denoted as 5!) is 5 × 4 × 3 × 2 × 1 = 120. In Python, we can calculate the factorial of a number using various methods, such as loops, recursion, built-in functions,
    4 min read
    Python | Product of Prefix in list
    Nowadays, especially in competitive programming, the utility of computing prefix product is quite popular and features in many problems. Hence, having a one-liner solution to it would possess a great help. Let’s discuss certain ways in which this problem can be solved. Method 1: Using list comprehen
    4 min read
    Check Prime Number in Python
    Given a positive integer N, the task is to write a Python program to check if the number is Prime or not in Python. For example, given a number 29, it has no divisors other than 1 and 29 itself. Hence, it is a prime number.Note: Negative numbers (e.g. -13) are not considered prime number.Using sympy
    7 min read
    Python | Sliced Product in List
    Accessing elements in a list has many types and variations. These are an essential part of Python programming and one must know to perform the same. This article discusses ways to fetch the initial K elements and do its multiplication. Let’s discuss a certain solution to perform this task. Method #1
    5 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences