Last active
April 3, 2022 10:32
-
-
Save acheong08/27148868d9245cbe8bce5602592a9b62 to your computer and use it in GitHub Desktop.
Sequences and series calculator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def Quadratic(): | |
option = int(input("Options: 1) Enter formula 2) Enter three terms")) | |
if option == 2: | |
terms = [None, None, None, None] | |
for i in range(1,4): | |
terms[i] = float(input(str("Enter term " + str(i) + ": "))) | |
diff1 = terms[2] - terms[1] | |
diff2 = terms[3] - terms[2] | |
quadiff = diff2 - diff1 | |
a = quadiff / 2 | |
quadiff2 = [None, None, None, None] | |
for i in range(1,4): | |
quadiff2[i] = terms[i] - (a*(i**2)) | |
nthdiff = quadiff2[2] - quadiff2[1] | |
zeroterm = quadiff2[1] - nthdiff | |
elif option == 1: | |
print("an^2 + bn + c") | |
a = float(input("Enter a: ")) | |
nthdiff = float(input("Enter b: ")) | |
zeroterm = float(input("Enter c: ")) | |
else: | |
print("Error") | |
exit() | |
nthTerm = str(str(a) + "n^2 " + "+ " + str(nthdiff) + "n" + " + " + str(zeroterm)) | |
print(nthTerm) | |
const1 = a | |
const2 = nthdiff | |
const3 = zeroterm | |
option = int(input("Options: 1) Show 10 terms 2) Calculate nth term 3) Find term number ")) | |
if option == 1: | |
for i in range(1,11): | |
print("Term " + str(i) + ": " + str((const1 * (i ** 2)) + (const2 * i) + (const3))) | |
elif option == 2: | |
nth = int(input("n:")) | |
answer = (const1 * (nth ** 2)) + (const2 * nth) + (const3) | |
print(answer) | |
elif option == 3: | |
value = float(input("Term value: ")) | |
nth = 0 | |
answer = None | |
while answer != value: | |
nth += 1 | |
answer = (const1 * (nth ** 2)) + (const2 * nth) + (const3) | |
if answer > value: | |
print("Error") | |
elif answer == value: | |
print("n: " + str(nth)) | |
else: | |
print("???") | |
def Geometric(): | |
option = int(input("1) Enter Formula 2) Enter first two terms : ")) | |
if option == 1: | |
a = float(input("Enter first number of sequence: ")) | |
r = float(input("Enter ratio: ")) | |
elif option == 2: | |
a = float(input("Enter first number of sequence: ")) | |
a2 = float(input("Enter second number of sequence: ")) | |
r = a2/a | |
else: | |
print("Error") | |
exit() | |
option = int(input("1) Show 10 terms 2) Calculate nth term 3) Find term number : ")) | |
print("Formula: " + str(a) + "*" + str(r) + "^(n-1)") | |
if option == 1: | |
for n in range(1,11): | |
value = a * (r**(n-1)) | |
print("Term " + str(n) + ": " + str(value)) | |
elif option == 2: | |
n = int(input("Enter term number: ")) | |
value = a * (r**(n-1)) | |
elif option == 3: | |
userVal = float(input("Enter term value: ")) | |
n = 0 | |
value = None | |
while value != userVal and value < userVal: | |
n += 1 | |
value = a * (r**(n-1)) | |
if value == userVal: | |
print("Nth term: " + str(n)) | |
else: | |
print("Error") | |
option = int(input("1) Quadratic 2) Geometric : ")) | |
if option == 1: | |
Quadratic() | |
elif option == 2: | |
Geometric() | |
else: | |
print("Error") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment