Python checking whether two numbers are mutually factors
In mathematics, if two numbers are mutually factors, it means they can divide each other, i.e., one number divides the other with no remainder. We can write a simple Python program to check whether two numbers are mutually factors.
Example
def
are_mutual_factors
(
a
,
b
)
:
if
a % b
==
0
and
b % a
==
0
:
return
True
else
:
return
False
Testing
num1
=
12
num2
=
6
result
=
are_mutual_factors
(
num1
,
num2
)
(
f
"{num1} and {num2} are mutually factors: {result}"
)
Code analysis:
are_mutual_factors(a, b)is a function used to check whether two numbersaandbare mutually factors.a % b == 0Checksawhether it is divisible byb.b % a == 0Checksbwhether it is divisible bya.- If both conditions are met, the function returns
True, indicating the two numbers are mutually factors; otherwise it returnsFalse。 - In the testing section, we defined two numbers
num1andnum2and call theare_mutual_factorsfunction to check whether they are mutually factors, and finally print the result.
Output result:
12 和 6 是否互为因数: True
Python3 Examples