the-honk/python/calculators/quadratic nth term.py
2021-08-26 02:42:17 +01:00

27 lines
519 B
Python

import operator
from _helpers import listInput
def diff(a):
return list(map(operator.sub, a[1:], a[:-1]))
def formatNumber(x):
if x % 1 == 0:
return int(x)
else:
return x
sequence = listInput('Please input a sequence of numbers')
row1 = diff(sequence)
row2 = diff(row1)
a = formatNumber(row2[0] / 2)
b = formatNumber(row1[0] - (3 * a))
c = formatNumber(sequence[0] - a - b)
print('''
a = {0}
b = {2}
c = {4}
Equation: {0}{1}{2}n{3}{4}
'''.format(a, '+' if b >= 0 else '', b, '+' if c >= 0 else '', c))