Python: The Factors
Python: Factors of the Number N
This is a short task to get the factors of a given number. The Definition of Factors of N is: The pairs of numbers you multiply to get the N number.
For instance, factors of 15 are 3 and 5, because 3×5 = 15. Some numbers have more than one factorization (more than one way of being factored). For instance, 12 can be factored as 1×12, 2×6, or 3×4
In this task we will write a Python code to ask the user for a number N then will get all the pairs number that if we multiply them will get that N number, we will store the pairs in a array ‘factors’.
The Code:
def factors_of_n(num):
a=1
factors=[]
while a <= num:
if num%a==0:
if (num/a,a) not in factors:
factors.append((a,int(num/a)))
a = a + 1
return factors
#Ask the user for a number
num=int(input(“Enter a number: “))
print(factors_of_n(num))