Advertisement
linesguy

all sequence solver

Feb 19th, 2020
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.27 KB | None | 0 0
  1. import math
  2. import random
  3.  
  4. # Functions: difference(arrayin), findDifference(arrayin, differenceLevel), findTerm(difference, number), makeNewArray(arrayin, length), compare(array1, array2)
  5.  
  6. def difference(arrayin):
  7.     # Accept array and output differences
  8.     if Working:
  9.         print("difference(" + str(arrayin) + ")")
  10.     arrayout = []
  11.     for i in range(len(arrayin)-1):
  12.         arrayout.append(arrayin[i+1]-arrayin[i])
  13.     if Working:
  14.         print("return: " + str(arrayout))
  15.     return arrayout
  16.  
  17. def findDifference(arrayin, differenceLevel):
  18.     # Accept array and difference level
  19.     # Return first number of resulting difference
  20.     if Working:
  21.         print("findDifference(" + str(arrayin) + ", " + str(differenceLevel) + ")")
  22.     newDifference = arrayin
  23.     for i in range(differenceLevel):
  24.         newDifference = difference(newDifference)
  25.     if Working:
  26.         print("return: " + str(newDifference[0]))
  27.     return newDifference[0]
  28.  
  29.  
  30. def findTerm(difference, number):
  31.     # Accept difference and number and return term
  32.     if Working:
  33.         print("findTerm(" + str(difference) + ", " + str(number) + ")")
  34.     if Working:
  35.         print("return: " + str(number / math.factorial(difference)))
  36.     return number / math.factorial(difference)
  37.  
  38. def makeNewArray(arrayin, length):
  39.     # 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
  40.     if Working:
  41.         print("makeNewArray(" + str(arrayin) + ", " + str(length) + ")")
  42.     # 2x^2 + 3x + 1
  43.     arrayout = []
  44.     for j in range(1, length+1):
  45.         add = 0
  46.         for i in range(0, len(arrayin)):
  47.             add += arrayin[i] * (j ** (len(arrayin)-i-1))
  48.         arrayout.append(add)
  49.     if Working:
  50.         print("return: " + str(arrayout))
  51.     return arrayout
  52.  
  53. def compare(array1, array2):
  54.     # Accept array1 and array2 and return array1-array2 as an array
  55.     # This assumes both are equal length
  56.     if Working:
  57.         print("compare(" + str(array1) + ", " + str(array2) + ")")
  58.     array3 = []
  59.     for i in range(len(array1)):
  60.         array3.append(array1[i]-array2[i])
  61.     if Working:
  62.         print("return: " + str(array3))
  63.     return array3
  64.  
  65. # -------------------------------------------------- #
  66. # Note: after 16 or so inputted numbers, Python's precision starts to fall apart
  67. # anything less than that might give you answers like 4.000000000232831 or -2.9999999331776053, just round those to 4 and -3 respectively.
  68.  
  69. Working = True
  70. inp = [1, 18 ,22] # Input number sequence here
  71. output = [0] * len(inp)
  72. currentArray = inp
  73. for currentTerm in range(len(inp)-1, -1, -1):
  74.     currentDifference = findDifference(currentArray, currentTerm)
  75.     term = findTerm(currentTerm, currentDifference)
  76.     output[len(inp)-currentTerm-1] = term
  77.     currentArray = makeNewArray(output, len(inp))
  78.     currentArray = compare(inp, currentArray)
  79.  
  80. if Working:
  81.     verification = makeNewArray(output, len(inp))
  82.     print("##########\nRaw answer array:")
  83.     print(output)
  84.     print("##########\nInput sequence:")
  85.     print(inp)
  86.     print("#########\nOutput sequence:")
  87.     print(verification)
  88. print("##########\nnth term:")
  89. for i in range(len(output)):
  90.     print(str(output[i]) + "*n^" + str(len(inp)-(i+1)) + " + ", end="")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement