The problem:
Professor Martinez needs a state machine that will recognize certain base-$3$ numbers. It should read the digits in left-to-right order. That is, if you’ve seen number $x$ and read a new digit $d$, your new number is $3x + d$. The machine should be in a final state whenever the number read so far is congruent to $3 \pmod 5$. For efficiency, the state machine must be deterministic. Specifically, if you look at any state $s$ and any action $\text{a}$, there is exactly one edge labelled $\text{a}$ leaving state $s$. Draw a state diagram that will meet his needs, using no more than $7$ states and, if you can, no more than $5$.
I already know the graphical solution to the problem, but I'm having trouble figuring out the logic behind it. How do I know if a base $3$ number is congruent to $3 \pmod 5$? So far, I have the following algorithm:
for every digit k in number n:
if(3k + d == 5k - 2) {
send to final state //is congruent to 3(mod 5)
}
else {
3k = 3k + d //updates the total value k
}
I'm not sure how to implement this in state diagram form, since any given number can have an arbitrarily large number of digits.
I don't know anything about finite state machines.
The modular part is likely clearest using $81 \equiv 1 \pmod 5.$ That means that you can take a base 3 number with digits (all 0,1,2) abcdefghijk and split it up as abc,defg,hijk (similar to the way we put commas for 1000 except four digits at a time). Then have a mod 5 evaluation for hijk, add that for defg, add that fr the three digit abc.
If you insist on going left to right you may be talking about Horner's rule for evaluating polynomials. Whatever step you are at, add the next digit, multiply by 3, reduce mod 5. Certainly that would work as an ordinary computer program.