Python Program to Find Prime Factors of a Number

If a number p is fully divisible by another number q, then q is said to be a factor of p. Here q will be greater than 1 and less than or equal to p.

So, if a number p is fully divisible by another number q and q is a prime number then q is said to be a prime factor of p.

Prime numbers are those numbers that are only divisible by 1 and themselves. In this article, we will see the code to find the prime factors of a number.

For example:

If we take 5, then prime factors will be:

5 = 5

5 is the only prime factor of 5.

If we take 20, then prime factors will be:

20 = 2 * 2 * 5

If we take 210, then prime factors will be:

210 = 2 * 3 * 5 * 7

Now let’s see the code of this problem. In this code, we will be creating two functions. The first function will help us to know if a number is prime or not. And the second function will help us to print the prime factor of a number.

Testcase 1: When the input is 100.

Output:

Testcase 2: When the Input is 270270.

Output:

Python Program to Find Prime Factors of a Number

We have used two functions to create the whole program.

The first function will tell us if a number is prime or not. It takes an integer and returns a Boolean value (True or false).

The second function will help to print the prime factors of a number. It will take an integer as input and print all the prime factors of that input. It returns None because all the printing is done within the function only.

Leave a Comment

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