2024/5/11

所花时间:1小时

代码行:70行

博客量:1篇

了解到的知识点:

def prime_factors(n):

    factors = []  # 存储质因子的列表

    divisor = 2  # 从最小的质数2开始除

    while n > 1:

        if n % divisor == 0:

            factors.append(str(divisor))  # 将质因子加入到列表中

            n //= divisor

        else:

            divisor += 1

    return factors

 

# 读取输入的整数

x = int(input())

 

# 获取质因子展开式

factors = prime_factors(x)

 

# 输出质因子展开式

output = f"{x}={''.join(factors)}"

print(output)

def f(a, b, c, d, x):

    return a * x**3 + b * x**2 + c * x + d

 

def f_prime(a, b, c, x):

    return 3 * a * x**2 + 2 * b * x + c

 

def newton_method(a, b, c, d, x0, tolerance=1e-6, max_iterations=1000):

    x = x0

    for _ in range(max_iterations):

        fx = f(a, b, c, d, x)

        f_prime_x = f_prime(a, b, c, x)

        x_new = x - fx / f_prime_x

        if abs(x_new - x) < tolerance:

            return round(x_new, 2)

        x = x_new

    return None

 

# 读取输入的方程系数和实数x

a, b, c, d, x = map(float, input().split())

 

# 使用牛顿迭代法求实根

result = newton_method(a, b, c, d, x)

 

# 输出实根

print(result)

posted @ 2024-05-11 21:54  为20岁努力  阅读(26)  评论(0编辑  收藏  举报