Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import math
- import random
- # Functions: difference(arrayin), findDifference(arrayin, differenceLevel), findTerm(difference, number), makeNewArray(arrayin, length), compare(array1, array2)
- def difference(arrayin):
- # Accept array and output differences
- if Working:
- print("difference(" + str(arrayin) + ")")
- arrayout = []
- for i in range(len(arrayin)-1):
- arrayout.append(arrayin[i+1]-arrayin[i])
- if Working:
- print("return: " + str(arrayout))
- return arrayout
- def findDifference(arrayin, differenceLevel):
- # Accept array and difference level
- # Return first number of resulting difference
- if Working:
- print("findDifference(" + str(arrayin) + ", " + str(differenceLevel) + ")")
- newDifference = arrayin
- for i in range(differenceLevel):
- newDifference = difference(newDifference)
- if Working:
- print("return: " + str(newDifference[0]))
- return newDifference[0]
- def findTerm(difference, number):
- # Accept difference and number and return term
- if Working:
- print("findTerm(" + str(difference) + ", " + str(number) + ")")
- if Working:
- print("return: " + str(number / math.factorial(difference)))
- return number / math.factorial(difference)
- def makeNewArray(arrayin, length):
- # Accept nth term in form of array (e.g 2x^2+3x+1 = [2, 3, 1]) and desired length of output and return sequence with given length
- if Working:
- print("makeNewArray(" + str(arrayin) + ", " + str(length) + ")")
- # 2x^2 + 3x + 1
- arrayout = []
- for j in range(1, length+1):
- add = 0
- for i in range(0, len(arrayin)):
- add += arrayin[i] * (j ** (len(arrayin)-i-1))
- arrayout.append(add)
- if Working:
- print("return: " + str(arrayout))
- return arrayout
- def compare(array1, array2):
- # Accept array1 and array2 and return array1-array2 as an array
- # This assumes both are equal length
- if Working:
- print("compare(" + str(array1) + ", " + str(array2) + ")")
- array3 = []
- for i in range(len(array1)):
- array3.append(array1[i]-array2[i])
- if Working:
- print("return: " + str(array3))
- return array3
- # -------------------------------------------------- #
- # Note: after 16 or so inputted numbers, Python's precision starts to fall apart
- # anything less than that might give you answers like 4.000000000232831 or -2.9999999331776053, just round those to 4 and -3 respectively.
- Working = True
- inp = [1, 18 ,22] # Input number sequence here
- output = [0] * len(inp)
- currentArray = inp
- for currentTerm in range(len(inp)-1, -1, -1):
- currentDifference = findDifference(currentArray, currentTerm)
- term = findTerm(currentTerm, currentDifference)
- output[len(inp)-currentTerm-1] = term
- currentArray = makeNewArray(output, len(inp))
- currentArray = compare(inp, currentArray)
- if Working:
- verification = makeNewArray(output, len(inp))
- print("##########\nRaw answer array:")
- print(output)
- print("##########\nInput sequence:")
- print(inp)
- print("#########\nOutput sequence:")
- print(verification)
- print("##########\nnth term:")
- for i in range(len(output)):
- print(str(output[i]) + "*n^" + str(len(inp)-(i+1)) + " + ", end="")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement