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())