5. In manufacturing electronic digital computers, regardless of what kind of positional notation is used, each digit requires a device. If using $n$-ary notation, each position has $n$ digits, and to represent an $m$-digit $n$-ary number in the machine, $m \times n$ devices are required. Let the maximum number the machine can represent be $M$ (using the number system of base $n$, $n \geqslant 2$) when the number of devices is $G$.
(1) If the machine is to represent an $m$-digit $n$-ary number, write the relationship between $M$, $m$, and $n$;
(2) Consider $G$ as a function of $n$, with $M$ as a constant, and write this functional relationship;
(3) Calculate $G(2)$, $G(3)$, $G(4)$, and $G(5)$ in terms of how many $\lg (M+1)$ each is;
(4) When $M$ is given, for what value of $n$ will the number of devices $G$ be minimized?
5. (1) $M=(n-1) n^{m-1}+(n-1) n^{m-2}+\cdots+(n-1) n^{1}+(n-1) n^{0}=n^{m}-1$;
(2) $n^{m}=M+1$, so $m=\frac{\lg (M+1)}{\lg n}$, then $G(n)=m n=\frac{n}{\lg n} \cdot \lg (M+1)(n \geqslant 2, n \in \mathbf{N})$;
(3) $G(2) \approx 6.67 \lg (M+1), G(3) \approx 6.25 \lg (M+1), G(4) \approx 6.67 \lg (M+1), G(5) \approx$
7.14 \lg (M+1)$;
(4) when $n=3$, $G$ is the smallest (can be proven by mathematical induction).