Prime factors of a number in Python

Prime factors of a number in python : The prime factor of given number is the factor of number. The factor of the number will multiply to get the other number as finding which number after multiplying get original number.
The simple definition of prime number is the number which is multiplied to the given original number.

  1. We will explain by the example below as the number is said to be prime number if divisible by number exactly.
  2. Then the next property is if we keep dividing the number by prime factor then we fully divide the number or produce another prime number.

Some interesting fact about the prime factor:-

  1. They are unique set of the prime factors for any numbers.
  2. To maintain the property of unique factorization and necessary that the number is 1 as neither prime nor composite.
  3. The prime factorization can help the divisibility, simplifying and finding the denominator for fractions.
  4. The prime factorization is important to people who try to make/break codes based on numbers.

Prime Factorization Example:-

Finding the prime factor of 16 from 2 then we need to keep dividing and dividend will be 1, 2 is only prime factor.
If we need to find the factor of 15 then we divide it by 2 and if not divisible we move to the next number 3 and then divide by 15 also provide the prime number 5 and s on.
Steps:-

  1. Take the value of integer and store in the variable.
  2. Use while loop then obtain the factors of the number are used by the modulus opearator.Then check if remainder of number is divisible by I is 0.
  3. Then the factor of integer is checked whether it is prime or not.
  4. They have two factors and called as prime numbers.
  5. Then use another while loop within the previous one and compute factors as prime or not.
  6. Then exit program.

Python program for prime number

def primeFactors (n):
while n%2==0:
print 2,
n=n/2
for i in range (3, int (math.sqrt (n)) +1, 2):
while n%i==0:
print i,
n=n/i
if n>2:
print n
n=315
primeFactors (n)
Output:-
3,3,5,7

Working:-

  1. Steps 1 and 2 take care of composite numbers and in third step of prime numbers.
  2. Step 1 will take the even number and then in step 1 all prime factors must be odd and explain I is then incremented by 2.
  3. Then the part is loop that runs till the square root of n not till n and then prove the optimization work.
  4. The composite number us prime factor and less than and equal to square root of itself.
  5. Let a and b have two factors of n and has,
  6. a*b=n
  7. Then find least prime number as factor i.

Find the least prime factor
Remove the occurrences I from the n by dividing n by i.
Then we have to repeat the steps as a, b and divided n by i=i+2.

Additional Services : Refurbished Laptops Sales, Python Classes, Share Market Classes And SEO Freelancer in Pune, India