Python Program to Find the Prime Factors of a Number

The provided Python program is designed to find the prime factors of a given number. It utilizes a function called prime_factors() to accomplish this task. The program takes a number as input, either from the user or as a command-line argument, and proceeds to calculate the prime factors.

Problem Statement

Write a Python program that prompts the user to enter a positive integer and then finds and displays the prime factors of that number.

Requirements:

  • The program should validate that the input is a positive integer. If the input is invalid, display an error message and prompt the user again until a valid input is provided.
  • The program should calculate and display all the prime factors of the given number.
  • The prime factors should be displayed in ascending order.

Python Program to Find the Prime Factors of a Number

def prime_factors(n):
    factors = []
    i = 2

    while i * i <= n:
        if n % i:
            i += 1
        else:
            n //= i
            factors.append(i)

    if n > 1:
        factors.append(n)

    return factors

# Example usage
number = int(input("Enter a number: "))
factors = prime_factors(number)

print("Prime factors of", number, "are:", factors)

How it Work

The Python program to find the prime factors of a given number works as follows:

  1. Prompt the user to enter a positive integer.
  2. Validate the input to ensure it is a positive integer. If the input is invalid (not a positive integer), display an error message and prompt the user again until a valid input is provided.
  3. Define a function called prime_factors(n) that takes the input number n as a parameter.
  4. Initialize an empty list called factors to store the prime factors.
  5. Initialize a variable i to 2.
  6. Use a while loop that runs until i * i is greater than n. This optimization ensures that we only check potential factors up to the square root of n.
  7. Inside the while loop, check if n is divisible evenly by i. If it is, divide n by i, append i to the factors list, and continue to the next iteration of the loop.
  8. If n is not divisible by i, increment i by 1 and continue to the next iteration of the loop.
  9. After the while loop, check if n is greater than 1. If it is, it means n itself is a prime number, so append it to the factors list.
  10. Return the factors list.
  11. Call the prime_factors() function with the input number and store the result in a variable.
  12. Print the prime factors of the given number.

The program follows this algorithm to find the prime factors of the input number. It utilizes a while loop, division, and iteration to determine the prime factors. The output is then displayed to the user.

Input/ Output

Python Program to Find the Prime Factors of a Number

Explanation: The user enters the number 56. The prime factors of 56 are 2, 2, 2, and 7. The program calculates and displays the prime factors in ascending order.

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

In this python tutorial, you will learn how to Display Prime Numbers Between Two Intervals using the if and else...
In this python tutorial, you will learn how to Calculate Standard Deviation with built in functions of the python programming...
In this Python program, we will convert temperature values from Celsius to Fahrenheit. The Celsius and Fahrenheit scales are two...