The algorithm is expressed in quite a few more words than needed. In reality this is a simple look up table:

0 -> 7
1 -> 8
2 -> 9
3 -> 0
4 -> 1
5 -> 2
6 -> 3
7 -> 4