Formula finder
Finds formula for any sequence of numbers, and then displays the first 25 numbers in the sequence using the formula. Please comment
The algorithm used was actually invented by me during a boring maths lesson, so don't be to harsh when judging it!
Here is the code:
(requires IanM's string utility plugin)
sync on : cls 0 : sync : sync
input "Enter sequence separated by ','s: ",seq$
seq$ = seq$ + ","
global dim seq(-1) as double float
rem _______________________
rem | | /
rem =|=|===== PARSE INPUT =====|=|=
rem / |_______________________|
l = len(seq$)
txt$ = ""
ID = -1
for i = 1 to l
char$ = mid$(seq$,i)
if char$ = ","
array insert at bottom seq(0)
inc ID
seq(ID) = val(txt$)
txt$ = ""
else
txt$ = txt$ + char$
endif
next i
TD = ID
global dim dif(ID) as double float
Power as integer
Stage as double float
Mult as double float
Mult2 as double float
Nth as double float
Nval as double float
Formula as string
Formula = ""
do
ID = TD
undim dif(0)
global dim dif(ID) as double float
for i = 0 to ID
dif(i) = seq(i)
next i
Stage = 0
while AllSame() = 0
dec ID
inc Stage
for i = 0 to ID
dif(i) = dif(i+1)-dif(i)
next i
array delete element dif(0),ID+1
endwhile
Power = Stage
Mult = dif(0)/Exc(Stage)
if Mult <> 0
if Mult < 0
Mult2 = 0-Mult
Formula = Formula + " - "
else
Mult2 = Mult
Formula = Formula + " + "
endif
Mult$ = str$(Mult2)
if Power = 0
Formula = Formula + Mult$
else
if Mult2 = 1
Mult$ = ""
endif
if Power = 1
Formula = Formula + Mult$ + "n"
else
Formula = Formula + Mult$ + "n^" + str$(Power)
endif
endif
endif
for i = 0 to TD
Nth = i+1
Nval = Mult*(Nth^Stage)
seq(i) = seq(i)-Nval
next i
if Power = 0
exit
endif
loop
if left$(Formula,3) = " - "
Formula = right$(Formula,len(Formula)-3)
Formula = "-"+Formula
else
Formula = right$(Formula,len(Formula)-3)
endif
print Formula
for i = 1 to 25
print str$(GetNum(i,Formula));
if i < 25
print ",";
endif
next i
sync : sync
wait key
end
function GetNum(i,Form as string)
Sum as double float
Mult as double float
Sum = 0
Form = Replace$(Form,"n","*"+str$(i))
Form = Replace$(Form," + ","+")
Form = Replace$(Form," - ","+-")
split string Form,"+"
C = split count()
dim Sect(C-1) as string
for i = 1 to C
Sect(i-1) = get split word$(i)
next i
for i = 1 to C
split string Sect(i-1),"*^"
C2 = split count()
if C2 = 1
Sum = Sum + val(get split word$(1))
else
Word$ = get split word$(1)
if Word$ = "-"
Mult = -1
else
Mult = val(Word$)
endif
if Mult = 0 then Mult = 1
if C2 = 2
Sum = Sum + (Mult*val(get split word$(2)))
else
Sum = Sum + (Mult*(val(get split word$(2))^val(get split word$(3))))
endif
endif
next i
endfunction Sum
function AllZero()
if seq(0) <> 0
exitfunction 0
endif
for i = 1 to array count(seq(0))
if seq(i) <> seq(i-1)
exitfunction 0
endif
next i
endfunction 1
function Exc(Num as integer)
Num2 as integer
Num2 = 1
for i = 2 to Num
Num2 = Num2*i
next i
Num3 as double float
Num3 = Num2
endfunction Num3
function AllSame()
for i = 1 to array count(dif(0))
if dif(i) <> dif(i-1)
exitfunction 0
endif
next i
endfunction 1
If you don't have it, here is the .exe:
edit:
I have found that due to floating point errors, it doesn't work so well with decimals. Whole numbers seem to work fine.
edit2:
Fixed negative sequence bug.