[ description ]
This script returns the maximum multipliers incognitas and supports N numbers of variables.
Examples: a * x = s1 ... a * x + b * y = s2 ... a * x + b * y + c * z = s3 ... N number of sum of multipliers.
It's known that big Z values makes the perfomance decrease but the main objective is to show a
PoC of the transforming of a
Finite
State
Automata through
REgex
[ explanation ]
Transforming
FSA into
RE-
by Model Engineering College
The idea behind the construction is the following: (assuming states are enumerated) we will give an algorithm by which we can calculate a regular expression Ti,j(k) which describes all the strings which can take us from state i in the FSA to state j via states 1, 2, ... k. The language described by an FSA with n states, and where the start state is named 1 is then the union of all strings which take us from state 1 to a final state f through any of the states: the sum of all regular expressions T1,f(n) where f ranges over the final states.
The algorithm to calculate Ti,j(k) is stated inductively over variable k:
Base case: k=0 . We subdivide this into two cases, when i is the same as j and when they are not:
· Ti,i(0) =1+a+...+z where a to z are the labels on transition arcs going from state i to itself. Note that if no such arcs exist, the expression becomes simply 1.
· Ti,j(0) =a+...+z where a to z are the labels on transition arcs going from state i to state j (which are not equal). Note that if no such arcs exist, the expression becomes simply 0.
Inductive case: When the third parameter (k) is larger than 0, we use the following equation to calculate the desired result:
Ti,j(k+1) = Ti,j(k) + Ti,k+1(k)(Tk+1,k+1(k))*Tk+1,j(k)
_________
[ examples ]
example 1:
Code:
; this example: 7 * answer1 + 9 * answer2 = 53
aers =
(
7 9 53
)
#include aers.ahk
example 2:Code:
; this example: 11 * answer1 + 3 * answer2 + 7 * answer3 = 16435
aers =
(
11 3 7 16435
)
#include aers.ahk
aers.ahkCode:
/*
AERS : Algebraic Equation ReGex Solver
by k3ph - Licensed under BSD [ http://autohotkey.net/~k3ph/license.txt ]
version: prototype 01
*/
loop, parse, aers, %A_Space%`n`;`t`,`r[](){}<> ; [vxe]'s parse
{
if (A_Loopfield is NUMBER)
{
n++
n%n% = %A_LoopField%
if (n = 1)
p_b := "^(.*)\1{" . n1 - 1
if (n = 2)
p_m := "}(.*)\" . n . "{" A_LoopField - 1
if (n > 2)
p_m := p_m . "}(.*)\" . n . "{" A_LoopField - 1
}
}
result := n%n% ; get the result from last root
roots := n - 1
last_n := result - 1
loop % result + 1 ; convert answer to match
{
if a_index > 1
result := result . chr(48)
else
result := ""
}
stringlen, last_nl, last_n
last_nl := 8 + last_nl
StringTrimRight, p_m, p_m, %last_nl% ; remove result from regex
p_e := "}$"
needle := p_b . p_m . p_e ; make needle
answers := RegExMatch(result, needle, match) ; the regex
if (aers != "") ; check if the script was feeded
loop %roots% ; check how many answers
{
if (answer = "") ; if null, answer is zero
answer = 0
answer := StrLen(match%A_Index%)
msgbox %answer% ; display the answers. if there are multiple possible solutions, the one with the highest value of x will be returned since x is handled earlier in the regex.
}
[ keywords ]
algebra, regex, aers