Python Program to Print Prime Factor of Given Number

Python Program to Print Prime Factor of Given Number

In this tutorial, we will discuss how we can get the prime factor of the given number using the Python program. All we familiar with the prime numbers, if not then prime numbers are the number that can be divided by one or itself. For example – 1, 2, 3, 5, 7, 11, 13, ……

Finding all prime factorization of a number

If the user enters the number as 12, then the output must be ‘2, 2, 3, and if the input is 315; the output should be “3 3 5 7”. The program must return the prime all prime factor of given number. The prime factors of 330 are 2, 3, 5, and 11. Therefore 11 is the most significant prime factor of 330.

For Example: 330 = 2 × 3 × 5 × 11.

Before writing the Python program, let’s understand the following conjectures.

  • 1st Conjecture – There can be at-least one prime factor that would be less than √n in case of n is not a prime number.

Proof –There are two greater sqrt(n) numbers, then their product should also divide n but which will exceed n, which contradicts our assumption. So there can NOT be more than one prime factor of n greater than sqrt(n).

Let’s see the following step to perform such an operation.

  • 2nd Conjecture – There can be AT-MOST 1 prime factor of n greater than sqrt(n).

Proof – Suppose there are two greater sqrt(n) number then their product should also divide n but which will exceed n, which contradicts our assumption. So there can NOT be more than 1 prime factor of n greater than sqrt(n).

Let’s see the following step to perform such operation.

Example – Python program to print prime factors

Output:

2
2
2
5
5

Explanation –

In the above code, we have imported the math module. The prime_factor() function is responsible for printing the composite number. First, we get the even numbers; after this, all remaining prime factors must be odd. In for loop, the num must be odd, so we incremented i by two. A for loop will run the square root of n times.

Let’s understand the following property of composite numbers.

Every composite number has at least one prime factor less than or equal to the square root.

The program will work as follows.

  • In the first step, find the least prime factor i.
  • The occurrence of i be will removed from n by repeatedly dividing n by i.
  • Repeat the above both steps for dividing n and i = i + 2. Both steps will be repetitive till n become either 1 or a prime number.

Let’s understand another example where we find the largest prime factor of a given number.

Example – 2 Python program to find the largest prime factor of a given number.

Output:

23

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/263521.html

(0)
上一篇 2022年5月30日
下一篇 2022年5月30日

相关推荐

发表回复

登录后才能评论