Find factors of a number.
One more step further into the numerical world with Python! This time, let’s learn about the factors of a number. Since We’ve covered factoring in my Allegra course, it’s the p[perfect time to re-learning about factoring from another angle with Python.
Okay, this is the third post in STEM with Python. Since Python is a great scripting language, it’s p[pretty much like an experimental language which I can enjoy toying with my numerical + mathematical creativity.
A factor of a number:
A factor is a number that divides another number, leaving no remainder. In other words, if multiplying two whole numbers gives us a product, then the numbers we are multiplying are factors of the product because they are divisible by the product. There are two methods of finding factors: multiplication and division.
splashlearn.com
Example:
Let’s take 12 and start dividing the number from 1 to 12.
12/1 = 12
12/2 = 6
12/3 = 4
12/4 = 3
12/5 = 2.4
12/6 = 2
12/7 = 1.7
…. continue it until 12…
And now you see, just as the splashlearn.com‘s description, a factor of a number is a number that divides another number, leaving no remainder. So, when it comes to 12, there are six of them: 1,2,3,4,6, and 12.
And here’s the coding solution to it.
Code:
Actual code:
x = int(input("enter a number: "))
print("factor of",x,"is: ")
for i in range(1, x+1):
if x%i==0:
print(i)
In the above code, what needs your attention is x+1 in the for loop condition. The +1 is necessary because without the expression it won’t reach the specified number. Let’s say you chose 12 – in this case, it will loop through only until 11. So, you need the +1.
for i in range(1, x+1):
Afterthoughts:
So how was that? Isn’t it exciting to learn and experiment with STEM in Python? I’ll keep posting about Python as well as my experimental activities revolving around the STEM-compatible language!
via: