Related but different.
Taken from the book: Marvin Minsky 1967 – Computation:
Finite and Infinite Machines, chapter 14.
Background
As the Gödel proved, it is possible to encode with a unique positive integer not just any string
but any list structure, with any level of nesting.
Procedure of encoding \$G(x)\$ is defined inductively:
- If \$x\$ is a number, \$G(x) = 2^x\$
- If \$x\$ is a list \$[n_0, n_1, n_2, n_3, …]\$, \$G(x) = 3^{G(n_0)} \cdot 5^{G(n_1)} \cdot 7^{G(n_2)} \cdot 11^{G(n_3)} \cdot...\$
Bases are consecutive primes.
Examples:
[0] → 3
[[0]] →
\$3^3 =\$ 27
[1, 2, 3] →
\$3^{2^1} \cdot 5^{2^2} \cdot 7^{2^3} = \$
32427005625
[[0, 1], 1] →
\$3^{3^{2^0} \cdot 5^{2^1}} \cdot 5^{2^1} = \$
15206669692833942727992099815471532675
Task
Create a program that encoding (in this way) any given (possibly ragged) list to integer number.
This is code-golf
, so the shortest code in bytes wins.
Input
Valid non-empty list.
UPD
Some considerations regarding empty lists are given here.
You may assume:
- There are no empty sublists
- Structure is not broken (all parentheses are paired)
- List contains only non-negative integers
Output
Positive integer obtained by the specified algorithm
Note
Numbers can be very large, be careful! Your code must be correct in principle, but it is acceptable that it cannot print some number. However, all test cases are checked for TIO with Mathematica program and work correctly.
Test cases
[0,0,0,0,0] → 15015
[1, 1, 1] → 11025
[[1], [1]] → 38443359375
[0, [[0]], 0] → 156462192535400390625
[[[0], 0], 0] → 128925668357571406980580744739545891609191243761536322527975268535
1
? Ah, that would be the same as the encoding of the number0
. \$\endgroup\$