连分式
连分式(continued fraction)由和与倒数的多层嵌套构成,可以是有限的,也可以是无限的。
表达式:或
import mathdef fraction_to_continued_fraction(numerator, denominator, max_terms):"""计算一个分数的连分式表示。参数:numerator (int): 分数的分子。denominator (int): 分数的分母。max_terms (int): 连分式表示的最大项数。返回:list: 连分式的部分分母组成的列表。"""coefficients = []current_numerator = numeratorcurrent_denominator = denominatorwhile current_denominator != 0 and len(coefficients) < max_terms:quotient = current_numerator // current_denominatorcoefficients.append(quotient)remainder = current_numerator % current_denominatorcurrent_numerator, current_denominator = current_denominator, remainderreturn coefficientsdef continued_fraction_to_decimal(coefficients):"""将连分式转换为十进制表示的数字。参数:coefficients (list): 连分式的部分分母组成的列表。返回:float: 连分式对应的十进制数。"""if not coefficients:return 0.0decimal_value = coefficients[-1]for coefficient in reversed(coefficients[:-1]):decimal_value = 1 / decimal_value + coefficientreturn decimal_valuedef real_number_to_continued_fraction(x, error_tolerance, max_terms):"""计算一个实数的连分式表示,考虑误差容忍度和最大项数。参数:x (float): 要表示的实数。error_tolerance (float): 允许的误差范围。max_terms (int): 连分式表示的最大项数。返回:list: 连分式的部分分母组成的列表。"""coefficients = []integer_part = int(x)fractional_part = x - integer_partcoefficients.append(integer_part)while fractional_part > error_tolerance and len(coefficients) < max_terms:reciprocal = 1 / fractional_partnext_term = int(reciprocal)coefficients.append(next_term)fractional_part = reciprocal - next_termcurrent_decimal = continued_fraction_to_decimal(coefficients)if abs(current_decimal - x) < error_tolerance:breakreturn coefficientsif __name__ == '__main__':fraction_test = fraction_to_continued_fraction(105, 33, 10)print('该测试分数的连分式表示:', fraction_test)print('测试分数的分布式转十进制数为:', continued_fraction_to_decimal(fraction_test))# 计算小数3.1415926的连分式表示pi_value = 3.1415926 # 近似π值error_tolerance = 1e-5 # 误差容忍度max_terms = 10 # 连分式的最大项数pi_fraction_real = real_number_to_continued_fraction(pi_value, error_tolerance, max_terms)print("小数3.1415926的连分式表示:", pi_fraction_real)
该测试分数的连分式表示: [3, 5, 2]
测试分数的分布式转十进制数为: 3.1818181818181817
小数3.1415926的连分式表示: [3, 7, 15, 1]
无穷连根式
表达式:
import mathdef infinite_nested_radical_recursive(a, n):"""递归计算无穷连根式的近似值。参数:a (float): 连根式中的常数。n (int): 递归深度,用于近似计算。返回:float: 无穷连根式的近似值。"""if n == 1:return math.sqrt(a)return math.sqrt(a + infinite_nested_radical_recursive(a, n - 1))# 示例:计算 a = 2 的无穷连根式近似值
a = 2
recursion_depth = 10
approximation_recursive = infinite_nested_radical_recursive(a, recursion_depth)
print(f"a = {a} 的无穷连根式近似值 (递归深度 {recursion_depth}): {approximation_recursive}")
a = 2 的无穷连根式近似值 (递归深度 10): 1.9999976469034038
平方根
可以使用巴比伦算法求数字的平方根。
假设数字为,步骤如下:
- 猜测,作为的猜测值
- 计算
- 将和的平均值当作新的猜测值
- 重复步骤2和3,直到足够小
import mathdef babylonian_sqrt(value, initial_guess, tolerance):"""使用巴比伦算法计算平方根的近似值。参数:value (float): 要求平方根的数。initial_guess (float): 初始猜测值。tolerance (float): 误差容忍度。返回:float: 近似的平方根值。"""current_error = tolerance * 2 # 初始设置一个较大的误差值guess = initial_guesswhile current_error > tolerance:quotient = value / guessnew_guess = (guess + quotient) / 2current_error = abs(new_guess**2 - value)guess = new_guessreturn guess# 测试平方根计算
value_to_sqrt = 5
initial_guess = 1
tolerance = 1e-15approx_sqrt = babylonian_sqrt(value_to_sqrt, initial_guess, tolerance)
print(f"使用巴比伦算法计算的 {value_to_sqrt} 的平方根近似值: {approx_sqrt}")
print(f"math.sqrt({value_to_sqrt}) 的结果: {math.sqrt(value_to_sqrt)}")
使用巴比伦算法计算的 5 的平方根近似值: 2.23606797749979
math.sqrt(5) 的结果: 2.23606797749979