📅  最后修改于: 2023-12-03 14:49:52.313000             🧑  作者: Mango
在数学中,我们经常需要将不同分母的分数相加或相减,这需要进行通分操作。在编程中,我们同样需要解决这个问题,下面介绍几种实现的方法。
def gcd(a, b):
if b == 0:
return a
return gcd(b, a % b)
def lcm(a, b):
return a * b // gcd(a, b)
def add_frac(frac1, frac2):
lcm_num = lcm(frac1[1], frac2[1])
frac1[0] *= lcm_num // frac1[1]
frac1[1] = lcm_num
frac2[0] *= lcm_num // frac2[1]
frac2[1] = lcm_num
res_num = frac1[0] + frac2[0]
res_den = lcm_num
GCD = gcd(res_num, res_den)
return [res_num // GCD, res_den // GCD]
def sub_frac(frac1, frac2):
lcm_num = lcm(frac1[1], frac2[1])
frac1[0] *= lcm_num // frac1[1]
frac1[1] = lcm_num
frac2[0] *= lcm_num // frac2[1]
frac2[1] = lcm_num
res_num = frac1[0] - frac2[0]
res_den = lcm_num
GCD = gcd(res_num, res_den)
return [res_num // GCD, res_den // GCD]
frac1 = [1, 4]
frac2 = [3, 8]
print(add_frac(frac1, frac2)) # 输出 [5, 8]
print(sub_frac(frac1, frac2)) # 输出 [1, 8]
def get_factors(n):
i = 2
factors = []
while i * i <= n:
if n % i == 0:
factors.append(i)
n //= i
else:
i += 1
if n > 1:
factors.append(n)
return factors
def get_gcd(factors1, factors2):
i, j = 0, 0
res = 1
while i < len(factors1) and j < len(factors2):
if factors1[i] == factors2[j]:
res *= factors1[i]
i += 1
j += 1
elif factors1[i] < factors2[j]:
i += 1
else:
j += 1
return res
def add_frac(frac1, frac2):
factors1, factors2 = get_factors(frac1[1]), get_factors(frac2[1])
lcm_num = get_gcd(factors1, factors2)
frac1[0] *= lcm_num // frac1[1]
frac1[1] = lcm_num
frac2[0] *= lcm_num // frac2[1]
frac2[1] = lcm_num
res_num = frac1[0] + frac2[0]
GCD = get_gcd(get_factors(res_num), factors1 + factors2)
return [res_num // GCD, lcm_num // GCD]
def sub_frac(frac1, frac2):
factors1, factors2 = get_factors(frac1[1]), get_factors(frac2[1])
lcm_num = get_gcd(factors1, factors2)
frac1[0] *= lcm_num // frac1[1]
frac1[1] = lcm_num
frac2[0] *= lcm_num // frac2[1]
frac2[1] = lcm_num
res_num = frac1[0] - frac2[0]
GCD = get_gcd(get_factors(res_num), factors1 + factors2)
return [res_num // GCD, lcm_num // GCD]
frac1 = [1, 4]
frac2 = [3, 8]
print(add_frac(frac1, frac2)) # 输出 [5, 8]
print(sub_frac(frac1, frac2)) # 输出 [1, 8]
def add_frac(frac1, frac2):
lcm_num = frac1[1] * frac2[1] // gcd(frac1[1], frac2[1])
frac1[0] *= lcm_num // frac1[1]
frac1[1] = lcm_num
frac2[0] *= lcm_num // frac2[1]
frac2[1] = lcm_num
res_num = frac1[0] + frac2[0]
GCD = gcd(res_num, lcm_num)
return [res_num // GCD, lcm_num // GCD]
def sub_frac(frac1, frac2):
lcm_num = frac1[1] * frac2[1] // gcd(frac1[1], frac2[1])
frac1[0] *= lcm_num // frac1[1]
frac1[1] = lcm_num
frac2[0] *= lcm_num // frac2[1]
frac2[1] = lcm_num
res_num = frac1[0] - frac2[0]
GCD = gcd(res_num, lcm_num)
return [res_num // GCD, lcm_num // GCD]
frac1 = [1, 4]
frac2 = [3, 8]
print(add_frac(frac1, frac2)) # 输出 [5, 8]
print(sub_frac(frac1, frac2)) # 输出 [1, 8]
在实际编码中,第三种方法是最简洁的。