Monday, 9 December 2024

Day 25 : Python Program to find the prime factor of a number

 

def prime_factors(n):

    factors = []

    while n % 2 == 0:

        factors.append(2)

        n //= 2

    i = 3

    while i <= n:

        if n % i == 0:

            factors.append(i)

            n //= i

        else:

            i += 2  

    return factors

num = int(input("Enter a number: "))

factors = prime_factors(num)

print(f"Prime factors of {num} are: {factors}")

Code Explanation:

1. Function Definition

def prime_factors(n):
    factors = []
A function named prime_factors is defined to calculate the prime factors of a number n.
An empty list factors is initialized to store the prime factors.

2. Handle Factor of 2 (Even Prime)

while n % 2 == 0:
    factors.append(2)
    n //= 2
This loop continuously divides n by 2 as long as it is divisible by 2.
Each time n is divisible by 2:
2 is added to the factors list.
n is updated to n // 2 (integer division).
After this step, n becomes an odd number because all factors of 2 are removed.

3. Handle Odd Factors

i = 3
while i <= n:
A variable i is initialized to 3 to check for odd factors.
The loop runs as long as i is less than or equal to n.

if n % i == 0:
    factors.append(i)
    n //= i
Inside the loop:
If n is divisible by i, it adds i to the factors list and updates n to n // i.
This process removes all occurrences of the factor i.

else:
    i += 2
If n is not divisible by i, the next odd number (i + 2) is tested as a potential factor.
Only odd numbers are checked because all even factors are already removed.

4. Return the Factors

return factors
Once all factors are processed (i.e., n becomes 1), the function returns the list of prime factors.

5. User Input and Function Call

num = int(input("Enter a number: "))
The user is prompted to input an integer (num) for which the prime factors will be calculated.

factors = prime_factors(num)
print(f"Prime factors of {num} are: {factors}")
The prime_factors function is called with num as its argument.
The returned list of factors is stored in the variable factors and printed to the user.

#source code --> clcoding.com 

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (28) AI (33) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (173) C (77) C# (12) C++ (82) Course (67) Coursera (223) Cybersecurity (24) data management (11) Data Science (127) Data Strucures (8) Deep Learning (20) Django (14) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Google (34) Hadoop (3) HTML&CSS (47) IBM (25) IoT (1) IS (25) Java (93) Leet Code (4) Machine Learning (59) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (1) Pandas (4) PHP (20) Projects (29) Python (923) Python Coding Challenge (318) Python Quiz (4) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (2) Software (17) SQL (42) UX Research (1) web application (8)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses