正如标题所说,我需要在不使用 Python 3.5 中的数学模块的情况下找到泰勒级数以及编写除法测试程序。我会在一张纸上做数学,但实际上我在学校从来没有教过这些东西,所以老实说,我完全不知道如何在纸上做这些事情。我已经完成了相当多的工作,但我需要一些帮助才能完成它。我有一个朋友教我有关用于编程的算法,所以我现在对此有所了解。请参阅我遇到的主要编程问题的非常长的评论。
对于泰勒级数:在计算/sin x/cos x 的值时,您的程序应该继续添加幂级数的项,直到它达到一个绝对值小于乘
以前项总和的绝对值的项。
公式:
如果 n 是正整数:n! = nx( n-1) x (n-2) x…x 3 x 2 x 1
对于 Lewis Carrol 整除性检验:
只要数字多于一位,就可以通过删除个位并从结果数字中减去该数字来缩短它。
当且仅当最终数字等于 0 时,原始数字可以被 11 整除
代码:
#I don't know why, but for some reason n is automatically set to 5 once it is called in the recursion function for sin and cos x.
#This causes the answers to for sin and cos x to be drastically incorrect.
x = float(input("Please input a real number: "))
def factorial(n):
res = 1
for i in range(1, n+1):
res *= i
return res
def question1():
ex = 1 + x
for n in range(2,15):
ex += (x**n)/factorial(n)
print("e^{} = {}".format(x, ex))
question1()
def question2():
cosx = 0
for n in range(0, 15):
cosx += ((-1)**n)/factorial(2*n)*(x**(2*n+1))
print("cos {} = {}".format(x, cosx))
question2()
def question3():
sinx = 0
for n in range(0, 15):
sinx += ((-1)**n)/factorial(2*n+1)*(x**(2*n+1))
print("Sin {} = {}".format(x, sinx))
question3()
##def question4():
## while x > 9:
## unit = x % 10
## new_x = x // 10
## print(new_x)
## if new_x == 0:
## break
##question4()
样本输出:
Please input a real number: 10
e^10.0 = 20188.170595424563
cos 10.0 = -10962.271813947433
Sin 10.0 = -1083.6650211561773
1.0
1.0
1.0
1.0
...