1 Star 0 Fork 0

wdc/Python001

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
QuadraticCalc.py 1.24 KB
一键复制 编辑 原始数据 按行查看 历史
# GGearing
# 02/10/2017
# Simple script to calculate the quadratic formula of a sequence of numbers and
# recognises when the sequence isn't quadratic
def findLinear(numbers): # find a & b of linear sequence
a = numbers[1] - numbers[0]
a1 = numbers[2] - numbers[1]
if a1 == a:
b = numbers[0] - a
return (a, b)
else:
print("Sequence is not linear")
sequence = []
first_difference = []
second_difference = []
for i in range(4): # input
term = str(i + 1)
inp = int(input("Enter term " + term + ": "))
sequence.append(inp)
for i in range(3):
gradient = sequence[i + 1] - sequence[i]
first_difference.append(gradient)
for i in range(2):
gradient = first_difference[i + 1] - first_difference[i]
second_difference.append(gradient)
if second_difference[0] == second_difference[1]: # checks to see if consistent
a = second_difference[0] / 2
subs_diff = []
for i in range(4):
n = i + 1
num = a * (n * n)
subs_diff.append((sequence[i]) - num)
b, c = findLinear(subs_diff)
print("Nth term: " + str(a) + "n^2 + " +
str(b) + "n + " + str(c)) # outputs nth term
else:
print("Sequence is not quadratic")
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/ijiwei/Python001.git
git@gitee.com:ijiwei/Python001.git
ijiwei
Python001
Python001
master

搜索帮助