Member-only story
Interview question: Use Python to determine the factors of a number
If a person wants to become a Python developer, he needs to know the solutions to a variety of problems because he may be asked questions about them at a coding interview. One such question would be to find the factors of a number.
A factor is an integer that divides exactly into a whole number without a remainder, making it a whole number. In other words, a factor is a number that divides another number evenly, with no numbers left over in the division process.
The factors of a number can be found by systematically dividing the number of each counting number, starting with one.
The psuedocode for finding the factors of a number is:-
- Define the number that needs to be factored as num.
- Define an empty list that will hold the factors as factor_lst.
- Define a function, find_factors, which will find the factors of an input number, x.
- In the function, create a for loop that will iterate from 1 to x+1, if x modulus i equals zero then it is a factor and will be appended to the factor_lst.
- When the for loop completes its iterations, the function will return the list, factor_lst.