【无标题】

1.计算多项式求和(1+1/2+1/3+...+1/100)

def problem1():
    total = 0.0
    for i in range(1,100):
        total += 1/i
    return total
print("问题结果:",problem1())

2.计算多项式求和(1-1/2+1/3-1/4+......1/n)

def problem2():
    n=int(input("请输入n的值:"))
    total = 0.0
    for i in range(1,n+1):
        if i % 2 ==1:
            total += 1/i
        else:
            total -= 1/i
    return total
print("问题结果:",problem2())

3.计算多项式求和(1 + 1/(1+2) + 1/(1+2+3) + ...1/(1+2+...1/n)

def problem3():
    n = int(input("请输入n的值"))
    total = 0
    for i in range(1,n+1):
        triangular_num= i * (i+ 1)//2
        total += 1 / triangular_num
    return total
print("问题结果:",problem3()) 

计算a+aa+aaa+a...a前6项之和(a=2)计算a+aa+aaa+a...a前6项之和(a=2)​​​​​​​4.计算a+aa+aaa+a...a前6项之和(a=2)

def calculate_series(a=2,n=6):
    total = 0
    current_term = 0
    for i in range(1,n+1):
         current_term = current_term*10+a
         total += current_term
    return total
result = calculate_series()
print(f"a+aa+aaa+...前6项之和(a=2)为:{result}")

5:用泰勒级数计算e的近似值,直到最后一项小于1e-6为止,e=1+1/1!+ 1/2!+1/3!+..1/n!

def  problem5():
    tolerance = 1e-6
    e_approx = 1.0
    term = 1.0
    n = 1
    while term >= tolerance:
        term = 1.0
        factorial = 1
        for i in range(1,n+1):
            factorial*=i
        term = 1 / factorial
        e_approx += term
        n += 1
    return e_approx
print("问题的结果:",problem5())

6.计算Π的公式为:Π/4=1-1/3+1/5-1/7+...计算Π的近似值

def problem6():
    terms = int(input("请输入问题的项数:"))
    pi_over_4 = 0.0
    for i in range(terms):
        denominator = 2 * i + 1  #1,3,5,7,...
        if i % 2 == 0: #奇数项为正
            pi_over_4 += 1 / denominator
        else:
            pi_over_4 -= 1 / denominator
    return pi_over_4* 4
print("问题结果:", problem6())

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值