Explicitly explained codes, and well written articles , a better place to learn programming

Prime Factorization

4 comments
Prime numbers are numbers  in which their factors are one and the number it self. for example, 5 is a prime number because factors of 5 are 1 and 5 itself. However , 6 is not prime number because its factors are not only one and six but also 2 and 3. Prime numbers  have wide range application in the science of cryptography,  the art of creating secret code, or unraveling secret code.

Prime Factorization is a a process of expressing a number using a multiple of prime numbers. for example, we can express 30 as 2x3x5. Here, we are able to express 30 as a multiple of prime numbers: 2,3,5. every number can be expressed as a product of prime numbers.

Here is the python code.

def factor(n):
    m = []
    x = 2
    while x <= n:

        while n%x==0:
            m.append(x)
            n = n/x

        x +=1
    print m
    m = [1]
    x = 2

                                            How it works

I created a function called factor, which accepts n as a parameter. you can change the name of the
function and the name of the variable to whatever you want, but it is advicable if you make it some thing pertaining to its purpose. Naming a function about factoriazation as protein sound weird,even if it is
possible.

Then I create to local variable m and x and define their value. 'm' is used to store all prime factors and x is used to store each number which ranges in [2,n] turn by turn.

following the variable declaration, we get in to while loop. what this loop basically does is, it checks
if the value of x is not greater than the number you want to calculate its prime factors.  the while loop
breaks (stops) if the value of x is greater than n. I did this because I know in my elementary school mathematics class that the prime factors of a number couldn't exceed the number. you can't have a prime factor 7 for the number 6.

the second while loops is used here in order to check if the remainder of n divided by x is 0.
I did this using the module (%) operators. This operator is an operator like that of addition(+),
multiplication (*) , division(/) and subtraction (-), But it is used to calculate the remainder
when two numbers are divided. for example,4%2 = 0 Because the remainder we get when we divide 4 by 2 is 0.
 Note: if you divide any number by its factor, then its remainder is 0.

 if the condition, n%x == 0, goes True, then the code \ in the while loop get executed.
 the first line in the while loop used to append x in the list m, which we defined earler. then the next line
 changes the value of n to n/x. this while loops break when n%x is different from 0. this means when
 x is no longer factor of n.This loop get exectued untill the value of x exceeds the value of n.
 
in the end, when all thing get its accomplishment, we print the value of m, which contains all prime factors, in a list.

Don't Forget to leave a comment.
 
  .



 


4 comments :