Markov normal algorithm for dividing numbers
Students whom I have the opportunity to teach shout that it is difficult, but only until the first-hand made working algorithm, then it flows into very interesting algorithmic problems.
Actually, to the topic of this post: our task is to write a normal Markov algorithm for dividing two integers with an accuracy of 4 decimal places (we use a unary calculus to specify numbers). For example, input: | / ||||, output: 0.25.
Moreover, we have only one operation - replacing one substring in the original string with another. Who cares what it is and how it works - welcome to cat.
Normal Markov Algorithm
By the normal Markov algorithm we mean a certain ordered set of products (substitution of substrings). Products can be both ordinary (run as many times as possible) and final (run only 1 time and after them the algorithm ends). Products are performed starting from the first. If the first one cannot be completed, we do the second, etc. If, after any production, one of the previous ones can be fulfilled again, we do it. The work of the algorithm ends when there is no next product to execute and all the previous ones cannot be completed or after some final product is completed.
Actually solving the problem
List of substitutions: FIN after the substring to which we replace means that such products are final. Writing an emulator that allows you to emulate the operation of such an algorithm will not be difficult in any of the programming languages. As a result, to enter | / |||| by string conversions we get 0.25. Who does not believe - check. (We write down an input on a piece of paper, for example the same | / |||| and perform the above substitutions until the algorithm finishes its work (condition for completing the work, see even higher)) PS Here is such an elegant and unusual version of programming and removal brain. PPS
%* на *%
%| на %*
*| на **
|* на t
t* на *t
t% на %t
%t на %v|
t на |
%v на ?d
?d на d?
|d на d|
? на %
*d на h
h* на oh
h% наh
h на «пустая строка»
* на «пустая строка»
d на |_
/| на -k
k| на kk
k на |+
+| на |+
- на ey
|e на e|
y на %
eo на 0o
e на «пустая строка»
|_ на .a
a. на .a
.. на .
.aaaaaaaaaa на a,.
,a на a,
.aaaaaaaaa на 9
.aaaaaaaa на 8
.aaaaaaa на 7
.aaaaaa на 6
.aaaaa на 5
.aaaa на 4
.aaa на 3
.aa на 2
.a на 1
. на 0
, на «пустая строка»
a на .a
o на p||||||||||
|p на p|
pp на p
% на u
u+ на u
u на _
|+ на |)+
) на (>
>+ на +>
+ на {
{ на |
>>>>> на =
|= на =
(= на =
( на /
p= на =<
<0 на 0<
<1 на 1<
<2 на 2<
<3 на 3<
<4 на 4<
<5 на 5<
<6 на 6<
<7 на 7<
<8 на 8<
<9 на 9<
<<<<< на$
0$ на $0
1$ на $1
2$ на $2
3$ на $3
4$ на $4
5$ на $5
6$ на $6
7$ на $7
8$ на $8
9$ на $9
=$ на .FIN
0= на =0
1= на =1
2= на =2
3= на =3
4= на =4
5= на =5
6= на =6
7= на =7
8= на =8
9= на =9
_> на «пустая строка»
0> на >0
1> на >1
2> на >2
3> на >3
4> на >4
5> на >5
6> на >6
7> на >7
8> на >8
9> на >9
p> на «пустая строка»
p на .FIN
_ на .FIN
Dear Sirs, programmers offer a competition from the series “Are you weak?” (Allows you to strain your brain and take a break from the usual programming). The task is simple - to compose a Markov algorithm for multiplying two ordinary fractions.
Example: Input: (1/2) * (2/5)
The result should be 1/5.
If it is interesting to anyone, go for it.