代码拉取完成,页面将自动刷新
# 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")
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。