- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Python Program for Find largest prime factor of a number

In this article, we will learn about the solution to the problem statement given below −

## Problem statement

Given a positive integer n. We need to find the largest prime factor of a number.

## Approach

- Factorise the given number input by dividing it with the divisor of a number.
- Now keep updating the maximum prime factor.

## Example

import math def maxPrimeFactor(n): # number must be even while n % 2 == 0: max_Prime = 2 n /= 1 # number must be odd for i in range(3, int(math.sqrt(n)) + 1, 2): while n % i == 0: max_Prime = i n = n / i # prime number greator than two if n > 2: max_Prime = n return int(max_Prime) # Driver code to test above function n = 15 print(maxPrimeFactor(n))

Time complexity: O(n^½)

Auxiliary space: O(1)

## Output

5

All the variables are declared in the global frame as shown in the figure given below:

## Conclusion

In this article, we learnt about the approach to Find largest prime factor of a number

- Related Articles
- C Program for Find largest prime factor of a number?
- C Program for Find the largest prime factor of a number?
- Java Program to find largest prime factor of a number
- Find largest prime factor of a number using C++.
- Finding the largest prime factor of a number in JavaScript
- Python Program for Product of unique prime factors of a number
- Python program to find largest number in a list
- Prime factor array of a Number in JavaScript
- Find sum of a number and its maximum prime factor in C++
- Python Program for Efficient program to print all prime factors of a given number
- Python program to find the largest number in a list
- Prime Factor in C++ Program
- Python Program to Find if a Number is Prime or Not Prime Using Recursion
- Find the difference between the smallest 3 digit prime number and largest 1digit prime number
- Python program to find the second largest number in a list

Advertisements