%==================================================================
%=====                The "MEMORY" Algorithm                  =====
%==================================================================
%===== This Finite State Machine (FSM) Internally "Remembers" =====
%===== A Sequence of Three Binary Digits. As We Key In Each   =====
%===== New Binary Digit, It Spews Forth The One Keyed Three   =====
%===== Strokes Before. Larger FSMs With More States Will      =====
%===== "Remember" Even Longer Strings.                        =====
%==================================================================

function FiniteStateMachineII()

clear
clc

Input =  input('Input Value To Finite State Machine (0 or 1, 9 for Exit): ');
StateBitI = Input;
StateBitII = 0;
StateBitIII = 0;

while (Input ~= 9)
    Input =  input('Input Value To Finite State Machine (0 or 1, 9 for Exit): ');
    if ((StateBitI == 0) && (StateBitII == 0) && (StateBitIII == 0)) %State = 000
                Output = StateBitIII;
        if (Input == 0)
            StateBitI = 0;
            StateBitII = 0;
            StateBitIII = 0;
        elseif (Input == 1)
            StateBitI = 1;
            StateBitII = 0;
            StateBitIII = 0;
        end
    elseif ((StateBitI == 0) && (StateBitII == 0) && (StateBitIII == 1)) %State = 001
                Output = StateBitIII;
        if (Input == 0)
            StateBitI = 0;
            StateBitII = 0;
            StateBitIII = 0;
        elseif (Input == 1)
            StateBitI = 1;
            StateBitII = 0;
            StateBitIII = 0;
        end        
    elseif ((StateBitI == 0) && (StateBitII == 1) && (StateBitIII == 0)) %State = 010
                Output = StateBitIII;
        if (Input == 0)
            StateBitI = 0;
            StateBitII = 0;
            StateBitIII = 1;
        elseif (Input == 1)
            StateBitI = 1;
            StateBitII = 0;
            StateBitIII = 1;
        end
    elseif ((StateBitI == 0) && (StateBitII == 1) && (StateBitIII == 1)) %State = 011
                Output = StateBitIII;
        if (Input == 0)
            StateBitI = 0;
            StateBitII = 0;
            StateBitIII = 1;
        elseif (Input == 1)
            StateBitI = 1;
            StateBitII = 0;
            StateBitIII = 1;
        end
    elseif ((StateBitI == 1) && (StateBitII == 0) && (StateBitIII == 0)) %State = 100
                Output = StateBitIII;
        if (Input == 0)
            StateBitI = 0;
            StateBitII = 1;
            StateBitIII = 0;
        elseif (Input == 1)
            StateBitI = 1;
            StateBitII = 1;
            StateBitIII = 0;
        end
    elseif ((StateBitI == 1) && (StateBitII == 0) && (StateBitIII == 1)) %State = 101
                Output = StateBitIII;
        if (Input == 0)
            StateBitI = 0;
            StateBitII = 1;
            StateBitIII = 0;
        elseif (Input == 1)
            StateBitI = 1;
            StateBitII = 1;
            StateBitIII = 0;
        end
    elseif ((StateBitI == 1) && (StateBitII == 1) && (StateBitIII == 0)) %State = 110
                Output = StateBitIII;
        if (Input == 0)
            StateBitI = 0;
            StateBitII = 1;
            StateBitIII = 1;
        elseif (Input == 1)
            StateBitI = 1;
            StateBitII = 1;
            StateBitIII = 1;
        end    
    elseif ((StateBitI == 1) && (StateBitII == 1) && (StateBitIII == 1)) %State = 111
                Output = StateBitIII;
        if (Input == 0)
            StateBitI = 0;
            StateBitII = 1;
            StateBitIII = 1;
        elseif (Input == 1)
            StateBitI = 1;
            StateBitII = 1;
            StateBitIII = 1;
        end        
    end
    display(Output);
end
end