File tree Expand file tree Collapse file tree 1 file changed +42
-0
lines changed Expand file tree Collapse file tree 1 file changed +42
-0
lines changed Original file line number Diff line number Diff line change
1
+ #GGearing
2
+ #02/10/2017
3
+ #Simple script to calculate the quadratic formula of a sequence of numbers and
4
+ #recognises when the sequence isn't quadratic
5
+
6
+ def findLinear (numbers ): ##find a & b of linear sequence
7
+ output = []
8
+ a = numbers [1 ]- numbers [0 ]
9
+ a1 = numbers [2 ]- numbers [1 ]
10
+ if a1 == a :
11
+ b = numbers [0 ]- a
12
+ return (a ,b )
13
+ else :
14
+ print ("Sequence is not linear" )
15
+
16
+ sequence = []
17
+ first_difference = []
18
+ second_difference = []
19
+ for i in range (4 ): #input
20
+ term = str (i + 1 )
21
+ inp = int (input ("Enter term " + term + ": " ))
22
+ sequence .append (inp )
23
+
24
+ for i in range (3 ):
25
+ gradient = sequence [i + 1 ]- sequence [i ]
26
+ first_difference .append (gradient )
27
+ for i in range (2 ):
28
+ gradient = first_difference [i + 1 ]- first_difference [i ]
29
+ second_difference .append (gradient )
30
+
31
+ if second_difference [0 ]== second_difference [1 ]: #checks to see if consistent
32
+ a = second_difference [0 ]/ 2
33
+ subs_diff = []
34
+ for i in range (4 ):
35
+ n = i + 1
36
+ num = a * (n * n )
37
+ subs_diff .append ((sequence [i ])- num )
38
+ b ,c = findLinear (subs_diff )
39
+ print ("Nth term: " + str (a )+ "n^2 + " + str (b )+ "n + " + str (c )) #outputs nth term
40
+ else :
41
+ print ("Sequence is not quadratic" )
42
+
You can’t perform that action at this time.
0 commit comments